Skip to content

Commit

Permalink
Allow enum members in Literals.
Browse files Browse the repository at this point in the history
We still allow unsolvables, because enum members will continue to show up as those until the overlay is enabled.

PiperOrigin-RevId: 400070986
  • Loading branch information
Solumin authored and rchen152 committed Oct 4, 2021
1 parent e6f5251 commit 89d9e32
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pytype/overlays/typing_overlay.py
Expand Up @@ -687,7 +687,7 @@ def _build_value(self, node, inner, ellipses):
values = []
errors = []
for i, param in enumerate(inner):
# TODO(b/173742489): Once pytype has proper support for enums, we should
# TODO(b/173742489): Once the enum overlay is enabled, we should
# stop allowing unsolvable and handle enums here.
if (param == self.vm.convert.none or
isinstance(param, abstract.LiteralClass) or
Expand All @@ -696,6 +696,8 @@ def _build_value(self, node, inner, ellipses):
elif (isinstance(param, abstract.ConcreteValue) and
isinstance(param.pyval, (int, str, bytes))):
value = abstract.LiteralClass(param, self.vm)
elif isinstance(param, abstract.Instance) and param.cls.is_enum:
value = abstract.LiteralClass(param, self.vm)
else:
if i in ellipses:
invalid_param = "..."
Expand Down
9 changes: 9 additions & 0 deletions pytype/tests/test_typing2.py
Expand Up @@ -900,6 +900,15 @@ def f(x: Literal["x", "y"]):
f(x)
""")

def test_enum(self):
# Requires the enum overlay
self.Check("""
import enum
from typing import Literal
class M(enum.Enum):
A = 1
x: Literal[M.A]
""")

if __name__ == "__main__":
test_base.main()

0 comments on commit 89d9e32

Please sign in to comment.