typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

enum auto must not be derived from IntFlag

Open ghpqans opened this issue 2 years ago • 4 comments

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).

ghpqans avatar Jul 13 '22 08:07 ghpqans

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 avatar Jul 13 '22 09:07 srittau

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

srittau avatar Jul 13 '22 09:07 srittau

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.

ghpqans avatar Jul 13 '22 10:07 ghpqans

+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.

zmievsa avatar Jul 19 '22 17:07 zmievsa