Skip to content

Support type alias as Literal #8014

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 5 commits into from
Nov 26, 2019
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/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3621,7 +3621,6 @@ def visit_member_expr(self, expr: MemberExpr) -> None:
type_info = self.type
elif isinstance(base.node, TypeAlias) and base.node.no_args:
assert isinstance(base.node.target, ProperType)
# TODO: support chained aliases.
if isinstance(base.node.target, Instance):
type_info = base.node.target.type

Expand Down Expand Up @@ -3969,6 +3968,10 @@ def lookup_qualified(self, name: str, ctx: Context,
namespace = node.fullname
elif isinstance(node, PlaceholderNode):
return sym
elif isinstance(node, TypeAlias) and node.no_args:
assert isinstance(node.target, ProperType)
if isinstance(node.target, Instance):
nextsym = node.target.type.get(part)
else:
if isinstance(node, Var):
typ = get_proper_type(node.type)
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-literal.test
Original file line number Diff line number Diff line change
Expand Up @@ -3149,3 +3149,17 @@ err_code = -TWO
if bool():
err_code = -THREE
[builtins fixtures/float.pyi]

[case testAliasForEnumTypeAsLiteral]
from typing_extensions import Literal
from enum import Enum

class Foo(Enum):
A = 1

F = Foo

x: Literal[Foo.A]
y: Literal[F.A]
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
21 changes: 21 additions & 0 deletions test-data/unit/check-type-aliases.test
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,24 @@ except E as e:
reveal_type(e) # N: Revealed type is '__main__.E'
[builtins fixtures/exception.pyi]
[out]

[case testNestedClassOnAliasAsType]
class Out:
class In:
class Inner:
pass

O = Out
I = Out.In
OI = O.In
A = Out
B = A

w: O.In
x: I.Inner
y: OI.Inner
z: B.In
reveal_type(w) # N: Revealed type is '__main__.Out.In'
reveal_type(x) # N: Revealed type is '__main__.Out.In.Inner'
reveal_type(y) # N: Revealed type is '__main__.Out.In.Inner'
reveal_type(z) # N: Revealed type is '__main__.Out.In'