Skip to content

Support type aliases in builtins #5338

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 1 commit into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
UnicodeExpr, OpExpr, UnaryExpr, LambdaExpr, TempNode, SymbolTableNode,
Context, Decorator, PrintStmt, BreakStmt, PassStmt, ContinueStmt,
ComparisonExpr, StarExpr, EllipsisExpr, RefExpr, PromoteExpr,
Import, ImportFrom, ImportAll, ImportBase,
Import, ImportFrom, ImportAll, ImportBase, TypeAlias,
ARG_POS, ARG_STAR, LITERAL_TYPE, MDEF, GDEF,
CONTRAVARIANT, COVARIANT, INVARIANT,
)
Expand Down Expand Up @@ -3171,6 +3171,9 @@ def named_type(self, name: str) -> Instance:
# Assume that the name refers to a type.
sym = self.lookup_qualified(name)
node = sym.node
if isinstance(node, TypeAlias):
assert isinstance(node.target, Instance)
Copy link
Collaborator

Choose a reason for hiding this comment

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

It can't ever be a chain of TypeAliases?

Copy link
Member Author

Choose a reason for hiding this comment

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

It can't, chained aliases are collapsed on sight. An alias can target other types (non-Instance) but this function is supposed to be used only with built-in types, that are all instances.

node = node.target.type
assert isinstance(node, TypeInfo)
any_type = AnyType(TypeOfAny.from_omitted_generics)
return Instance(node, [any_type] * len(node.defn.type_vars))
Expand Down
5 changes: 4 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,10 @@ def named_type_or_none(self, qualified_name: str,
if not sym:
return None
node = sym.node
assert isinstance(node, TypeInfo)
if isinstance(node, TypeAlias):
assert isinstance(node.target, Instance)
node = node.target.type
assert isinstance(node, TypeInfo), node
if args is not None:
# TODO: assert len(args) == len(node.defn.type_vars)
return Instance(node, args)
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/python2eval.test
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,11 @@ def f(): # no annotation
path = 'test'
path = os.path.join(f(), 'test.py')
[out]

[case testBytesWorkInPython2WithFullStubs_python2]
MYPY = False
if MYPY:
import lib
[file lib.pyi]
x = b'abc'
[out]