@@ -74,7 +74,7 @@ def __init_subclass__(cls, /, *args, **kwds):
7474 def evaluate (self , * , globals = None , locals = None , type_params = None , owner = None ):
7575 """Evaluate the forward reference and return the value.
7676
77- If the forward reference is not evaluatable , raise an exception.
77+ If the forward reference cannot be evaluated , raise an exception.
7878 """
7979 if self .__forward_evaluated__ :
8080 return self .__forward_value__
@@ -89,12 +89,10 @@ def evaluate(self, *, globals=None, locals=None, type_params=None, owner=None):
8989 return value
9090 if owner is None :
9191 owner = self .__owner__
92- if type_params is None and owner is None :
93- raise TypeError ("Either 'type_params' or 'owner' must be provided" )
9492
95- if self .__forward_module__ is not None :
93+ if globals is None and self .__forward_module__ is not None :
9694 globals = getattr (
97- sys .modules .get (self .__forward_module__ , None ), "__dict__" , globals
95+ sys .modules .get (self .__forward_module__ , None ), "__dict__" , None
9896 )
9997 if globals is None :
10098 globals = self .__globals__
@@ -112,14 +110,14 @@ def evaluate(self, *, globals=None, locals=None, type_params=None, owner=None):
112110
113111 if locals is None :
114112 locals = {}
115- if isinstance (self . __owner__ , type ):
116- locals .update (vars (self . __owner__ ))
113+ if isinstance (owner , type ):
114+ locals .update (vars (owner ))
117115
118- if type_params is None and self . __owner__ is not None :
116+ if type_params is None and owner is not None :
119117 # "Inject" type parameters into the local namespace
120118 # (unless they are shadowed by assignments *in* the local namespace),
121119 # as a way of emulating annotation scopes when calling `eval()`
122- type_params = getattr (self . __owner__ , "__type_params__" , None )
120+ type_params = getattr (owner , "__type_params__" , None )
123121
124122 # type parameters require some special handling,
125123 # as they exist in their own scope
@@ -129,7 +127,14 @@ def evaluate(self, *, globals=None, locals=None, type_params=None, owner=None):
129127 # but should in turn be overridden by names in the class scope
130128 # (which here are called `globalns`!)
131129 if type_params is not None :
132- globals , locals = dict (globals ), dict (locals )
130+ if globals is None :
131+ globals = {}
132+ else :
133+ globals = dict (globals )
134+ if locals is None :
135+ locals = {}
136+ else :
137+ locals = dict (locals )
133138 for param in type_params :
134139 param_name = param .__name__
135140 if not self .__forward_is_class__ or param_name not in globals :
0 commit comments