Skip to content

Commit

Permalink
Rename type alias helper with a misleading name (#15199)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilevkivskyi committed May 6, 2023
1 parent b69060a commit bfc1a76
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
8 changes: 4 additions & 4 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@
from mypy.traverser import has_await_expression
from mypy.typeanal import (
check_for_explicit_any,
expand_type_alias,
has_any_from_unimported_type,
instantiate_type_alias,
make_optional_type,
set_any_tvars,
)
Expand Down Expand Up @@ -3965,12 +3965,12 @@ def visit_type_application(self, tapp: TypeApplication) -> Type:
There are two different options here, depending on whether expr refers
to a type alias or directly to a generic class. In the first case we need
to use a dedicated function typeanal.expand_type_alias(). This
is due to some differences in how type arguments are applied and checked.
to use a dedicated function typeanal.instantiate_type_alias(). This
is due to slight differences in how type arguments are applied and checked.
"""
if isinstance(tapp.expr, RefExpr) and isinstance(tapp.expr.node, TypeAlias):
# Subscription of a (generic) alias in runtime context, expand the alias.
item = expand_type_alias(
item = instantiate_type_alias(
tapp.expr.node,
tapp.types,
self.chk.fail,
Expand Down
18 changes: 10 additions & 8 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
an_args = self.pack_paramspec_args(an_args)

disallow_any = self.options.disallow_any_generics and not self.is_typeshed_stub
res = expand_type_alias(
res = instantiate_type_alias(
node,
an_args,
self.fail,
Expand All @@ -408,7 +408,7 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
unexpanded_type=t,
disallow_any=disallow_any,
)
# The only case where expand_type_alias() can return an incorrect instance is
# The only case where instantiate_type_alias() can return an incorrect instance is
# when it is top-level instance, so no need to recurse.
if (
isinstance(res, Instance) # type: ignore[misc]
Expand Down Expand Up @@ -714,7 +714,7 @@ def analyze_type_with_type_info(
# The class has a Tuple[...] base class so it will be
# represented as a tuple type.
if info.special_alias:
return expand_type_alias(
return instantiate_type_alias(
info.special_alias,
# TODO: should we allow NamedTuples generic in ParamSpec?
self.anal_array(args),
Expand All @@ -730,7 +730,7 @@ def analyze_type_with_type_info(
# The class has a TypedDict[...] base class so it will be
# represented as a typeddict type.
if info.special_alias:
return expand_type_alias(
return instantiate_type_alias(
info.special_alias,
# TODO: should we allow TypedDicts generic in ParamSpec?
self.anal_array(args),
Expand Down Expand Up @@ -1713,7 +1713,7 @@ def fix_instance(
t.invalid = True


def expand_type_alias(
def instantiate_type_alias(
node: TypeAlias,
args: list[Type],
fail: MsgCallback,
Expand All @@ -1725,11 +1725,13 @@ def expand_type_alias(
disallow_any: bool = False,
use_standard_error: bool = False,
) -> Type:
"""Expand a (generic) type alias target following the rules outlined in TypeAlias docstring.
"""Create an instance of a (generic) type alias from alias node and type arguments.
We are following the rules outlined in TypeAlias docstring.
Here:
target: original target type
args: types to be substituted in place of type variables
node: type alias node (definition)
args: type arguments (types to be substituted in place of type variables
when expanding the alias)
fail: error reporter callback
no_args: whether original definition used a bare generic `A = List`
ctx: context where expansion happens
Expand Down

0 comments on commit bfc1a76

Please sign in to comment.