Skip to content

Allowing TypeVar to include any user defined Literal[<strings>] #8937

Answered by erictraut
coretl asked this question in Q&A
Discussion options

You must be logged in to vote

You're attempting to use a value-constrained TypeVar here. I generally recommend avoiding these if possible. They are poorly specified in the typing spec and have many unusual and surprising behaviors and limitations. This concept is unique to Python and doesn't appear in other programming languages that support generics. There's a good reason for this.

In your code sample, you're defining a TypeVar that includes a value constraint of str and LiteralString, but LiteralString is a subtype of str. All LiteralString values are already legal values of the type str, so that's an immediate red flag.

Another issue here is that you're attempting to pass the runtime value for a type expression (Li…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@coretl
Comment options

@erictraut
Comment options

@coretl
Comment options

Answer selected by coretl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants