Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@AugustoPontes1
Copy link
Contributor

@AugustoPontes1 AugustoPontes1 commented Nov 8, 2025

Trac ticket number

ticket-#36712

Branch description

Using the django 5.12 version with python 3.14, when you add a templatetag with deferred annotations, a name and value error raises, this PR gives a suggestion to fix this issue

Checklist

  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.
  • I have added or updated relevant docs, including release notes if applicable.
  • I have attached screenshots in both light and dark modes for any UI changes.

@prauscher
Copy link
Contributor

I guess simply using inspect.signature won't be enough, as it must be called with annotation_format=annotationlib.Format.FORWARDREF (which is only available starting in Python 3.14). I'd suggest something around #20024. As inspect.signature is available since Python 3.3 I do not think a fallback to getfullargspec is required. Maybe

from django.utils.version import PY314
if PY314:
    import annotationlib

# ...

if PY314:
    sig = inspect.signature(func, annotation_format=annotationlib.Format.FORWARDREF)
else:
    sig = inspect.signature(func)
alen = len(sig.parameters)

non_default_params = [
    p for p in sig.parameters.values() if p.default is p.empty
]
dlen = alen - len(non_default_params)

could work. A test case could probably be

from typing import TYPE_CHECKING
from django.utils.version import PY314

if TYPE_CHECKING:
    from django.utils.safestring import SafeText 


class SomeTestCase(UnitTest):
    @unittest.skipUnless(PY314, "Deferred annotations are Python 3.14+ only")
    def test_register_filter_deferred_annotations(self):
        register = template.Library()

        @register.filter("example")
        def example_filter(value: str) -> SafeText:
            return escape(value)

        # Inspection during register fails with deferred annotations with python 3.14+
        # TODO: Maybe check if filter can be used

@AugustoPontes1
Copy link
Contributor Author

Thanks for your suggestions, i implemented and tested again these changes with other python versions, it seems ok for now, but if i need to do other improvements, im here to help ☺️

Copy link
Member

@jacobtylerwalls jacobtylerwalls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates. Left a couple tips.

@jacobtylerwalls
Copy link
Member

Thanks for the updates @AugustoPontes1. Re-reading the issue, I'm realizing we have similar issues cropping up in several more places (you can grep for where else we're using getfullargspec().

I think we're going to need to factor out a helper for this, and just unit test that.

But an upstream patch would be simpler. I'm going to wait a few days to see if any responds on python/cpython#141560.

@AugustoPontes1
Copy link
Contributor Author

AugustoPontes1 commented Nov 14, 2025

Thanks for the updates @AugustoPontes1. Re-reading the issue, I'm realizing we have similar issues cropping up in several more places (you can grep for where else we're using getfullargspec().

I think we're going to need to factor out a helper for this, and just unit test that.

But an upstream patch would be simpler. I'm going to wait a few days to see if any responds on python/cpython#141560.

No problem, i also saw some PRs opened on the cpython repo that are talking about this problem, but they discussed this problem a long time ago, and from what i saw, they didnt get a solution for this, and you remembered well that there are other modules on django that uses the getfullargspec(), and definetely we will need a better solution for this and other problems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants