Created
August 2, 2022 13:01
-
-
Save woile/6686e53daa7863557c45c4c1b783af6e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Literal, TypedDict | |
from enum import Enum | |
class Conn(str, Enum): | |
TCP = "TCP" | |
UDP = "UDP" | |
class TcpSettings(TypedDict): | |
conn: Literal[Conn.TCP] | |
foo: bool | |
t = TcpSettings(conn="TCP", foo=True) | |
# Incompatible types (expression has type "Literal['TCP']", TypedDict item "conn" has type "Literal[Conn.TCP]")mypy(error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Possible solutions.
Use the enum for declaration
Use a str literal in the class
But is there a way to have both? An enum + letting users who use
TcpSettings
to avoid the enum by providing a str literal?