Skip to content

Commit e6692ca

Browse files
authored
Support type aliases in builtins (#5338)
This fixes a recently discovered internal crash.
1 parent 0d6690f commit e6692ca

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

mypy/checker.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
UnicodeExpr, OpExpr, UnaryExpr, LambdaExpr, TempNode, SymbolTableNode,
2020
Context, Decorator, PrintStmt, BreakStmt, PassStmt, ContinueStmt,
2121
ComparisonExpr, StarExpr, EllipsisExpr, RefExpr, PromoteExpr,
22-
Import, ImportFrom, ImportAll, ImportBase,
22+
Import, ImportFrom, ImportAll, ImportBase, TypeAlias,
2323
ARG_POS, ARG_STAR, LITERAL_TYPE, MDEF, GDEF,
2424
CONTRAVARIANT, COVARIANT, INVARIANT,
2525
)
@@ -3171,6 +3171,9 @@ def named_type(self, name: str) -> Instance:
31713171
# Assume that the name refers to a type.
31723172
sym = self.lookup_qualified(name)
31733173
node = sym.node
3174+
if isinstance(node, TypeAlias):
3175+
assert isinstance(node.target, Instance)
3176+
node = node.target.type
31743177
assert isinstance(node, TypeInfo)
31753178
any_type = AnyType(TypeOfAny.from_omitted_generics)
31763179
return Instance(node, [any_type] * len(node.defn.type_vars))

mypy/semanal.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,10 @@ def named_type_or_none(self, qualified_name: str,
13091309
if not sym:
13101310
return None
13111311
node = sym.node
1312-
assert isinstance(node, TypeInfo)
1312+
if isinstance(node, TypeAlias):
1313+
assert isinstance(node.target, Instance)
1314+
node = node.target.type
1315+
assert isinstance(node, TypeInfo), node
13131316
if args is not None:
13141317
# TODO: assert len(args) == len(node.defn.type_vars)
13151318
return Instance(node, args)

test-data/unit/python2eval.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,11 @@ def f(): # no annotation
404404
path = 'test'
405405
path = os.path.join(f(), 'test.py')
406406
[out]
407+
408+
[case testBytesWorkInPython2WithFullStubs_python2]
409+
MYPY = False
410+
if MYPY:
411+
import lib
412+
[file lib.pyi]
413+
x = b'abc'
414+
[out]

0 commit comments

Comments
 (0)