1515 MethodSigContext ,
1616 Plugin ,
1717)
18+ from mypy .plugins import constants
1819from mypy .plugins .common import try_getting_str_literals
1920from mypy .subtypes import is_subtype
2021from mypy .typeops import is_literal_type_like , make_simplified_union
3637 get_proper_types ,
3738)
3839
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+
3950
4051class DefaultPlugin (Plugin ):
4152 """Type checker plugin that is enabled by default."""
4253
4354 def get_function_hook (self , fullname : str ) -> Callable [[FunctionContext ], Type ] | None :
44- from mypy .plugins import ctypes , enums , singledispatch
45-
4655 if fullname == "_ctypes.Array" :
56+ from mypy .plugins import ctypes
57+
4758 return ctypes .array_constructor_callback
4859 elif fullname == "functools.singledispatch" :
60+ from mypy .plugins import singledispatch
61+
4962 return singledispatch .create_singledispatch_function_callback
5063 elif fullname == "functools.partial" :
5164 import mypy .plugins .functools
5265
5366 return mypy .plugins .functools .partial_new_callback
5467 elif fullname == "enum.member" :
68+ from mypy .plugins import enums
69+
5570 return enums .enum_member_callback
5671
5772 return None
5873
5974 def get_function_signature_hook (
6075 self , fullname : str
6176 ) -> Callable [[FunctionSigContext ], FunctionLike ] | None :
62- from mypy .plugins import attrs , dataclasses
63-
6477 if fullname in ("attr.evolve" , "attrs.evolve" , "attr.assoc" , "attrs.assoc" ):
78+ from mypy .plugins import attrs
79+
6580 return attrs .evolve_function_sig_callback
6681 elif fullname in ("attr.fields" , "attrs.fields" ):
82+ from mypy .plugins import attrs
83+
6784 return attrs .fields_function_sig_callback
6885 elif fullname == "dataclasses.replace" :
86+ from mypy .plugins import dataclasses
87+
6988 return dataclasses .replace_function_sig_callback
7089 return None
7190
7291 def get_method_signature_hook (
7392 self , fullname : str
7493 ) -> Callable [[MethodSigContext ], FunctionLike ] | None :
75- from mypy .plugins import ctypes , singledispatch
76-
7794 if fullname == "typing.Mapping.get" :
7895 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 :
8097 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 :
8299 return typed_dict_pop_signature_callback
83100 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
87102
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
94106
95- if fullname in typed_dict_updates :
107+ return singledispatch .call_singledispatch_function_callback
108+ elif fullname in TD_UPDATE_METHOD_NAMES :
96109 return typed_dict_update_signature_callback
97-
98110 return None
99111
100112 def get_method_hook (self , fullname : str ) -> Callable [[MethodContext ], Type ] | None :
101- from mypy .plugins import ctypes , singledispatch
102-
103113 if fullname == "typing.Mapping.get" :
104114 return typed_dict_get_callback
105115 elif fullname == "builtins.int.__pow__" :
@@ -117,12 +127,20 @@ def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | No
117127 elif fullname in {n + ".__delitem__" for n in TPDICT_FB_NAMES }:
118128 return typed_dict_delitem_callback
119129 elif fullname == "_ctypes.Array.__getitem__" :
130+ from mypy .plugins import ctypes
131+
120132 return ctypes .array_getitem_callback
121133 elif fullname == "_ctypes.Array.__iter__" :
134+ from mypy .plugins import ctypes
135+
122136 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+
124140 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+
126144 return singledispatch .call_singledispatch_function_after_register_argument
127145 elif fullname == "functools.partial.__call__" :
128146 import mypy .plugins .functools
@@ -131,15 +149,21 @@ def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | No
131149 return None
132150
133151 def get_attribute_hook (self , fullname : str ) -> Callable [[AttributeContext ], Type ] | None :
134- from mypy .plugins import ctypes , enums
135-
136152 if fullname == "_ctypes.Array.value" :
153+ from mypy .plugins import ctypes
154+
137155 return ctypes .array_value_callback
138156 elif fullname == "_ctypes.Array.raw" :
157+ from mypy .plugins import ctypes
158+
139159 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+
141163 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+
143167 return enums .enum_value_callback
144168 return None
145169
0 commit comments