Skip to content

Commit b459158

Browse files
committed
rework logic for clarity
1 parent b851fab commit b459158

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

mypy/stubtest.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,15 @@ def is_subtype_helper(left: mypy.types.Type, right: mypy.types.Type) -> bool:
15931593
return mypy.subtypes.is_subtype(left, right)
15941594

15951595

1596+
def get_mypy_node_for_name(module: str, type_name: str) -> mypy.nodes.SymbolNode | None:
1597+
stub = get_stub(module)
1598+
if stub is None:
1599+
return None
1600+
if type_name not in stub.names:
1601+
return None
1602+
return stub.names[type_name].node
1603+
1604+
15961605
def get_mypy_type_of_runtime_value(
15971606
runtime: Any, type_context: mypy.types.Type | None = None
15981607
) -> mypy.types.Type | None:
@@ -1656,53 +1665,48 @@ def anytype() -> mypy.types.AnyType:
16561665
is_ellipsis_args=True,
16571666
)
16581667

1668+
skip_type_object_type = False
16591669
if type_context:
1660-
# Don't attempt to account for context if the context is generic
1670+
# Don't attempt to process the type object when context is generic
16611671
# This is related to issue #3737
16621672
type_context = mypy.types.get_proper_type(type_context)
1673+
# Callable types with a generic return value
16631674
if isinstance(type_context, mypy.types.CallableType):
16641675
if isinstance(type_context.ret_type, mypy.types.TypeVarType):
1665-
type_context = None
1676+
skip_type_object_type = True
1677+
# Type[x] where x is generic
16661678
if isinstance(type_context, mypy.types.TypeType):
16671679
if isinstance(type_context.item, mypy.types.TypeVarType):
1668-
type_context = None
1680+
skip_type_object_type = True
16691681

1670-
if type_context:
1682+
if isinstance(runtime, type) and not skip_type_object_type:
16711683

16721684
def _named_type(name: str) -> mypy.types.Instance:
16731685
parts = name.rsplit(".", maxsplit=1)
1674-
stub = get_stub(parts[0])
1675-
assert stub is not None
1676-
assert parts[1] in stub.names
1677-
node = stub.names[parts[1]]
1678-
assert isinstance(node.node, nodes.TypeInfo)
1686+
node = get_mypy_node_for_name(parts[0], parts[1])
1687+
assert isinstance(node, nodes.TypeInfo)
16791688
any_type = mypy.types.AnyType(mypy.types.TypeOfAny.special_form)
1680-
return mypy.types.Instance(node.node, [any_type] * len(node.node.defn.type_vars))
1681-
1682-
if isinstance(runtime, type):
1683-
# Try and look up a stub for the runtime object itself
1684-
# The logic here is similar to ExpressionChecker.analyze_ref_expr
1685-
stub = get_stub(runtime.__module__)
1686-
if stub is not None:
1687-
if runtime.__name__ in stub.names:
1688-
type_info = stub.names[runtime.__name__].node
1689-
if isinstance(type_info, nodes.TypeInfo):
1690-
result: mypy.types.Type | None = None
1691-
result = mypy.checkmember.type_object_type(type_info, _named_type)
1692-
if mypy.checkexpr.is_type_type_context(type_context):
1693-
# This is the type in a type[] expression, so substitute type
1694-
# variables with Any.
1695-
result = mypy.erasetype.erase_typevars(result)
1696-
return result
1689+
return mypy.types.Instance(node, [any_type] * len(node.defn.type_vars))
1690+
1691+
# Try and look up a stub for the runtime object itself
1692+
# The logic here is similar to ExpressionChecker.analyze_ref_expr
1693+
stub = get_stub(runtime.__module__)
1694+
if stub is not None:
1695+
if runtime.__name__ in stub.names:
1696+
type_info = stub.names[runtime.__name__].node
1697+
if isinstance(type_info, nodes.TypeInfo):
1698+
result: mypy.types.Type | None = None
1699+
result = mypy.checkmember.type_object_type(type_info, _named_type)
1700+
if mypy.checkexpr.is_type_type_context(type_context):
1701+
# This is the type in a type[] expression, so substitute type
1702+
# variables with Any.
1703+
result = mypy.erasetype.erase_typevars(result)
1704+
return result
16971705

16981706
# Try and look up a stub for the runtime object's type
1699-
stub = get_stub(type(runtime).__module__)
1700-
if stub is None:
1701-
return None
1702-
type_name = type(runtime).__name__
1703-
if type_name not in stub.names:
1707+
type_info = get_mypy_node_for_name(type(runtime).__module__, type(runtime).__name__)
1708+
if type_info is None:
17041709
return None
1705-
type_info = stub.names[type_name].node
17061710
if isinstance(type_info, nodes.Var):
17071711
return type_info.type
17081712
if not isinstance(type_info, nodes.TypeInfo):

0 commit comments

Comments
 (0)