Skip to content
Open
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
13 changes: 9 additions & 4 deletions conformance/results/basilisk/enums_expansion.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
conformant = "Pass"
conformance_automated = "Pass"
conformant = "Partial"
notes = """
Does not accept an enum-typed value where a complete union of all literal members is declared, although it treats the two as equivalent for `assert_type`.
"""
conformance_automated = "Fail"
errors_diff = """
Line 85: Unexpected errors ['enums_expansion.py:85:5: error: Type mismatch: `x` is annotated `Literal[Answer.Yes, Answer.No]` (answer.yes | answer.no) but assigned answer [assignment_compatibility]']
"""
output = """
enums_expansion.py:25:9: error: Redundant `assert_type` with `Literal[Color.GREEN]` on enum-typed parameter [enums_expansion]
enums_expansion.py:53:9: error: Redundant `assert_type` with `Literal[CustomFlags.FLAG3]` on enum-typed parameter [enums_expansion]
enums_expansion.py:28:9: error: Redundant `assert_type` with `Literal[Color.GREEN]` on enum-typed parameter [enums_expansion]
enums_expansion.py:56:9: error: Redundant `assert_type` with `Literal[CustomFlags.FLAG3]` on enum-typed parameter [enums_expansion]
enums_expansion.py:85:5: error: Type mismatch: `x` is annotated `Literal[Answer.Yes, Answer.No]` (answer.yes | answer.no) but assigned answer [assignment_compatibility]
"""
6 changes: 3 additions & 3 deletions conformance/results/mypy/enums_expansion.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Improperly applies narrowing to `Flag` subclass.
"""
conformance_automated = "Fail"
errors_diff = """
Line 53: Expected 1 errors
Line 52: Unexpected errors ['enums_expansion.py:52: error: Expression is of type "Literal[CustomFlags.FLAG3]", not "CustomFlags" [assert-type]']
Line 56: Expected 1 errors
Line 55: Unexpected errors ['enums_expansion.py:55: error: Expression is of type "Literal[CustomFlags.FLAG3]", not "CustomFlags" [assert-type]']
"""
output = """
enums_expansion.py:52: error: Expression is of type "Literal[CustomFlags.FLAG3]", not "CustomFlags" [assert-type]
enums_expansion.py:55: error: Expression is of type "Literal[CustomFlags.FLAG3]", not "CustomFlags" [assert-type]
"""
2 changes: 1 addition & 1 deletion conformance/results/pycroscope/enums_expansion.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ conformance_automated = "Pass"
errors_diff = """
"""
output = """
./enums_expansion.py:53:20: enums_expansion.CustomFlags is not equivalent to Literal[<CustomFlags.FLAG3: 4>]
./enums_expansion.py:56:20: enums_expansion.CustomFlags is not equivalent to Literal[<CustomFlags.FLAG3: 4>]
"""
2 changes: 1 addition & 1 deletion conformance/results/pyrefly/enums_expansion.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ conformance_automated = "Pass"
errors_diff = """
"""
output = """
ERROR enums_expansion.py:53:20-51: assert_type(CustomFlags, Literal[CustomFlags.FLAG3]) failed [assert-type]
ERROR enums_expansion.py:56:20-51: assert_type(CustomFlags, Literal[CustomFlags.FLAG3]) failed [assert-type]
"""
16 changes: 13 additions & 3 deletions conformance/results/pyright/enums_expansion.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
conformant = "Pass"
conformance_automated = "Pass"
conformant = "Partial"
notes = """
Does not treat a complete union of all literal members as equivalent to the enum type.
"""
conformance_automated = "Fail"
errors_diff = """
Line 85: Unexpected errors ['enums_expansion.py:85:41 - error: Type "Answer" is not assignable to declared type "Literal[Answer.Yes, Answer.No]"']
Line 86: Unexpected errors ['enums_expansion.py:86:17 - error: "assert_type" mismatch: expected "Literal[Answer.Yes, Answer.No]" but received "Answer" (reportAssertTypeFailure)']
"""
output = """
enums_expansion.py:53:21 - error: "assert_type" mismatch: expected "Literal[CustomFlags.FLAG3]" but received "CustomFlags" (reportAssertTypeFailure)
enums_expansion.py:56:21 - error: "assert_type" mismatch: expected "Literal[CustomFlags.FLAG3]" but received "CustomFlags" (reportAssertTypeFailure)
enums_expansion.py:85:41 - error: Type "Answer" is not assignable to declared type "Literal[Answer.Yes, Answer.No]"
  Type "Answer" is not assignable to type "Literal[Answer.Yes, Answer.No]"
    "Answer" is not assignable to type "Literal[Answer.Yes]"
    "Answer" is not assignable to type "Literal[Answer.No]" (reportAssignmentType)
enums_expansion.py:86:17 - error: "assert_type" mismatch: expected "Literal[Answer.Yes, Answer.No]" but received "Answer" (reportAssertTypeFailure)
"""
33 changes: 24 additions & 9 deletions conformance/results/results.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions conformance/results/ty/enums_expansion.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
conformance_automated = "Fail"
conformant = "Partial"
notes = """Does not support `enum.Flag`."""
notes = """
Does not support `enum.Flag`."""
errors_diff = """
Line 53: Expected 1 errors
Line 52: Unexpected errors ['enums_expansion.py:52:9: error[type-assertion-failure] Type `Literal[CustomFlags.FLAG3]` does not match asserted type `CustomFlags`']
Line 56: Expected 1 errors
Line 55: Unexpected errors ['enums_expansion.py:55:9: error[type-assertion-failure] Type `Literal[CustomFlags.FLAG3]` does not match asserted type `CustomFlags`']
"""
output = """
enums_expansion.py:52:9: error[type-assertion-failure] Type `Literal[CustomFlags.FLAG3]` does not match asserted type `CustomFlags`
enums_expansion.py:55:9: error[type-assertion-failure] Type `Literal[CustomFlags.FLAG3]` does not match asserted type `CustomFlags`
"""
14 changes: 11 additions & 3 deletions conformance/results/zuban/enums_expansion.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
conformance_automated = "Pass"
conformant = "Partial"
notes = """
Does not treat a complete union of all literal members as equivalent to the enum type.
"""
conformance_automated = "Fail"
errors_diff = """
Line 85: Unexpected errors ['enums_expansion.py:85: error: Incompatible types in assignment (expression has type "Answer", variable has type "Literal[Answer.Yes, Answer.No]") [assignment]']
Line 86: Unexpected errors ['enums_expansion.py:86: error: Expression is of type "Answer", not "Literal[Answer.Yes, Answer.No]" [misc]']
"""
output = """
enums_expansion.py:35: error: Statement is unreachable [unreachable]
enums_expansion.py:53: error: Expression is of type "CustomFlags", not "Literal[CustomFlags.FLAG3]" [misc]
enums_expansion.py:38: error: Statement is unreachable [unreachable]
enums_expansion.py:56: error: Expression is of type "CustomFlags", not "Literal[CustomFlags.FLAG3]" [misc]
enums_expansion.py:85: error: Incompatible types in assignment (expression has type "Answer", variable has type "Literal[Answer.Yes, Answer.No]") [assignment]
enums_expansion.py:86: error: Expression is of type "Answer", not "Literal[Answer.Yes, Answer.No]" [misc]
"""
18 changes: 13 additions & 5 deletions conformance/tests/enums_expansion.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
from typing import Literal, Never, assert_type

# > From the perspective of the type system, most enum classes are equivalent
# > to the union of the literal members within that enum. Type checkers may
# > therefore expand an enum type
# > to the union of the literal members within that enum. [...] Because of the
# > equivalency between an enum class and the union of literal members within
# > that enum, the two types may be used interchangeably. Type checkers may
# > therefore expand an enum type (that does not derive from enum.Flag) into a
# > union of literal values during type narrowing and exhaustion detection


class Color(Enum):
Expand All @@ -35,8 +38,8 @@ def print_color2(c: Color):
assert_type(c, Never) # E?


# > This rule does not apply to classes that derive from enum. Flag because
# > these enums allow flags to be combined in arbitrary ways.
# > (This rule does not apply to classes that derive from enum.Flag because
# > these enums allow flags to be combined in arbitrary ways.)


class CustomFlags(Flag):
Expand Down Expand Up @@ -64,7 +67,7 @@ def test2(f: CustomFlags):


# > A type checker should treat a complete union of all literal members as
# > compatible with the enum type.
# > equivalent to the enum type.


class Answer(Enum):
Expand All @@ -76,3 +79,8 @@ def test3(val: object) -> list[Answer]:
assert val is Answer.Yes or val is Answer.No
x = [val]
return x


def test4(a: Answer) -> None:
x: Literal[Answer.Yes, Answer.No] = a
assert_type(a, Literal[Answer.Yes, Answer.No])