@@ -357,7 +357,7 @@ def _type_check(arg, msg):
357
357
if (
358
358
type (arg ).__name__ in ('_Union' , '_Optional' ) and
359
359
not getattr (arg , '__origin__' , None ) or
360
- isinstance (arg , TypingMeta ) and _gorg ( arg ) in (Generic , _Protocol )
360
+ isinstance (arg , TypingMeta ) and arg . _gorg in (Generic , _Protocol )
361
361
):
362
362
raise TypeError ("Plain %s is not valid as type argument" % arg )
363
363
return arg
@@ -945,29 +945,6 @@ def __getitem__(self, arg):
945
945
Optional = _Optional (_root = True )
946
946
947
947
948
- def _gorg (a ):
949
- """Return the farthest origin of a generic class (internal helper)."""
950
- assert isinstance (a , GenericMeta )
951
- while a .__origin__ is not None :
952
- a = a .__origin__
953
- return a
954
-
955
-
956
- def _geqv (a , b ):
957
- """Return whether two generic classes are equivalent (internal helper).
958
-
959
- The intention is to consider generic class X and any of its
960
- parameterized forms (X[T], X[int], etc.) as equivalent.
961
-
962
- However, X is not equivalent to a subclass of X.
963
-
964
- The relation is reflexive, symmetric and transitive.
965
- """
966
- assert isinstance (a , GenericMeta ) and isinstance (b , GenericMeta )
967
- # Reduce each to its origin.
968
- return _gorg (a ) is _gorg (b )
969
-
970
-
971
948
def _next_in_mro (cls ):
972
949
"""Helper for Generic.__new__.
973
950
@@ -977,7 +954,7 @@ def _next_in_mro(cls):
977
954
next_in_mro = object
978
955
# Look for the last occurrence of Generic or Generic[...].
979
956
for i , c in enumerate (cls .__mro__ [:- 1 ]):
980
- if isinstance (c , GenericMeta ) and _gorg ( c ) is Generic :
957
+ if isinstance (c , GenericMeta ) and c . _gorg is Generic :
981
958
next_in_mro = cls .__mro__ [i + 1 ]
982
959
return next_in_mro
983
960
@@ -1078,13 +1055,15 @@ def __new__(cls, name, bases, namespace,
1078
1055
extra = namespace .get ('__extra__' )
1079
1056
if extra is not None and type (extra ) is abc .ABCMeta and extra not in bases :
1080
1057
bases = (extra ,) + bases
1081
- bases = tuple (_gorg ( b ) if isinstance (b , GenericMeta ) else b for b in bases )
1058
+ bases = tuple (b . _gorg if isinstance (b , GenericMeta ) else b for b in bases )
1082
1059
1083
1060
# remove bare Generic from bases if there are other generic bases
1084
1061
if any (isinstance (b , GenericMeta ) and b is not Generic for b in bases ):
1085
1062
bases = tuple (b for b in bases if b is not Generic )
1086
1063
namespace .update ({'__origin__' : origin , '__extra__' : extra })
1087
1064
self = super (GenericMeta , cls ).__new__ (cls , name , bases , namespace )
1065
+ super (GenericMeta , self ).__setattr__ ('_gorg' ,
1066
+ self if not origin else origin ._gorg )
1088
1067
1089
1068
self .__parameters__ = tvars
1090
1069
# Be prepared that GenericMeta will be subclassed by TupleMeta
@@ -1131,7 +1110,7 @@ def __init__(self, *args, **kwargs):
1131
1110
def _abc_negative_cache (self ):
1132
1111
if isinstance (self .__extra__ , abc .ABCMeta ):
1133
1112
return self .__extra__ ._abc_negative_cache
1134
- return _gorg ( self ) ._abc_generic_negative_cache
1113
+ return self . _gorg ._abc_generic_negative_cache
1135
1114
1136
1115
@_abc_negative_cache .setter
1137
1116
def _abc_negative_cache (self , value ):
@@ -1145,7 +1124,7 @@ def _abc_negative_cache(self, value):
1145
1124
def _abc_negative_cache_version (self ):
1146
1125
if isinstance (self .__extra__ , abc .ABCMeta ):
1147
1126
return self .__extra__ ._abc_negative_cache_version
1148
- return _gorg ( self ) ._abc_generic_negative_cache_version
1127
+ return self . _gorg ._abc_generic_negative_cache_version
1149
1128
1150
1129
@_abc_negative_cache_version .setter
1151
1130
def _abc_negative_cache_version (self , value ):
@@ -1195,7 +1174,7 @@ def _subs_tree(self, tvars=None, args=None):
1195
1174
if self .__origin__ is None :
1196
1175
return self
1197
1176
tree_args = _subs_tree (self , tvars , args )
1198
- return (_gorg ( self ) ,) + tuple (tree_args )
1177
+ return (self . _gorg ,) + tuple (tree_args )
1199
1178
1200
1179
def __eq__ (self , other ):
1201
1180
if not isinstance (other , GenericMeta ):
@@ -1211,7 +1190,7 @@ def __hash__(self):
1211
1190
def __getitem__ (self , params ):
1212
1191
if not isinstance (params , tuple ):
1213
1192
params = (params ,)
1214
- if not params and not _gorg ( self ) is Tuple :
1193
+ if not params and self . _gorg is not Tuple :
1215
1194
raise TypeError (
1216
1195
"Parameter list to %s[...] cannot be empty" % _qualname (self ))
1217
1196
msg = "Parameters to generic types must be types."
@@ -1287,7 +1266,7 @@ def __setattr__(self, attr, value):
1287
1266
):
1288
1267
super (GenericMeta , self ).__setattr__ (attr , value )
1289
1268
else :
1290
- super (GenericMeta , _gorg ( self ) ).__setattr__ (attr , value )
1269
+ super (GenericMeta , self . _gorg ).__setattr__ (attr , value )
1291
1270
1292
1271
1293
1272
# Prevent checks for Generic to crash when defining Generic.
@@ -1300,7 +1279,7 @@ def _generic_new(base_cls, cls, *args, **kwds):
1300
1279
if cls .__origin__ is None :
1301
1280
return base_cls .__new__ (cls )
1302
1281
else :
1303
- origin = _gorg ( cls )
1282
+ origin = cls . _gorg
1304
1283
obj = base_cls .__new__ (origin )
1305
1284
try :
1306
1285
obj .__orig_class__ = cls
@@ -1335,7 +1314,7 @@ def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
1335
1314
__slots__ = ()
1336
1315
1337
1316
def __new__ (cls , * args , ** kwds ):
1338
- if _geqv ( cls , Generic ) :
1317
+ if cls . _gorg is Generic :
1339
1318
raise TypeError ("Type Generic cannot be instantiated; "
1340
1319
"it can be used only as a base class" )
1341
1320
return _generic_new (cls .__next_in_mro__ , cls , * args , ** kwds )
@@ -1357,7 +1336,7 @@ class TupleMeta(GenericMeta):
1357
1336
1358
1337
@_tp_cache
1359
1338
def __getitem__ (self , parameters ):
1360
- if self .__origin__ is not None or not _geqv ( self , Tuple ) :
1339
+ if self .__origin__ is not None or self . _gorg is not Tuple :
1361
1340
# Normal generic rules apply if this is not the first subscription
1362
1341
# or a subscription of a subclass.
1363
1342
return super (TupleMeta , self ).__getitem__ (parameters )
@@ -1401,7 +1380,7 @@ class Tuple(tuple):
1401
1380
__slots__ = ()
1402
1381
1403
1382
def __new__ (cls , * args , ** kwds ):
1404
- if _geqv ( cls , Tuple ) :
1383
+ if cls . _gorg is Tuple :
1405
1384
raise TypeError ("Type Tuple cannot be instantiated; "
1406
1385
"use tuple() instead" )
1407
1386
return _generic_new (tuple , cls , * args , ** kwds )
@@ -1416,7 +1395,7 @@ def __repr__(self):
1416
1395
return self ._tree_repr (self ._subs_tree ())
1417
1396
1418
1397
def _tree_repr (self , tree ):
1419
- if _gorg ( self ) is not Callable :
1398
+ if self . _gorg is not Callable :
1420
1399
return super (CallableMeta , self )._tree_repr (tree )
1421
1400
# For actual Callable (not its subclass) we override
1422
1401
# super(CallableMeta, self)._tree_repr() for nice formatting.
@@ -1436,7 +1415,7 @@ def __getitem__(self, parameters):
1436
1415
with hashable arguments to improve speed.
1437
1416
"""
1438
1417
1439
- if self .__origin__ is not None or not _geqv ( self , Callable ) :
1418
+ if self .__origin__ is not None or self . _gorg is not Callable :
1440
1419
return super (CallableMeta , self ).__getitem__ (parameters )
1441
1420
if not isinstance (parameters , tuple ) or len (parameters ) != 2 :
1442
1421
raise TypeError ("Callable must be used as "
@@ -1480,7 +1459,7 @@ class Callable(object):
1480
1459
__slots__ = ()
1481
1460
1482
1461
def __new__ (cls , * args , ** kwds ):
1483
- if _geqv ( cls , Callable ) :
1462
+ if cls . _gorg is Callable :
1484
1463
raise TypeError ("Type Callable cannot be instantiated; "
1485
1464
"use a non-abstract subclass instead" )
1486
1465
return _generic_new (cls .__next_in_mro__ , cls , * args , ** kwds )
@@ -1530,7 +1509,7 @@ def no_type_check(arg):
1530
1509
if isinstance (arg , type ):
1531
1510
arg_attrs = arg .__dict__ .copy ()
1532
1511
for attr , val in arg .__dict__ .items ():
1533
- if val in arg .__bases__ :
1512
+ if val in arg .__bases__ + ( arg ,) :
1534
1513
arg_attrs .pop (attr )
1535
1514
for obj in arg_attrs .values ():
1536
1515
if isinstance (obj , types .FunctionType ):
@@ -1647,6 +1626,7 @@ def _get_protocol_attrs(self):
1647
1626
if (not attr .startswith ('_abc_' ) and
1648
1627
attr != '__abstractmethods__' and
1649
1628
attr != '_is_protocol' and
1629
+ attr != '_gorg' and
1650
1630
attr != '__dict__' and
1651
1631
attr != '__args__' and
1652
1632
attr != '__slots__' and
@@ -1798,7 +1778,7 @@ class List(list, MutableSequence[T]):
1798
1778
__extra__ = list
1799
1779
1800
1780
def __new__ (cls , * args , ** kwds ):
1801
- if _geqv ( cls , List ) :
1781
+ if cls . _gorg is List :
1802
1782
raise TypeError ("Type List cannot be instantiated; "
1803
1783
"use list() instead" )
1804
1784
return _generic_new (list , cls , * args , ** kwds )
@@ -1809,7 +1789,7 @@ class Deque(collections.deque, MutableSequence[T]):
1809
1789
__extra__ = collections .deque
1810
1790
1811
1791
def __new__ (cls , * args , ** kwds ):
1812
- if _geqv ( cls , Deque ) :
1792
+ if cls . _gorg is Deque :
1813
1793
return collections .deque (* args , ** kwds )
1814
1794
return _generic_new (collections .deque , cls , * args , ** kwds )
1815
1795
@@ -1819,7 +1799,7 @@ class Set(set, MutableSet[T]):
1819
1799
__extra__ = set
1820
1800
1821
1801
def __new__ (cls , * args , ** kwds ):
1822
- if _geqv ( cls , Set ) :
1802
+ if cls . _gorg is Set :
1823
1803
raise TypeError ("Type Set cannot be instantiated; "
1824
1804
"use set() instead" )
1825
1805
return _generic_new (set , cls , * args , ** kwds )
@@ -1830,7 +1810,7 @@ class FrozenSet(frozenset, AbstractSet[T_co]):
1830
1810
__extra__ = frozenset
1831
1811
1832
1812
def __new__ (cls , * args , ** kwds ):
1833
- if _geqv ( cls , FrozenSet ) :
1813
+ if cls . _gorg is FrozenSet :
1834
1814
raise TypeError ("Type FrozenSet cannot be instantiated; "
1835
1815
"use frozenset() instead" )
1836
1816
return _generic_new (frozenset , cls , * args , ** kwds )
@@ -1887,7 +1867,7 @@ class Dict(dict, MutableMapping[KT, VT]):
1887
1867
__extra__ = dict
1888
1868
1889
1869
def __new__ (cls , * args , ** kwds ):
1890
- if _geqv ( cls , Dict ) :
1870
+ if cls . _gorg is Dict :
1891
1871
raise TypeError ("Type Dict cannot be instantiated; "
1892
1872
"use dict() instead" )
1893
1873
return _generic_new (dict , cls , * args , ** kwds )
@@ -1898,7 +1878,7 @@ class DefaultDict(collections.defaultdict, MutableMapping[KT, VT]):
1898
1878
__extra__ = collections .defaultdict
1899
1879
1900
1880
def __new__ (cls , * args , ** kwds ):
1901
- if _geqv ( cls , DefaultDict ) :
1881
+ if cls . _gorg is DefaultDict :
1902
1882
return collections .defaultdict (* args , ** kwds )
1903
1883
return _generic_new (collections .defaultdict , cls , * args , ** kwds )
1904
1884
@@ -1908,7 +1888,7 @@ class Counter(collections.Counter, Dict[T, int]):
1908
1888
__extra__ = collections .Counter
1909
1889
1910
1890
def __new__ (cls , * args , ** kwds ):
1911
- if _geqv ( cls , Counter ) :
1891
+ if cls . _gorg is Counter :
1912
1892
return collections .Counter (* args , ** kwds )
1913
1893
return _generic_new (collections .Counter , cls , * args , ** kwds )
1914
1894
@@ -1927,7 +1907,7 @@ class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co]):
1927
1907
__extra__ = _G_base
1928
1908
1929
1909
def __new__ (cls , * args , ** kwds ):
1930
- if _geqv ( cls , Generator ) :
1910
+ if cls . _gorg is Generator :
1931
1911
raise TypeError ("Type Generator cannot be instantiated; "
1932
1912
"create a subclass instead" )
1933
1913
return _generic_new (_G_base , cls , * args , ** kwds )
0 commit comments