Skip to content

Commit 5eae9a2

Browse files
committed
[ty] functional enum syntax
astral-sh/ty#876
1 parent 8d5d41c commit 5eae9a2

11 files changed

Lines changed: 493 additions & 27 deletions

File tree

crates/ty_python_semantic/resources/mdtest/dataclasses/dataclasses.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,16 +1936,16 @@ from enum import Enum
19361936

19371937
E = Enum("E", "A B C")
19381938

1939-
# TODO: should emit `invalid-dataclass`
1939+
# error: [invalid-dataclass] "Cannot use `dataclass()` on an enum class"
19401940
dataclass(E)
19411941

1942-
# TODO: should emit `invalid-dataclass`
1942+
# error: [invalid-dataclass] "Cannot use `dataclass()` on an enum class"
19431943
dataclass()(E)
19441944

1945-
# TODO: should emit `invalid-dataclass`
1945+
# error: [invalid-dataclass] "Cannot use `dataclass()` on an enum class"
19461946
dataclass(Enum("Inline1", "X Y"))
19471947

1948-
# TODO: should emit `invalid-dataclass`
1948+
# error: [invalid-dataclass] "Cannot use `dataclass()` on an enum class"
19491949
dataclass()(Enum("Inline2", "X Y"))
19501950
```
19511951

crates/ty_python_semantic/resources/mdtest/enums.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,35 @@ def _(x: EnumWithSubclassOfEnumMetaMetaclass):
12511251

12521252
## Function syntax
12531253

1254-
To do: <https://typing.python.org/en/latest/spec/enums.html#enum-definition>
1254+
```py
1255+
from enum import Enum
1256+
from ty_extensions import enum_members
1257+
1258+
Color = Enum("Color", "RED", "GREEN", "BLUE")
1259+
1260+
# revealed: tuple[Literal["RED"], Literal["GREEN"], Literal["BLUE"]]
1261+
reveal_type(enum_members(Color))
1262+
1263+
Color = Enum("Color", "RED GREEN BLUE")
1264+
1265+
# revealed: tuple[Literal["RED"], Literal["GREEN"], Literal["BLUE"]]
1266+
reveal_type(enum_members(Color))
1267+
1268+
Color = Enum("Color", "RED, GREEN, BLUE")
1269+
1270+
# revealed: tuple[Literal["RED"], Literal["GREEN"], Literal["BLUE"]]
1271+
reveal_type(enum_members(Color))
1272+
1273+
Color = Enum("Color", [("RED", 1), ("GREEN", 2), ("BLUE", 3)])
1274+
1275+
# TODO: This should be supported
1276+
reveal_type(enum_members(Color)) # revealed: Unknown
1277+
1278+
Color = Enum("Color", (("RED", 1), ("GREEN", 2), ("BLUE", 3)))
1279+
1280+
# TODO: This should be supported
1281+
reveal_type(enum_members(Color)) # revealed: Unknown
1282+
```
12551283

12561284
## Exhaustiveness checking
12571285

0 commit comments

Comments
 (0)