@@ -76,6 +76,17 @@ def wrapper(self):
76
76
return wrapper
77
77
78
78
79
+ class EqualToForwardRef :
80
+ def __init__ (self , arg , module = None ):
81
+ self .arg = arg
82
+ self .module = module
83
+
84
+ def __eq__ (self , other ):
85
+ if not isinstance (other , ForwardRef ):
86
+ return NotImplemented
87
+ return self .arg == other .__forward_arg__ and self .module == other .__forward_module__
88
+
89
+
79
90
class Employee :
80
91
pass
81
92
@@ -467,8 +478,8 @@ def test_or(self):
467
478
self .assertEqual (X | "x" , Union [X , "x" ])
468
479
self .assertEqual ("x" | X , Union ["x" , X ])
469
480
# make sure the order is correct
470
- self .assertEqual (get_args (X | "x" ), (X , ForwardRef ("x" )))
471
- self .assertEqual (get_args ("x" | X ), (ForwardRef ("x" ), X ))
481
+ self .assertEqual (get_args (X | "x" ), (X , EqualToForwardRef ("x" )))
482
+ self .assertEqual (get_args ("x" | X ), (EqualToForwardRef ("x" ), X ))
472
483
473
484
def test_union_constrained (self ):
474
485
A = TypeVar ('A' , str , bytes )
@@ -4965,7 +4976,7 @@ class C3:
4965
4976
def f (x : X ): ...
4966
4977
self .assertEqual (
4967
4978
get_type_hints (f , globals (), locals ()),
4968
- {'x' : list [list [ForwardRef ('X' )]]}
4979
+ {'x' : list [list [EqualToForwardRef ('X' )]]}
4969
4980
)
4970
4981
4971
4982
def test_pep695_generic_class_with_future_annotations (self ):
@@ -5183,12 +5194,15 @@ class Node(Generic[T]): ...
5183
5194
Callable [..., T ], Callable [[int ], int ],
5184
5195
Tuple [Any , Any ], Node [T ], Node [int ], Node [Any ], typing .Iterable [T ],
5185
5196
typing .Iterable [Any ], typing .Iterable [int ], typing .Dict [int , str ],
5186
- typing .Dict [T , Any ], ClassVar [int ], ClassVar [List [T ]], Tuple ['T' , 'T' ],
5187
- Union ['T' , int ], List ['T' ], typing .Mapping ['T' , int ]]
5197
+ typing .Dict [T , Any ], ClassVar [int ], ClassVar [List [T ]]]
5188
5198
for t in things + [Any ]:
5189
5199
self .assertEqual (t , copy (t ))
5190
5200
self .assertEqual (t , deepcopy (t ))
5191
5201
5202
+ shallow_things = [Tuple ['T' , 'T' ], Union ['T' , int ], List ['T' ], typing .Mapping ['T' , int ]]
5203
+ for t in things + [Any ]:
5204
+ self .assertEqual (t , copy (t ))
5205
+
5192
5206
def test_immutability_by_copy_and_pickle (self ):
5193
5207
# Special forms like Union, Any, etc., generic aliases to containers like List,
5194
5208
# Mapping, etc., and type variabcles are considered immutable by copy and pickle.
@@ -6087,82 +6101,6 @@ def test_forwardref_only_str_arg(self):
6087
6101
with self .assertRaises (TypeError ):
6088
6102
typing .ForwardRef (1 ) # only `str` type is allowed
6089
6103
6090
- def test_forward_equality (self ):
6091
- fr = typing .ForwardRef ('int' )
6092
- self .assertEqual (fr , typing .ForwardRef ('int' ))
6093
- self .assertNotEqual (List ['int' ], List [int ])
6094
- self .assertNotEqual (fr , typing .ForwardRef ('int' , module = __name__ ))
6095
- frm = typing .ForwardRef ('int' , module = __name__ )
6096
- self .assertEqual (frm , typing .ForwardRef ('int' , module = __name__ ))
6097
- self .assertNotEqual (frm , typing .ForwardRef ('int' , module = '__other_name__' ))
6098
-
6099
- def test_forward_equality_gth (self ):
6100
- c1 = typing .ForwardRef ('C' )
6101
- c1_gth = typing .ForwardRef ('C' )
6102
- c2 = typing .ForwardRef ('C' )
6103
- c2_gth = typing .ForwardRef ('C' )
6104
-
6105
- class C :
6106
- pass
6107
- def foo (a : c1_gth , b : c2_gth ):
6108
- pass
6109
-
6110
- self .assertEqual (get_type_hints (foo , globals (), locals ()), {'a' : C , 'b' : C })
6111
- self .assertEqual (c1 , c2 )
6112
- self .assertEqual (c1 , c1_gth )
6113
- self .assertEqual (c1_gth , c2_gth )
6114
- self .assertEqual (List [c1 ], List [c1_gth ])
6115
- self .assertNotEqual (List [c1 ], List [C ])
6116
- self .assertNotEqual (List [c1_gth ], List [C ])
6117
- self .assertEqual (Union [c1 , c1_gth ], Union [c1 ])
6118
- self .assertEqual (Union [c1 , c1_gth , int ], Union [c1 , int ])
6119
-
6120
- def test_forward_equality_hash (self ):
6121
- c1 = typing .ForwardRef ('int' )
6122
- c1_gth = typing .ForwardRef ('int' )
6123
- c2 = typing .ForwardRef ('int' )
6124
- c2_gth = typing .ForwardRef ('int' )
6125
-
6126
- def foo (a : c1_gth , b : c2_gth ):
6127
- pass
6128
- get_type_hints (foo , globals (), locals ())
6129
-
6130
- self .assertEqual (hash (c1 ), hash (c2 ))
6131
- self .assertEqual (hash (c1_gth ), hash (c2_gth ))
6132
- self .assertEqual (hash (c1 ), hash (c1_gth ))
6133
-
6134
- c3 = typing .ForwardRef ('int' , module = __name__ )
6135
- c4 = typing .ForwardRef ('int' , module = '__other_name__' )
6136
-
6137
- self .assertNotEqual (hash (c3 ), hash (c1 ))
6138
- self .assertNotEqual (hash (c3 ), hash (c1_gth ))
6139
- self .assertNotEqual (hash (c3 ), hash (c4 ))
6140
- self .assertEqual (hash (c3 ), hash (typing .ForwardRef ('int' , module = __name__ )))
6141
-
6142
- def test_forward_equality_namespace (self ):
6143
- class A :
6144
- pass
6145
- def namespace1 ():
6146
- a = typing .ForwardRef ('A' )
6147
- def fun (x : a ):
6148
- pass
6149
- get_type_hints (fun , globals (), locals ())
6150
- return a
6151
-
6152
- def namespace2 ():
6153
- a = typing .ForwardRef ('A' )
6154
-
6155
- class A :
6156
- pass
6157
- def fun (x : a ):
6158
- pass
6159
-
6160
- get_type_hints (fun , globals (), locals ())
6161
- return a
6162
-
6163
- self .assertEqual (namespace1 (), namespace1 ())
6164
- self .assertNotEqual (namespace1 (), namespace2 ())
6165
-
6166
6104
def test_forward_repr (self ):
6167
6105
self .assertEqual (repr (List ['int' ]), "typing.List[ForwardRef('int')]" )
6168
6106
self .assertEqual (repr (List [ForwardRef ('int' , module = 'mod' )]),
@@ -6226,7 +6164,7 @@ def cmp(o1, o2):
6226
6164
r1 = namespace1 ()
6227
6165
r2 = namespace2 ()
6228
6166
self .assertIsNot (r1 , r2 )
6229
- self .assertRaises ( RecursionError , cmp , r1 , r2 )
6167
+ self .assertNotEqual ( r1 , r2 )
6230
6168
6231
6169
def test_union_forward_recursion (self ):
6232
6170
ValueList = List ['Value' ]
@@ -7146,7 +7084,7 @@ def func(x: undefined) -> undefined: ...
7146
7084
# FORWARDREF
7147
7085
self .assertEqual (
7148
7086
get_type_hints (func , format = annotationlib .Format .FORWARDREF ),
7149
- {'x' : ForwardRef ('undefined' ), 'return' : ForwardRef ('undefined' )},
7087
+ {'x' : EqualToForwardRef ('undefined' ), 'return' : EqualToForwardRef ('undefined' )},
7150
7088
)
7151
7089
7152
7090
# STRING
@@ -8030,7 +7968,7 @@ class Y(NamedTuple):
8030
7968
class Z (NamedTuple ):
8031
7969
a : None
8032
7970
b : "str"
8033
- annos = {'a' : type (None ), 'b' : ForwardRef ("str" )}
7971
+ annos = {'a' : type (None ), 'b' : EqualToForwardRef ("str" )}
8034
7972
self .assertEqual (Z .__annotations__ , annos )
8035
7973
self .assertEqual (Z .__annotate__ (annotationlib .Format .VALUE ), annos )
8036
7974
self .assertEqual (Z .__annotate__ (annotationlib .Format .FORWARDREF ), annos )
@@ -8046,7 +7984,7 @@ class X(NamedTuple):
8046
7984
"""
8047
7985
ns = run_code (textwrap .dedent (code ))
8048
7986
X = ns ['X' ]
8049
- self .assertEqual (X .__annotations__ , {'a' : ForwardRef ("int" ), 'b' : ForwardRef ("None" )})
7987
+ self .assertEqual (X .__annotations__ , {'a' : EqualToForwardRef ("int" ), 'b' : EqualToForwardRef ("None" )})
8050
7988
8051
7989
def test_deferred_annotations (self ):
8052
7990
class X (NamedTuple ):
@@ -9032,7 +8970,7 @@ class X(TypedDict):
9032
8970
class Y (TypedDict ):
9033
8971
a : None
9034
8972
b : "int"
9035
- fwdref = ForwardRef ('int' , module = __name__ )
8973
+ fwdref = EqualToForwardRef ('int' , module = __name__ )
9036
8974
self .assertEqual (Y .__annotations__ , {'a' : type (None ), 'b' : fwdref })
9037
8975
self .assertEqual (Y .__annotate__ (annotationlib .Format .FORWARDREF ), {'a' : type (None ), 'b' : fwdref })
9038
8976
0 commit comments