| 1 |
import unittest |
| 2 |
from typing import TYPE_CHECKING |
| 3 |
|
| 4 |
from django.utils import inspect |
| 5 |
from django.utils.version import PY314 |
| 6 |
|
| 7 |
if TYPE_CHECKING: |
| 8 |
from django.utils.safestring import SafeString |
| 9 |
|
| 10 |
|
| 11 |
class Person: |
| 12 |
def no_arguments(self): |
| 13 |
return None |
| 14 |
|
| 15 |
def one_argument(self, something): |
| 16 |
return something |
| 17 |
|
| 18 |
def just_args(self, *args): |
| 19 |
return args |
| 20 |
|
| 21 |
def all_kinds(self, name, address="home", age=25, *args, **kwargs): |
| 22 |
return kwargs |
| 23 |
|
| 24 |
@classmethod |
| 25 |
def cls_all_kinds(cls, name, address="home", age=25, *args, **kwargs): |
| 26 |
return kwargs |
| 27 |
|
| 28 |
|
| 29 |
class TestInspectMethods(unittest.TestCase): |
| 30 |
def test_get_callable_parameters(self): |
| 31 |
self.assertIs( |
| 32 |
inspect._get_callable_parameters(Person.no_arguments), |
| 33 |
inspect._get_callable_parameters(Person.no_arguments), |
| 34 |
) |
| 35 |
self.assertIs( |
| 36 |
inspect._get_callable_parameters(Person().no_arguments), |
| 37 |
inspect._get_callable_parameters(Person().no_arguments), |
| 38 |
) |
| 39 |
|
| 40 |
def test_get_func_full_args_no_arguments(self): |
| 41 |
self.assertEqual(inspect.get_func_full_args(Person.no_arguments), []) |
| 42 |
self.assertEqual(inspect.get_func_full_args(Person().no_arguments), []) |
| 43 |
|
| 44 |
def test_get_func_full_args_one_argument(self): |
| 45 |
self.assertEqual( |
| 46 |
inspect.get_func_full_args(Person.one_argument), [("something",)] |
| 47 |
) |
| 48 |
self.assertEqual( |
| 49 |
inspect.get_func_full_args(Person().one_argument), |
| 50 |
[("something",)], |
| 51 |
) |
| 52 |
|
| 53 |
def test_get_func_full_args_all_arguments_method(self): |
| 54 |
arguments = [ |
| 55 |
("name",), |
| 56 |
("address", "home"), |
| 57 |
("age", 25), |
| 58 |
("*args",), |
| 59 |
("**kwargs",), |
| 60 |
] |
| 61 |
self.assertEqual(inspect.get_func_full_args(Person.all_kinds), arguments) |
| 62 |
self.assertEqual(inspect.get_func_full_args(Person().all_kinds), arguments) |
| 63 |
|
| 64 |
def test_get_func_full_args_all_arguments_classmethod(self): |
| 65 |
arguments = [ |
| 66 |
("name",), |
| 67 |
("address", "home"), |
| 68 |
("age", 25), |
| 69 |
("*args",), |
| 70 |
("**kwargs",), |
| 71 |
] |
| 72 |
self.assertEqual(inspect.get_func_full_args(Person.cls_all_kinds), arguments) |
| 73 |
self.assertEqual(inspect.get_func_full_args(Person().cls_all_kinds), arguments) |
| 74 |
|
| 75 |
def test_func_accepts_var_args_has_var_args(self): |
| 76 |
self.assertIs(inspect.func_accepts_var_args(Person.just_args), True) |
| 77 |
self.assertIs(inspect.func_accepts_var_args(Person().just_args), True) |
| 78 |
|
| 79 |
def test_func_accepts_var_args_no_var_args(self): |
| 80 |
self.assertIs(inspect.func_accepts_var_args(Person.one_argument), False) |
| 81 |
self.assertIs(inspect.func_accepts_var_args(Person().one_argument), False) |
| 82 |
|
| 83 |
def test_method_has_no_args(self): |
| 84 |
self.assertIs(inspect.method_has_no_args(Person.no_arguments), True) |
| 85 |
self.assertIs(inspect.method_has_no_args(Person().no_arguments), True) |
| 86 |
self.assertIs(inspect.method_has_no_args(Person.one_argument), False) |
| 87 |
self.assertIs(inspect.method_has_no_args(Person().one_argument), False) |
| 88 |
|
| 89 |
def test_func_supports_parameter(self): |
| 90 |
self.assertIs( |
| 91 |
inspect.func_supports_parameter(Person.all_kinds, "address"), True |
| 92 |
) |
| 93 |
self.assertIs( |
| 94 |
inspect.func_supports_parameter(Person().all_kinds, "address"), |
| 95 |
True, |
| 96 |
) |
| 97 |
self.assertIs(inspect.func_supports_parameter(Person.all_kinds, "zone"), False) |
| 98 |
self.assertIs( |
| 99 |
inspect.func_supports_parameter(Person().all_kinds, "zone"), |
| 100 |
False, |
| 101 |
) |
| 102 |
|
| 103 |
def test_func_accepts_kwargs(self): |
| 104 |
self.assertIs(inspect.func_accepts_kwargs(Person.just_args), False) |
| 105 |
self.assertIs(inspect.func_accepts_kwargs(Person().just_args), False) |
| 106 |
self.assertIs(inspect.func_accepts_kwargs(Person.all_kinds), True) |
| 107 |
self.assertIs(inspect.func_accepts_kwargs(Person().just_args), False) |
| 108 |
|
| 109 |
@unittest.skipUnless(PY314, "Deferred annotations are Python 3.14+ only") |
| 110 |
def test_func_accepts_kwargs_deferred_annotations(self): |
| 111 |
|
| 112 |
def func_with_annotations(self, name: str, complex: SafeString) -> None: |
| 113 |
pass |
| 114 |
|
| 115 |
# Inspection fails with deferred annotations with python 3.14+. Earlier |
| 116 |
# Python versions trigger the NameError on module initialization. |
| 117 |
self.assertIs(inspect.func_accepts_kwargs(func_with_annotations), False) |