Skip to content

Commit df0449b

Browse files
committed
Optimize default plugin
1 parent 4980ae5 commit df0449b

File tree

3 files changed

+44
-32
lines changed

3 files changed

+44
-32
lines changed

mypy/plugins/default.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
MethodSigContext,
1616
Plugin,
1717
)
18+
from mypy.plugins import singledispatch_const
1819
from mypy.plugins.common import try_getting_str_literals
1920
from mypy.subtypes import is_subtype
2021
from mypy.typeops import is_literal_type_like, make_simplified_union
@@ -36,6 +37,16 @@
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

4051
class 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

mypy/plugins/singledispatch.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
from __future__ import annotations
22

33
from collections.abc import Sequence
4-
from typing import Final, NamedTuple, TypeVar, Union
4+
from typing import NamedTuple, TypeVar, Union
55
from typing_extensions import TypeAlias as _TypeAlias
66

77
from mypy.messages import format_type
88
from mypy.nodes import ARG_POS, Argument, Block, ClassDef, Context, SymbolTable, TypeInfo, Var
99
from mypy.options import Options
1010
from mypy.plugin import CheckerPluginInterface, FunctionContext, MethodContext, MethodSigContext
1111
from mypy.plugins.common import add_method_to_class
12+
from mypy.plugins.singledispatch_const import REGISTER_RETURN_CLASS
1213
from mypy.subtypes import is_subtype
1314
from mypy.types import (
1415
AnyType,
@@ -33,13 +34,6 @@ class RegisterCallableInfo(NamedTuple):
3334
singledispatch_obj: Instance
3435

3536

36-
SINGLEDISPATCH_TYPE: Final = "functools._SingleDispatchCallable"
37-
38-
SINGLEDISPATCH_REGISTER_METHOD: Final = f"{SINGLEDISPATCH_TYPE}.register"
39-
40-
SINGLEDISPATCH_CALLABLE_CALL_METHOD: Final = f"{SINGLEDISPATCH_TYPE}.__call__"
41-
42-
4337
def get_singledispatch_info(typ: Instance) -> SingledispatchTypeVars | None:
4438
if len(typ.args) == 2:
4539
return SingledispatchTypeVars(*typ.args) # type: ignore[arg-type]
@@ -56,11 +50,6 @@ def get_first_arg(args: list[list[T]]) -> T | None:
5650
return None
5751

5852

59-
REGISTER_RETURN_CLASS: Final = "_SingleDispatchRegisterCallable"
60-
61-
REGISTER_CALLABLE_CALL_METHOD: Final = f"functools.{REGISTER_RETURN_CLASS}.__call__"
62-
63-
6453
def make_fake_register_class_instance(
6554
api: CheckerPluginInterface, type_args: Sequence[Type]
6655
) -> Instance:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""Constant definitions for singledispatch plugin moved here to help with import cycle."""
2+
3+
from typing import Final
4+
5+
SINGLEDISPATCH_TYPE: Final = "functools._SingleDispatchCallable"
6+
7+
SINGLEDISPATCH_REGISTER_METHOD: Final = f"{SINGLEDISPATCH_TYPE}.register"
8+
9+
SINGLEDISPATCH_CALLABLE_CALL_METHOD: Final = f"{SINGLEDISPATCH_TYPE}.__call__"
10+
11+
REGISTER_RETURN_CLASS: Final = "_SingleDispatchRegisterCallable"
12+
13+
REGISTER_CALLABLE_CALL_METHOD: Final = f"functools.{REGISTER_RETURN_CLASS}.__call__"

0 commit comments

Comments
 (0)