Repository: django/django
File: tests/utils_tests/test_inspect.py (Python)

Code before:
    def test_func_supports_parameter(self):
        self.assertIs(
            inspect.func_supports_parameter(Person.all_kinds, "address"), True
        )
        self.assertIs(
            inspect.func_supports_parameter(Person().all_kinds, "address"),
            True,
        )
        self.assertIs(inspect.func_supports_parameter(Person.all_kinds, "zone"), False)
        self.assertIs(
            inspect.func_supports_parameter(Person().all_kinds, "zone"),
            False,
        )

    def test_func_accepts_kwargs(self):
        self.assertIs(inspect.func_accepts_kwargs(Person.just_args), False)
        self.assertIs(inspect.func_accepts_kwargs(Person().just_args), False)
        self.assertIs(inspect.func_accepts_kwargs(Person.all_kinds), True)
        self.assertIs(inspect.func_accepts_kwargs(Person().just_args), False)

    def test_regression_36696(self):
        # See https://code.djangoproject.com/ticket/36696
        # inspection failed with deferred annotations starting with python 3.14
        # however, we can only render this bug with Python 3.14, as earlier
        # versions would trigger the NameError on initialization of this
        # module.
        if not PY314:
            return

        self.assertIs(inspect.func_accepts_kwargs(func_with_annotations), False)


class IsModuleLevelFunctionTestCase(unittest.TestCase):
    @classmethod
    def _class_method(cls) -> None:
        return None

    @staticmethod
    def _static_method() -> None:
        return None

    def test_builtin(self):
        self.assertIs(inspect.is_module_level_function(any), False)
        self.assertIs(inspect.is_module_level_function(isinstance), False)

    def test_from_module(self):
        self.assertIs(inspect.is_module_level_function(subprocess.run), True)
        self.assertIs(inspect.is_module_level_function(subprocess.check_output), True)
        self.assertIs(
            inspect.is_module_level_function(inspect.is_module_level_function), True
        )

Review comment (bug):
We normally don't include links to tickets in test comments, and avoid using `we` and similar. Maybe:
```
        # Inspection fails with deferred annotations with python 3.14+. Earlier
        # Python versions trigger the NameError on module initialization.
```

Code after:
            True,
        )
        self.assertIs(inspect.func_supports_parameter(Person.all_kinds, "zone"), False)
        self.assertIs(
            inspect.func_supports_parameter(Person().all_kinds, "zone"),
            False,
        )

    def test_func_accepts_kwargs(self):
        self.assertIs(inspect.func_accepts_kwargs(Person.just_args), False)
        self.assertIs(inspect.func_accepts_kwargs(Person().just_args), False)
        self.assertIs(inspect.func_accepts_kwargs(Person.all_kinds), True)
        self.assertIs(inspect.func_accepts_kwargs(Person().just_args), False)

    @unittest.skipUnless(PY314, "Deferred annotations are Python 3.14+ only")
    def test_func_accepts_kwargs_deferred_annotations(self):

        def func_with_annotations(self, name: str, complex: SafeString) -> None:
            pass

        # Inspection fails with deferred annotations with python 3.14+. Earlier
        # Python versions trigger the NameError on module initialization.
        self.assertIs(inspect.func_accepts_kwargs(func_with_annotations), False)


class IsModuleLevelFunctionTestCase(unittest.TestCase):
    @classmethod
    def _class_method(cls) -> None:
        return None

    @staticmethod
    def _static_method() -> None:
        return None

    def test_builtin(self):
        self.assertIs(inspect.is_module_level_function(any), False)
        self.assertIs(inspect.is_module_level_function(isinstance), False)

    def test_from_module(self):
        self.assertIs(inspect.is_module_level_function(subprocess.run), True)
        self.assertIs(inspect.is_module_level_function(subprocess.check_output), True)
        self.assertIs(
            inspect.is_module_level_function(inspect.is_module_level_function), True
        )

    def test_private_function(self):
        def private_function():
            pass

        self.assertIs(inspect.is_module_level_function(private_function), False)