Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type annotations for Enum attribute

I have this piece of code:

import enum   class Color(enum.Enum):     RED = '1'     BLUE = '2'     GREEN = '3'   def get_color_return_something(some_color):     pass 

How do I properly add type annotations to the some_color variable in this function, if I suppose that I'll receive an enum attribute from the Color enum (for example: Color.RED)?

like image 513
Yuval Pruss Avatar asked Oct 03 '18 10:10

Yuval Pruss


People also ask

Which datatype can be used with enum?

The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type.

What is an enum attribute?

Definition. An Enumeration (or enum ) is a data type that includes a set of named values called elements or members. The enumerator names are usually identifiers that behave as constants in the language.

What is enum data type in Python?

Enum is a class in python for creating enumerations, which are a set of symbolic names (members) bound to unique, constant values. The members of an enumeration can be compared by these symbolic anmes, and the enumeration itself can be iterated over.

What is enum data type in JavaScript?

Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript. Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums.


1 Answers

Type hinting the Color class should work:

def get_color_return_something(some_color: Color):     print(some_color.value) 
like image 139
ibarrond Avatar answered Oct 02 '22 21:10

ibarrond