Skip to content

Commit ad68de5

Browse files
committed
Optimize default plugin more
1 parent df0449b commit ad68de5

File tree

5 files changed

+47
-36
lines changed

5 files changed

+47
-36
lines changed

mypy/plugins/constants.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Constant definitions for plugins kept here to help with import cycles."""
2+
3+
from typing import Final
4+
5+
from mypy.semanal_enum import ENUM_BASES
6+
7+
SINGLEDISPATCH_TYPE: Final = "functools._SingleDispatchCallable"
8+
SINGLEDISPATCH_REGISTER_METHOD: Final = f"{SINGLEDISPATCH_TYPE}.register"
9+
SINGLEDISPATCH_CALLABLE_CALL_METHOD: Final = f"{SINGLEDISPATCH_TYPE}.__call__"
10+
SINGLEDISPATCH_REGISTER_RETURN_CLASS: Final = "_SingleDispatchRegisterCallable"
11+
SINGLEDISPATCH_REGISTER_CALLABLE_CALL_METHOD: Final = f"functools.{SINGLEDISPATCH_REGISTER_RETURN_CLASS}.__call__"
12+
13+
ENUM_NAME_ACCESS: Final = {f"{prefix}.name" for prefix in ENUM_BASES} | {
14+
f"{prefix}._name_" for prefix in ENUM_BASES
15+
}
16+
ENUM_VALUE_ACCESS: Final = {f"{prefix}.value" for prefix in ENUM_BASES} | {
17+
f"{prefix}._value_" for prefix in ENUM_BASES
18+
}

mypy/plugins/default.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
MethodSigContext,
1616
Plugin,
1717
)
18-
from mypy.plugins import singledispatch_const
18+
from mypy.plugins import constants
1919
from mypy.plugins.common import try_getting_str_literals
2020
from mypy.subtypes import is_subtype
2121
from mypy.typeops import is_literal_type_like, make_simplified_union
@@ -52,31 +52,39 @@ class DefaultPlugin(Plugin):
5252
"""Type checker plugin that is enabled by default."""
5353

5454
def get_function_hook(self, fullname: str) -> Callable[[FunctionContext], Type] | None:
55-
from mypy.plugins import ctypes, enums, singledispatch
56-
5755
if fullname == "_ctypes.Array":
56+
from mypy.plugins import ctypes
57+
5858
return ctypes.array_constructor_callback
5959
elif fullname == "functools.singledispatch":
60+
from mypy.plugins import singledispatch
61+
6062
return singledispatch.create_singledispatch_function_callback
6163
elif fullname == "functools.partial":
6264
import mypy.plugins.functools
6365

6466
return mypy.plugins.functools.partial_new_callback
6567
elif fullname == "enum.member":
68+
from mypy.plugins import enums
69+
6670
return enums.enum_member_callback
6771

6872
return None
6973

7074
def get_function_signature_hook(
7175
self, fullname: str
7276
) -> Callable[[FunctionSigContext], FunctionLike] | None:
73-
from mypy.plugins import attrs, dataclasses
74-
7577
if fullname in ("attr.evolve", "attrs.evolve", "attr.assoc", "attrs.assoc"):
78+
from mypy.plugins import attrs
79+
7680
return attrs.evolve_function_sig_callback
7781
elif fullname in ("attr.fields", "attrs.fields"):
82+
from mypy.plugins import attrs
83+
7884
return attrs.fields_function_sig_callback
7985
elif fullname == "dataclasses.replace":
86+
from mypy.plugins import dataclasses
87+
8088
return dataclasses.replace_function_sig_callback
8189
return None
8290

@@ -93,7 +101,7 @@ def get_method_signature_hook(
93101
from mypy.plugins import ctypes
94102

95103
return ctypes.array_setitem_callback
96-
elif fullname == singledispatch_const.SINGLEDISPATCH_CALLABLE_CALL_METHOD:
104+
elif fullname == constants.SINGLEDISPATCH_CALLABLE_CALL_METHOD:
97105
from mypy.plugins import singledispatch
98106

99107
return singledispatch.call_singledispatch_function_callback
@@ -126,11 +134,11 @@ def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | No
126134
from mypy.plugins import ctypes
127135

128136
return ctypes.array_iter_callback
129-
elif fullname == singledispatch_const.SINGLEDISPATCH_REGISTER_METHOD:
137+
elif fullname == constants.SINGLEDISPATCH_REGISTER_METHOD:
130138
from mypy.plugins import singledispatch
131139

132140
return singledispatch.singledispatch_register_callback
133-
elif fullname == singledispatch_const.REGISTER_CALLABLE_CALL_METHOD:
141+
elif fullname == constants.SINGLEDISPATCH_REGISTER_CALLABLE_CALL_METHOD:
134142
from mypy.plugins import singledispatch
135143

136144
return singledispatch.call_singledispatch_function_after_register_argument
@@ -141,15 +149,21 @@ def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | No
141149
return None
142150

143151
def get_attribute_hook(self, fullname: str) -> Callable[[AttributeContext], Type] | None:
144-
from mypy.plugins import ctypes, enums
145-
146152
if fullname == "_ctypes.Array.value":
153+
from mypy.plugins import ctypes
154+
147155
return ctypes.array_value_callback
148156
elif fullname == "_ctypes.Array.raw":
157+
from mypy.plugins import ctypes
158+
149159
return ctypes.array_raw_callback
150-
elif fullname in enums.ENUM_NAME_ACCESS:
160+
elif fullname in constants.ENUM_NAME_ACCESS:
161+
from mypy.plugins import enums
162+
151163
return enums.enum_name_callback
152-
elif fullname in enums.ENUM_VALUE_ACCESS:
164+
elif fullname in constants.ENUM_VALUE_ACCESS:
165+
from mypy.plugins import enums
166+
153167
return enums.enum_value_callback
154168
return None
155169

mypy/plugins/enums.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import mypy.plugin # To avoid circular imports.
2020
from mypy.nodes import TypeInfo
21-
from mypy.semanal_enum import ENUM_BASES
2221
from mypy.subtypes import is_equivalent
2322
from mypy.typeops import fixup_partial_type, make_simplified_union
2423
from mypy.types import (
@@ -31,13 +30,6 @@
3130
is_named_instance,
3231
)
3332

34-
ENUM_NAME_ACCESS: Final = {f"{prefix}.name" for prefix in ENUM_BASES} | {
35-
f"{prefix}._name_" for prefix in ENUM_BASES
36-
}
37-
ENUM_VALUE_ACCESS: Final = {f"{prefix}.value" for prefix in ENUM_BASES} | {
38-
f"{prefix}._value_" for prefix in ENUM_BASES
39-
}
40-
4133

4234
def enum_name_callback(ctx: mypy.plugin.AttributeContext) -> Type:
4335
"""This plugin refines the 'name' attribute in enums to act as if

mypy/plugins/singledispatch.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
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
12+
from mypy.plugins.constants import SINGLEDISPATCH_REGISTER_RETURN_CLASS
1313
from mypy.subtypes import is_subtype
1414
from mypy.types import (
1515
AnyType,
@@ -53,8 +53,8 @@ def get_first_arg(args: list[list[T]]) -> T | None:
5353
def make_fake_register_class_instance(
5454
api: CheckerPluginInterface, type_args: Sequence[Type]
5555
) -> Instance:
56-
defn = ClassDef(REGISTER_RETURN_CLASS, Block([]))
57-
defn.fullname = f"functools.{REGISTER_RETURN_CLASS}"
56+
defn = ClassDef(SINGLEDISPATCH_REGISTER_RETURN_CLASS, Block([]))
57+
defn.fullname = f"functools.{SINGLEDISPATCH_REGISTER_RETURN_CLASS}"
5858
info = TypeInfo(SymbolTable(), defn, "functools")
5959
obj_type = api.named_generic_type("builtins.object", []).type
6060
info.bases = [Instance(obj_type, [])]

mypy/plugins/singledispatch_const.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)