@@ -40,11 +40,39 @@ def get_possible_methods(method: Callable) -> list[Callable]:
40
40
return overloads
41
41
42
42
43
+ base_types = (
44
+ int ,
45
+ float ,
46
+ str ,
47
+ bytes ,
48
+ bytearray ,
49
+ bool ,
50
+ type ,
51
+ BaseException ,
52
+ set ,
53
+ list ,
54
+ tuple ,
55
+ range ,
56
+ memoryview ,
57
+ dict ,
58
+ frozenset ,
59
+ complex ,
60
+ )
61
+
62
+
43
63
class Intersection :
44
64
__intersects__ : set [type [object ]]
45
65
46
66
def __init__ (self , * intersects : type [object ]) -> None :
47
67
self .__intersects__ = set (reversed (intersects ))
68
+ for i in self .__intersects__ :
69
+ if not hasattr (i , "mro" ):
70
+ raise ValueError (f"mro not found for { i } " )
71
+ for base_type in base_types :
72
+ if base_type in i .mro ():
73
+ raise TypeError (
74
+ f"Type { i } found to derive from base type { base_type } "
75
+ )
48
76
self ._test_lsp ()
49
77
50
78
def __class_getitem__ (cls , key ):
@@ -55,14 +83,15 @@ def _test_lsp(self):
55
83
signatures : dict [str , list [Signature ]] = {}
56
84
for i in self .__intersects__ :
57
85
# Resolve basic annotations, ensuring no clashes
58
- for annotation_name , annotation_type in i .__annotations__ .items ():
59
- if annotation_name in intersected_attrs :
60
- if annotation_type != intersected_attrs [annotation_name ]:
61
- raise TypeError (
62
- f"Attribute { annotation_name } has type clash on { annotation_type } vs { intersected_attrs [annotation_name ]} "
63
- )
64
- else :
65
- intersected_attrs [annotation_name ] = annotation_type
86
+ if hasattr (i , "__annotations__" ):
87
+ for annotation_name , annotation_type in i .__annotations__ .items ():
88
+ if annotation_name in intersected_attrs :
89
+ if annotation_type != intersected_attrs [annotation_name ]:
90
+ raise TypeError (
91
+ f"Attribute { annotation_name } has type clash on { annotation_type } vs { intersected_attrs [annotation_name ]} "
92
+ )
93
+ else :
94
+ intersected_attrs [annotation_name ] = annotation_type
66
95
67
96
for method_name in dir (i ):
68
97
method = getattr (i , method_name )
0 commit comments