Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enum auto must not be derived from IntFlag #8286

Open
ghpqans opened this issue Jul 13, 2022 · 4 comments
Open

enum auto must not be derived from IntFlag #8286

ghpqans opened this issue Jul 13, 2022 · 4 comments

Comments

@ghpqans
Copy link

ghpqans commented Jul 13, 2022

When using auto enumeration within an class Example(enum.Enum): X = auto(), the invokation of auto() is actually constructing an instance of enum.auto, which just has a default constructor without any extra arguments.

However, when deriving class auto(IntFlag) in enum.pyi the stub actually has a constructor argument of type int, the value for the IntFlag.

This contradicts the definition in the original enum.py implementation.
As a matter of fact this leads to confusing type hints, e.g. enum class auto(IntFlag).

@srittau
Copy link
Collaborator

srittau commented Jul 13, 2022

I agree that this is confusing an error prone. In the last few years we tend to align stubs as closely to the implementation as possible. In this case, I believe the best approach is for type checkers would be to special case auto.

That said, as a stop-gap we could try not to derive auto from IntFlag and copy IntFlags field over, instead or alternatively to just override auto.__init__().

@srittau
Copy link
Collaborator

srittau commented Jul 13, 2022

Also cf. #1331 for PR where the current implementation was added.

@ghpqans
Copy link
Author

ghpqans commented Jul 13, 2022

What are you using the IntFlags's fields for with that original implementation for auto:

_auto_null = object()
class auto:
   """
   Instances are replaced with an appropriate value in Enum class suites.
   """
   value = _auto_null

The derived classes from EnumMeta just use instances of class auto as a reference wrapper. There are no method calls or other attributes to be found.

@zmievsa
Copy link
Contributor

zmievsa commented Jul 19, 2022

+1
When working with fastapi, for example, we tend to use a lot of string enums, and we would love to have native support for them and any other types that could possibly use auto.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants