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 16:54:46 -0500 |
| commit | deac58ed822c6cd819e916e6c3d1736c7adfa31e (patch) | |
| tree | 705f1e46ce13320352804c299509fcf8fa3e27af /django/utils/inspect.py | |
| parent | 254b30483def1fa75270de9f45ad1f2a19f39bdb (diff) | |
[6.0.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/inspect.py')
| -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 f0f43ae17e..a04669fc11 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 @@ -130,3 +121,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) |
