diff options
| author | 93578237 <43147888+93578237@users.noreply.github.com> | 2026-02-09 16:06:50 -0500 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2026-02-10 17:08:13 -0500 |
| commit | a4999ef1b9790a4c0e793cf0e5c464e9935c3c3a (patch) | |
| tree | f40aea6d8d811cb1ea3e8babc428debd2d175675 /django/utils | |
| parent | e9b85373580338c4878d3f930b52c361398065ad (diff) | |
[5.2.x] Fixed #36903 -- Fixed further NameErrors when inspecting functions with deferred annotations.
Provide a wrapper for safe introspection of user functions on Python 3.14+.
Follow-up to 601914722956cc41f1f2c53972d669ddee6ffc04.
Backport of 56ed37e17e5b1a509aa68a0c797dcff34fcc1366 from main.
Diffstat (limited to 'django/utils')
| -rw-r--r-- | django/utils/inspect.py | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/django/utils/inspect.py b/django/utils/inspect.py index 6f6bd7b7b9..b5d968062b 100644 --- a/django/utils/inspect.py +++ b/django/utils/inspect.py @@ -17,16 +17,7 @@ if PY314: @functools.lru_cache(maxsize=512) def _get_func_parameters(func, remove_first): - # As the annotations are not used in any case, inspect the signature with - # FORWARDREF to leave any deferred annotations unevaluated. - if PY314: - signature = inspect.signature( - func, annotation_format=annotationlib.Format.FORWARDREF - ) - else: - signature = inspect.signature(func) - - parameters = tuple(signature.parameters.values()) + parameters = tuple(signature(func).parameters.values()) if remove_first: parameters = parameters[1:] return parameters @@ -120,3 +111,14 @@ def lazy_annotations(): yield finally: inspect._signature_from_callable = original_helper + + +def signature(obj): + """ + A wrapper around inspect.signature that leaves deferred annotations + unevaluated on Python 3.14+, since they are not used in our case. + """ + if PY314: + return inspect.signature(obj, annotation_format=annotationlib.Format.FORWARDREF) + else: + return inspect.signature(obj) |
