is_simple_callable raises NameError on Python 3.14 when method has TYPE_CHECKING-guarded annotations
#9959
Unanswered
simonpercivall
asked this question in
Potential Issue
Replies: 1 comment 2 replies
-
|
Thanks for the report and detailed explanation. The suggested fix looks good to me. I have a preference for the |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
is_simple_callable()callsinspect.signature(obj)which, under Python 3.14'sPEP 649 (lazy annotation evaluation), eagerly evaluates annotation thunks. This
raises
NameErrorwhen the annotated method uses types imported underif TYPE_CHECKING:.The function only inspects parameter kinds and defaults — it never uses annotations.
The fix is to pass
annotation_format=annotationlib.Format.FORWARDREFtoinspect.signature(), which replaces unresolvable names withForwardRefobjectsinstead of raising.
Steps to reproduce
On Python <= 3.13 (or with
from __future__ import annotations), this works fine.On Python 3.14, serializing a
MyModelinstance raises:The traceback goes through:
rest_framework.fields.get_attribute(line 107) orrest_framework.relations(line 178)is_simple_callable(line 82)inspect.signature(obj)evaluates the annotation thunkannotationlib._get_dunder_annotationstriggersNameErrorExpected behavior
is_simple_callableshould not evaluate annotations, since it only checks whetherparameters have defaults.
Suggested fix
Or, since
annotationlibis stdlib in 3.14+:Environment
Beta Was this translation helpful? Give feedback.
All reactions