8000 Start using TypeAliasType in the semantic analyzer by ilevkivskyi · Pull Request #7923 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Start using TypeAliasType in the semantic analyzer #7923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix some issues
  • Loading branch information
ilevkivskyi committed Nov 10, 2019
commit ad8f4b4ed40a4b7f525928be589b12465177cbe0
2 changes: 1 addition & 1 deletion mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ def instance_alias_type(alias: TypeAlias,
target = get_proper_type(alias.target) # type: Type
assert isinstance(get_proper_type(target),
Instance), "Must be called only with aliases to classes"
target = set_any_tvars(alias, alias.line, alias.column)
target = get_proper_type(set_any_tvars(alias, alias.line, alias.column))
assert isinstance(target, Instance) # type: ignore[misc]
tp = type_object_type(target.type, builtin_type)
return expand_type_by_instance(tp, target)
Expand Down
14 changes: 12 additions & 2 deletions mypy/semanal_typeargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
operations, including subtype checks.
"""

from typing import List, Optional
from typing import List, Optional, Set

from mypy.nodes import TypeInfo, Context, MypyFile, FuncItem, ClassDef, Block
from mypy.types import Type, Instance, TypeVarType, AnyType, get_proper_types
from mypy.types import (
Type, Instance, TypeVarType, AnyType, get_proper_types, TypeAliasType, get_proper_type
)
from mypy.mixedtraverser import MixedTraverserVisitor
from mypy.subtypes import is_subtype
from mypy.sametypes import is_same_type
Expand All @@ -27,6 +29,7 @@ def __init__(self, errors: Errors, options: Options, is_typeshed_file: bool) ->
self.scope = Scope()
# Should we also analyze function definitions, or only module top-levels?
self.recurse_into_functions = True
self.seen_aliases = set() # type: Set[TypeAliasType]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment (also explain why we need this).


def visit_mypy_file(self, o: MypyFile) -> None:
self.errors.set_file(o.path, o.fullname(), scope=self.scope)
Expand All @@ -48,6 +51,13 @@ def visit_block(self, o: Block) -> None:
if not o.is_unreachable:
super().visit_block(o)

def visit_type_alias_type(self, t: TypeAliasType) -> None:
super().visit_type_alias_type(t)
if t in self.seen_aliases:
return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment about this sp 8000 ecial case.

self.seen_aliases.add(t)
get_proper_type(t).accept(self)

def visit_instance(self, t: Instance) -> None:
# Type argument counts were checked in the main semantic analyzer pass. We assume
# that the counts are correct here.
Expand Down
11 changes: 7 additions & 4 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,11 +1007,14 @@ def expand_type_alias(node: TypeAlias, args: List[Type],
fail('Bad number of arguments for type alias, expected: %s, given: %s'
% (exp_len, act_len), ctx)
return set_any_tvars(node, ctx.line, ctx.column, from_error=True)
typ = TypeAliasType(node, args, ctx.line, ctx.column) # type: Type
typ = TypeAliasType(node, args, ctx.line, ctx.column)
assert typ.alias is not None
# HACK: Implement FlexibleAlias[T, typ] by expanding it to typ here.
if (isinstance(typ, Instance) # type: ignore
and typ.type.fullname() == 'mypy_extensions.FlexibleAlias'):
typ = typ.args[-1]
if (isinstance(typ.alias.target, Instance) # type: ignore
and typ.alias.target.type.fullname() == 'mypy_extensions.FlexibleAlias'):
exp = get_proper_type(typ)
assert isinstance(exp, Instance)
return exp.args[-1]
return typ


Expand Down
0