Repository navigation
is_simple_callable raises NameError on Python 3.14 when method has TYPE_CHECKING-guarded annotations
#9959
|
The function only inspects parameter kinds and defaults — it never uses annotations. Steps to reproduce# models.py
from typing import TYPE_CHECKING
from django.db import models
if TYPE_CHECKING:
from myapp.other import SomeType
class MyModel(models.Model):
name = models.CharField(max_length=100)
def get_display_name(self) -> "SomeType":
return self.name
# serializers.py
from rest_framework import serializers
class MyModelSerializer(serializers.ModelSerializer):
display_name = serializers.CharField(source="get_display_name")
class Meta:
model = MyModel
fields = ["name", "display_name"]On Python <= 3.13 (or with The traceback goes through:
Expected behavior
Suggested fiximport sys
def is_simple_callable(obj):
...
kwargs = {}
if sys.version_info >= (3, 14):
import annotationlib
kwargs["annotation_format"] = annotationlib.Format.FORWARDREF
sig = inspect.signature(obj, **kwargs)
...Or, since try:
import annotationlib
_SIGNATURE_KWARGS = {"annotation_format": annotationlib.Format.FORWARDREF}
except ImportError:
_SIGNATURE_KWARGS = {}
def is_simple_callable(obj):
...
sig = inspect.signature(obj, **_SIGNATURE_KWARGS)
...Environment
|
Replies: 2 comments 2 replies
|
Thanks for the report and detailed explanation. The suggested fix looks good to me. I have a preference for the |
Should be fixed by #10069 now