We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Let's say we have a function that takes multiple Literal arguments.
def takes_a_literal(foo: Literal["a", "b"]) -> None: print(foo)
And we call that function, from a function that takes a string.
def does_something(foo: str) -> None: takes_a_literal(foo)
Obviously, this will fail because foo can be anything. But what if we want to be assertive?
foo
assert foo in ["a", "b], reveal_type(foo) is still str.
assert foo in ["a", "b]
reveal_type(foo)
str
I can't even cast it: cast(Literal["a", "b"], foo) -- reveal_type(foo) still says str.
cast(Literal["a", "b"], foo)
The only thing I've found that works is this:
literal_foo: Literal["a", "b"] if foo == "a": literal_foo = "a" elif foo == "b": literal_foo = "b"
Then passing literal_foo. This gets quite wordy with more than a couple of Literals.
literal_foo
Is there another way to do this?
The text was updated successfully, but these errors were encountered:
This is a duplicate of #10977.
Sorry, something went wrong.
No branches or pull requests
Let's say we have a function that takes multiple Literal arguments.
And we call that function, from a function that takes a string.
Obviously, this will fail because
foo
can be anything. But what if we want to be assertive?assert foo in ["a", "b]
,reveal_type(foo)
is stillstr
.I can't even cast it:
cast(Literal["a", "b"], foo)
--reveal_type(foo)
still saysstr
.The only thing I've found that works is this:
Then passing
literal_foo
. This gets quite wordy with more than a couple of Literals.Is there another way to do this?
The text was updated successfully, but these errors were encountered: