@@ -74,7 +74,7 @@ def __init_subclass__(cls, /, *args, **kwds):
74
74
def evaluate (self , * , globals = None , locals = None , type_params = None , owner = None ):
75
75
"""Evaluate the forward reference and return the value.
76
76
77
- If the forward reference is not evaluatable , raise an exception.
77
+ If the forward reference cannot be evaluated , raise an exception.
78
78
"""
79
79
if self .__forward_evaluated__ :
80
80
return self .__forward_value__
@@ -89,12 +89,10 @@ def evaluate(self, *, globals=None, locals=None, type_params=None, owner=None):
89
89
return value
90
90
if owner is None :
91
91
owner = self .__owner__
92
- if type_params is None and owner is None :
93
- raise TypeError ("Either 'type_params' or 'owner' must be provided" )
94
92
95
- if self .__forward_module__ is not None :
93
+ if globals is None and self .__forward_module__ is not None :
96
94
globals = getattr (
97
- sys .modules .get (self .__forward_module__ , None ), "__dict__" , globals
95
+ sys .modules .get (self .__forward_module__ , None ), "__dict__" , None
98
96
)
99
97
if globals is None :
100
98
globals = self .__globals__
@@ -112,14 +110,14 @@ def evaluate(self, *, globals=None, locals=None, type_params=None, owner=None):
112
110
113
111
if locals is None :
114
112
locals = {}
115
- if isinstance (self . __owner__ , type ):
116
- locals .update (vars (self . __owner__ ))
113
+ if isinstance (owner , type ):
114
+ locals .update (vars (owner ))
117
115
118
- if type_params is None and self . __owner__ is not None :
116
+ if type_params is None and owner is not None :
119
117
# "Inject" type parameters into the local namespace
120
118
# (unless they are shadowed by assignments *in* the local namespace),
121
119
# 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 )
123
121
124
122
# type parameters require some special handling,
125
123
# as they exist in their own scope
@@ -129,7 +127,14 @@ def evaluate(self, *, globals=None, locals=None, type_params=None, owner=None):
129
127
# but should in turn be overridden by names in the class scope
130
128
# (which here are called `globalns`!)
131
129
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 )
133
138
for param in type_params :
134
139
param_name = param .__name__
135
140
if not self .__forward_is_class__ or param_name not in globals :
0 commit comments