Skip to content

Commit c99973c

Browse files
authored
Move one more function from checkmember.py (#18825)
This is one last small refactoring before I start the actual non-trivial changes.
1 parent b7185c9 commit c99973c

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

mypy/checkexpr.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import mypy.errorcodes as codes
1616
from mypy import applytype, erasetype, join, message_registry, nodes, operators, types
1717
from mypy.argmap import ArgTypeExpander, map_actuals_to_formals, map_formals_to_actuals
18-
from mypy.checkmember import analyze_member_access, typeddict_callable
18+
from mypy.checkmember import analyze_member_access
1919
from mypy.checkstrformat import StringFormatterChecker
2020
from mypy.erasetype import erase_type, remove_instance_last_known_values, replace_meta_vars
2121
from mypy.errors import ErrorWatcher, report_internal_error
@@ -957,7 +957,20 @@ def typeddict_callable(self, info: TypeInfo) -> CallableType:
957957
Note it is not safe to move this to type_object_type() since it will crash
958958
on plugin-generated TypedDicts, that may not have the special_alias.
959959
"""
960-
return typeddict_callable(info, self.named_type)
960+
assert info.special_alias is not None
961+
target = info.special_alias.target
962+
assert isinstance(target, ProperType) and isinstance(target, TypedDictType)
963+
expected_types = list(target.items.values())
964+
kinds = [ArgKind.ARG_NAMED] * len(expected_types)
965+
names = list(target.items.keys())
966+
return CallableType(
967+
expected_types,
968+
kinds,
969+
names,
970+
target,
971+
self.named_type("builtins.type"),
972+
variables=info.defn.type_vars,
973+
)
961974

962975
def typeddict_callable_from_context(self, callee: TypedDictType) -> CallableType:
963976
return CallableType(

mypy/checkmember.py

+1-27
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
ARG_STAR,
2020
EXCLUDED_ENUM_ATTRIBUTES,
2121
SYMBOL_FUNCBASE_TYPES,
22-
ArgKind,
2322
Context,
2423
Decorator,
2524
FuncBase,
@@ -1094,7 +1093,7 @@ def analyze_class_attribute_access(
10941093
if isinstance(node.node, TypeInfo):
10951094
if node.node.typeddict_type:
10961095
# We special-case TypedDict, because they don't define any constructor.
1097-
return typeddict_callable(node.node, mx.named_type)
1096+
return mx.chk.expr_checker.typeddict_callable(node.node)
10981097
elif node.node.fullname == "types.NoneType":
10991098
# We special case NoneType, because its stub definition is not related to None.
11001099
return TypeType(NoneType())
@@ -1280,31 +1279,6 @@ class B(A[str]): pass
12801279
return t
12811280

12821281

1283-
def typeddict_callable(info: TypeInfo, named_type: Callable[[str], Instance]) -> CallableType:
1284-
"""Construct a reasonable type for a TypedDict type in runtime context.
1285-
1286-
If it appears as a callee, it will be special-cased anyway, e.g. it is
1287-
also allowed to accept a single positional argument if it is a dict literal.
1288-
1289-
Note it is not safe to move this to type_object_type() since it will crash
1290-
on plugin-generated TypedDicts, that may not have the special_alias.
1291-
"""
1292-
assert info.special_alias is not None
1293-
target = info.special_alias.target
1294-
assert isinstance(target, ProperType) and isinstance(target, TypedDictType)
1295-
expected_types = list(target.items.values())
1296-
kinds = [ArgKind.ARG_NAMED] * len(expected_types)
1297-
names = list(target.items.keys())
1298-
return CallableType(
1299-
expected_types,
1300-
kinds,
1301-
names,
1302-
target,
1303-
named_type("builtins.type"),
1304-
variables=info.defn.type_vars,
1305-
)
1306-
1307-
13081282
def analyze_decorator_or_funcbase_access(
13091283
defn: Decorator | FuncBase, itype: Instance, name: str, mx: MemberContext
13101284
) -> Type:

0 commit comments

Comments
 (0)