Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14069

typing Protocol valid for Enumerator member

$
0
0

What is the right typing to accept an Enumerator Type which should match a Protocol ?I mean the Enumerator should have members defined inside a Protocol

Here is a simplified code exemple.

from typing import Protocol from enum import Enum class MyProto(Protocol):   item1: int    item2: int def check_item1( e: MyProto, value:int):    return e.item1 == value  class MyEnum(int, Enum):   item1 = 0    item2 = 1    other_item_not_in_protocol = 2 check_item1( MyEnum,  0 ) #<-- pyright complains about everything :

Pyright complains

# Argument of type "type[MyEnum]" cannot be assigned to parameter "e" of type "MyProto" #in function "check_item1"#     "item1" is invariant because it is mutable#     "item1" is an incompatible type#     "item1" must be defined as a ClassVar to be compatible with protocol#       "Literal[MyEnum.item1]" is incompatible with "int"#     "item2" is invariant because it is mutable#     "item2" is an incompatible type#     "item2" must be defined as a ClassVar to be compatible with protocol#       "Literal[MyEnum.item2]" is incompatible with "int" [reportArgumentType]

Mypy also complains:

# error: Argument 1 to "check_item1" has incompatible type "type[MyEnum]"; expected # "MyProto"  [arg-type]# Found 1 error in 1 file (checked 1 source file)

Viewing all articles
Browse latest Browse all 14069

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>