
This site's content is licensed under aĬommons Attribution 4.0 International License. Ignore” comment on each call to a multi-value enum type.Ĭopyright © 2020–2023 John T. To be a way around this other than just slapping a “ # type: Unfortunately, at time of writing, there does not seem Three arguments, it thinks there are two arguments missing from theĭirection() call. On the last line, having already seen a Direction._new_ that takes The second mypy problem is issue #10573: mypy is not aware of the EnumĬlass’s trickery with its constructor, so when it sees Direction('north')

_value_ = value return obj def _init_ ( self, value : str, x : int, y : int ) -> None : self. So: class Direction ( Enum ): NORTH = ( "north", 0, 1 ) # NORTH_WEST = ( "north-west", - 1, 1 ) def _new_ ( cls, value : str, x : int, y : int ) -> Direction : obj = object. … or else move the extra-value assignments to an _init_ method, like Them in the main body of the class, like so: class Direction ( Enum ): NORTH = ( "north", 0, 1 ) # NORTH_WEST = ( "north-west", - 1, 1 ) # Add these two lines: x : int y : int def _new_ ( cls, value : str, x : int, y : int ) -> Direction. To make mypy aware of the extra-value attributes: either add annotations for y = y return obj # Check that `value` and other attributes are what they're supposed to be: for d in Direction : print ( f " ") being seen as erroneous. So: from enum import Enum class Direction ( Enum ): NORTH = ( "north", 0, 1 ) NORTH_EAST = ( "north-east", 1, 1 ) EAST = ( "east", 1, 0 ) SOUTH_EAST = ( "south-east", 1, - 1 ) SOUTH = ( "south", 0, - 1 ) SOUTH_WEST = ( "south-west", - 1, - 1 ) WEST = ( "west", - 1, 0 ) NORTH_WEST = ( "north-west", - 1, 1 ) def _new_ ( cls, value, x, y ): obj = object. Of the tuple (which are passed as arguments to _new_) appropriately, like The solution is to define each enum member with a tuple of the value andĮxtra attributes, and then write a _new_ method that assigns the elements Have x and y attributes giving the deltas to add to a coordinate’sĬomponents in order to move a step in the respective direction. Used in some data source you have to parse and each member should additionally

Ordinal directions where the members’ values are human-readable names There a way to define enum members with a combination of a “main” value and aįor a specific example, let’s say you want to define an enum for cardinal and Other attributes, whereas you want or need value to be something else.
#Python enum code
Similar to what you want, but on closer inspection, you see it won’t do - theĮxample code produces an enum whose members’ values are tuples of the Have one or more additional constant attributes alongside value, but theseĪttributes can’t be easily derived from value (and thus don’t make for The Python docs provide an example involving planets Enum default from Addon preferences Coding Python Support chafouin (chafouin) May 5, 2018, 12:11pm 1. Say you’re creating an enum type in Python and you want each enum member to
