1515 MethodSigContext ,
1616 Plugin ,
1717)
18+ from mypy .plugins import singledispatch_const
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."""
@@ -72,34 +83,25 @@ def get_function_signature_hook(
7283 def get_method_signature_hook (
7384 self , fullname : str
7485 ) -> Callable [[MethodSigContext ], FunctionLike ] | None :
75- from mypy .plugins import ctypes , singledispatch
76-
7786 if fullname == "typing.Mapping.get" :
7887 return typed_dict_get_signature_callback
79- elif fullname in { n + ".setdefault" for n in TPDICT_FB_NAMES } :
88+ elif fullname in TD_SETDEFAULT_NAMES :
8089 return typed_dict_setdefault_signature_callback
81- elif fullname in { n + ".pop" for n in TPDICT_FB_NAMES } :
90+ elif fullname in TD_POP_NAMES :
8291 return typed_dict_pop_signature_callback
8392 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
93+ from mypy .plugins import ctypes
8794
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__" )
95+ return ctypes .array_setitem_callback
96+ elif fullname == singledispatch_const .SINGLEDISPATCH_CALLABLE_CALL_METHOD :
97+ from mypy .plugins import singledispatch
9498
95- if fullname in typed_dict_updates :
99+ return singledispatch .call_singledispatch_function_callback
100+ elif fullname in TD_UPDATE_METHOD_NAMES :
96101 return typed_dict_update_signature_callback
97-
98102 return None
99103
100104 def get_method_hook (self , fullname : str ) -> Callable [[MethodContext ], Type ] | None :
101- from mypy .plugins import ctypes , singledispatch
102-
103105 if fullname == "typing.Mapping.get" :
104106 return typed_dict_get_callback
105107 elif fullname == "builtins.int.__pow__" :
@@ -117,12 +119,20 @@ def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | No
117119 elif fullname in {n + ".__delitem__" for n in TPDICT_FB_NAMES }:
118120 return typed_dict_delitem_callback
119121 elif fullname == "_ctypes.Array.__getitem__" :
122+ from mypy .plugins import ctypes
123+
120124 return ctypes .array_getitem_callback
121125 elif fullname == "_ctypes.Array.__iter__" :
126+ from mypy .plugins import ctypes
127+
122128 return ctypes .array_iter_callback
123- elif fullname == singledispatch .SINGLEDISPATCH_REGISTER_METHOD :
129+ elif fullname == singledispatch_const .SINGLEDISPATCH_REGISTER_METHOD :
130+ from mypy .plugins import singledispatch
131+
124132 return singledispatch .singledispatch_register_callback
125- elif fullname == singledispatch .REGISTER_CALLABLE_CALL_METHOD :
133+ elif fullname == singledispatch_const .REGISTER_CALLABLE_CALL_METHOD :
134+ from mypy .plugins import singledispatch
135+
126136 return singledispatch .call_singledispatch_function_after_register_argument
127137 elif fullname == "functools.partial.__call__" :
128138 import mypy .plugins .functools
0 commit comments