Commit b642756
[ty] Add support for functional
## Summary
astral-sh/ty#876
https://typing.python.org/en/latest/spec/enums.html#enum-definition
this pr implements the functional syntax for creating enums:
`Enum('Color2', 'RED, GREEN, BLUE')`
it mostly copies the `namedtuple` implementation
it supports the `start=` and `type=` kwargs for `Enum` as well
([docs](https://docs.python.org/3/library/enum.html#enum.EnumType.__call__))
it also supports `Flag` and `IntFlag`
the `ddtrace` diffs look to be correct - `IntEnum` call is now properly
recognized
###
for non-string-literal `name` arguments, it falls back to just returning
`type[Enum]` - this came up by way of a psycopg regression on this
[line](https://github.com/psycopg/psycopg/blob/eb87f0eb58ee05b7202b7d8835b925b084d32b5c/psycopg/psycopg/types/enum.py#L173)
- wasn't sure if this was better or worse than returning a
`DynamicEnumLiteral` with an unknown name and members.
this now looks correct for psycopg: `+
psycopg/psycopg/types/enum.py:173:12 [error] [invalid-return-type]
Return type does not match returned value: expected `Enum`, found
`type[Enum]``
however, it appears mypy does not correctly support the functional API
(i am wondering if this is why the psycopg typing is the way it is), or
maybe when just returning the result of the `Enum` call (without
assigning it to a name), it always assumes its looking up a member:
```python
from enum import Enum, EnumType
from typing import reveal_type
def make_color_1() -> EnumType:
return Enum("Color1", names="RED GREEN")
def make_color_2() -> EnumType:
return Enum("Color2", names=("RED", "GREEN"))
reveal_type(make_color_1())
reveal_type(make_color_2())
```
```bash
# mypy output
main.py:6: error: Incompatible return value type (got "Enum", expected "EnumMeta") [return-value]
main.py:10: error: Incompatible return value type (got "Enum", expected "EnumMeta") [return-value]
main.py:13: note: Revealed type is "enum.EnumMeta"
main.py:14: note: Revealed type is "enum.EnumMeta"
Found 2 errors in 1 file (checked 1 source file)
# runtime output
Runtime type is 'EnumType'
Runtime type is 'EnumType'
```
## Test Plan
mdtests
---------
Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>Enum(...) syntax (#23602)1 parent 9e3bd44 commit b642756
16 files changed
Lines changed: 2038 additions & 110 deletions
File tree
- crates/ty_python_semantic
- resources/mdtest
- annotations
- dataclasses
- src
- types
- class
- infer
- builder
Lines changed: 1 addition & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | | - | |
| 42 | + | |
44 | 43 | | |
45 | 44 | | |
46 | 45 | | |
| |||
Lines changed: 4 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1936 | 1936 | | |
1937 | 1937 | | |
1938 | 1938 | | |
1939 | | - | |
| 1939 | + | |
1940 | 1940 | | |
1941 | 1941 | | |
1942 | | - | |
| 1942 | + | |
1943 | 1943 | | |
1944 | 1944 | | |
1945 | | - | |
| 1945 | + | |
1946 | 1946 | | |
1947 | 1947 | | |
1948 | | - | |
| 1948 | + | |
1949 | 1949 | | |
1950 | 1950 | | |
1951 | 1951 | | |
| |||
0 commit comments