15
15
MethodSigContext ,
16
16
Plugin ,
17
17
)
18
+ from mypy .plugins import constants
18
19
from mypy .plugins .common import try_getting_str_literals
19
20
from mypy .subtypes import is_subtype
20
21
from mypy .typeops import is_literal_type_like , make_simplified_union
36
37
get_proper_types ,
37
38
)
38
39
40
+ TD_SETDEFAULT_NAMES : Final = {n + ".setdefault" for n in TPDICT_FB_NAMES }
41
+ TD_POP_NAMES : Final = {n + ".pop" for n in TPDICT_FB_NAMES }
42
+
43
+ TD_UPDATE_METHOD_NAMES : Final = (
44
+ {n + ".update" for n in TPDICT_FB_NAMES }
45
+ | {n + ".__or__" for n in TPDICT_FB_NAMES }
46
+ | {n + ".__ror__" for n in TPDICT_FB_NAMES }
47
+ | {n + ".__ior__" for n in TPDICT_FB_NAMES }
48
+ )
49
+
39
50
40
51
class DefaultPlugin (Plugin ):
41
52
"""Type checker plugin that is enabled by default."""
42
53
43
54
def get_function_hook (self , fullname : str ) -> Callable [[FunctionContext ], Type ] | None :
44
- from mypy .plugins import ctypes , enums , singledispatch
45
-
46
55
if fullname == "_ctypes.Array" :
56
+ from mypy .plugins import ctypes
57
+
47
58
return ctypes .array_constructor_callback
48
59
elif fullname == "functools.singledispatch" :
60
+ from mypy .plugins import singledispatch
61
+
49
62
return singledispatch .create_singledispatch_function_callback
50
63
elif fullname == "functools.partial" :
51
64
import mypy .plugins .functools
52
65
53
66
return mypy .plugins .functools .partial_new_callback
54
67
elif fullname == "enum.member" :
68
+ from mypy .plugins import enums
69
+
55
70
return enums .enum_member_callback
56
71
57
72
return None
58
73
59
74
def get_function_signature_hook (
60
75
self , fullname : str
61
76
) -> Callable [[FunctionSigContext ], FunctionLike ] | None :
62
- from mypy .plugins import attrs , dataclasses
63
-
64
77
if fullname in ("attr.evolve" , "attrs.evolve" , "attr.assoc" , "attrs.assoc" ):
78
+ from mypy .plugins import attrs
79
+
65
80
return attrs .evolve_function_sig_callback
66
81
elif fullname in ("attr.fields" , "attrs.fields" ):
82
+ from mypy .plugins import attrs
83
+
67
84
return attrs .fields_function_sig_callback
68
85
elif fullname == "dataclasses.replace" :
86
+ from mypy .plugins import dataclasses
87
+
69
88
return dataclasses .replace_function_sig_callback
70
89
return None
71
90
72
91
def get_method_signature_hook (
73
92
self , fullname : str
74
93
) -> Callable [[MethodSigContext ], FunctionLike ] | None :
75
- from mypy .plugins import ctypes , singledispatch
76
-
77
94
if fullname == "typing.Mapping.get" :
78
95
return typed_dict_get_signature_callback
79
- elif fullname in { n + ".setdefault" for n in TPDICT_FB_NAMES } :
96
+ elif fullname in TD_SETDEFAULT_NAMES :
80
97
return typed_dict_setdefault_signature_callback
81
- elif fullname in { n + ".pop" for n in TPDICT_FB_NAMES } :
98
+ elif fullname in TD_POP_NAMES :
82
99
return typed_dict_pop_signature_callback
83
100
elif fullname == "_ctypes.Array.__setitem__" :
84
- return ctypes .array_setitem_callback
85
- elif fullname == singledispatch .SINGLEDISPATCH_CALLABLE_CALL_METHOD :
86
- return singledispatch .call_singledispatch_function_callback
101
+ from mypy .plugins import ctypes
87
102
88
- typed_dict_updates = set ()
89
- for n in TPDICT_FB_NAMES :
90
- typed_dict_updates .add (n + ".update" )
91
- typed_dict_updates .add (n + ".__or__" )
92
- typed_dict_updates .add (n + ".__ror__" )
93
- typed_dict_updates .add (n + ".__ior__" )
103
+ return ctypes .array_setitem_callback
104
+ elif fullname == constants .SINGLEDISPATCH_CALLABLE_CALL_METHOD :
105
+ from mypy .plugins import singledispatch
94
106
95
- if fullname in typed_dict_updates :
107
+ return singledispatch .call_singledispatch_function_callback
108
+ elif fullname in TD_UPDATE_METHOD_NAMES :
96
109
return typed_dict_update_signature_callback
97
-
98
110
return None
99
111
100
112
def get_method_hook (self , fullname : str ) -> Callable [[MethodContext ], Type ] | None :
101
- from mypy .plugins import ctypes , singledispatch
102
-
103
113
if fullname == "typing.Mapping.get" :
104
114
return typed_dict_get_callback
105
115
elif fullname == "builtins.int.__pow__" :
@@ -117,12 +127,20 @@ def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | No
117
127
elif fullname in {n + ".__delitem__" for n in TPDICT_FB_NAMES }:
118
128
return typed_dict_delitem_callback
119
129
elif fullname == "_ctypes.Array.__getitem__" :
130
+ from mypy .plugins import ctypes
131
+
120
132
return ctypes .array_getitem_callback
121
133
elif fullname == "_ctypes.Array.__iter__" :
134
+ from mypy .plugins import ctypes
135
+
122
136
return ctypes .array_iter_callback
123
- elif fullname == singledispatch .SINGLEDISPATCH_REGISTER_METHOD :
137
+ elif fullname == constants .SINGLEDISPATCH_REGISTER_METHOD :
138
+ from mypy .plugins import singledispatch
139
+
124
140
return singledispatch .singledispatch_register_callback
125
- elif fullname == singledispatch .REGISTER_CALLABLE_CALL_METHOD :
141
+ elif fullname == constants .SINGLEDISPATCH_REGISTER_CALLABLE_CALL_METHOD :
142
+ from mypy .plugins import singledispatch
143
+
126
144
return singledispatch .call_singledispatch_function_after_register_argument
127
145
elif fullname == "functools.partial.__call__" :
128
146
import mypy .plugins .functools
@@ -131,15 +149,21 @@ def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | No
131
149
return None
132
150
133
151
def get_attribute_hook (self , fullname : str ) -> Callable [[AttributeContext ], Type ] | None :
134
- from mypy .plugins import ctypes , enums
135
-
136
152
if fullname == "_ctypes.Array.value" :
153
+ from mypy .plugins import ctypes
154
+
137
155
return ctypes .array_value_callback
138
156
elif fullname == "_ctypes.Array.raw" :
157
+ from mypy .plugins import ctypes
158
+
139
159
return ctypes .array_raw_callback
140
- elif fullname in enums .ENUM_NAME_ACCESS :
160
+ elif fullname in constants .ENUM_NAME_ACCESS :
161
+ from mypy .plugins import enums
162
+
141
163
return enums .enum_name_callback
142
- elif fullname in enums .ENUM_VALUE_ACCESS :
164
+ elif fullname in constants .ENUM_VALUE_ACCESS :
165
+ from mypy .plugins import enums
166
+
143
167
return enums .enum_value_callback
144
168
return None
145
169
0 commit comments