File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import functools
22import inspect
33
4+ from django .utils .version import PY314
5+
6+ if PY314 :
7+ import annotationlib
8+
49
510@functools .lru_cache (maxsize = 512 )
611def _get_func_parameters (func , remove_first ):
7- parameters = tuple (inspect .signature (func ).parameters .values ())
12+ # As the annotations are not used in any case, inspect the signature with
13+ # FORWARDREF to leave any deferred annotations unevaluated.
14+ if PY314 :
15+ signature = inspect .signature (
16+ func , annotation_format = annotationlib .Format .FORWARDREF
17+ )
18+ else :
19+ signature = inspect .signature (func )
20+
21+ parameters = tuple (signature .parameters .values ())
822 if remove_first :
923 parameters = parameters [1 :]
1024 return parameters
Original file line number Diff line number Diff line change 11import unittest
2+ from typing import TYPE_CHECKING
23
34from django .utils import inspect
5+ from django .utils .version import PY314
6+
7+ if TYPE_CHECKING :
8+ from django .utils .safestring import SafeString
49
510
611class Person :
@@ -100,3 +105,13 @@ def test_func_accepts_kwargs(self):
100105 self .assertIs (inspect .func_accepts_kwargs (Person ().just_args ), False )
101106 self .assertIs (inspect .func_accepts_kwargs (Person .all_kinds ), True )
102107 self .assertIs (inspect .func_accepts_kwargs (Person ().just_args ), False )
108+
109+ @unittest .skipUnless (PY314 , "Deferred annotations are Python 3.14+ only" )
110+ def test_func_accepts_kwargs_deferred_annotations (self ):
111+
112+ def func_with_annotations (self , name : str , complex : SafeString ) -> None :
113+ pass
114+
115+ # Inspection fails with deferred annotations with python 3.14+. Earlier
116+ # Python versions trigger the NameError on module initialization.
117+ self .assertIs (inspect .func_accepts_kwargs (func_with_annotations ), False )
You can’t perform that action at this time.
0 commit comments