El lun, 22 feb 2021 a las 13:24, Eric Traut (<
eric@traut.com>) escribió:
Thanks, but I'm not looking for a workaround. I'm looking for how this is intended to work so typeshed stubs and type checkers can handle this consistently.
As I said above, I think the logical way to annotate an enum in a type stub would be:
```python
class Color(Enum):
RED = ...
YELLOW = ...
GREEN = ...
components: Tuple[float, float, float]
html_encoding: str
```
In other words, variables that use assignments are considered enumeration members, and variables that use a simple type annotation are instance variables. But that doesn't appear to match the assumptions in typeshed stubs, which use variables with type annotations to indicate enumeration members.
I'm interested in hearing from maintainers of typeshed and the other major type checkers (mypy, pyre, pytype).
I feel like this is an area that wasn't specified in PEP 484 or other PEPs, so mypy ended up doing whatever made sense to implement, and typeshed went with whatever worked for mypy. It would be good to come up with a way to specify enums in stubs that can cover all use cases and work with all type checkers.
The syntax you propose unfortunately doesn't provide a way for the type checker to figure out the underlying type of the enum (i.e., the type of Color.RED.value; usually it's going to be int, but sometimes str). It's pretty common for users to need this.
--
Eric Traut
Contributor to Pyright & Pylance
Microsoft Corp.
_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: jelle.zijlstra@gmail.com