2

My code is basically the following:

def foo(param: Literal["a", "b"]) -> None:
    ...

i = input("A or B?").lower()
assert i in ["a","b"]
foo(i)

However mypy complains that it can't know that

Argument 1 to "foo" has incompatible type "str"; expected "Union[Literal['a'], Literal['b']]"

and I can't find documentation on how to convert this to a literal expression.

2
  • 1
    you can use typing.cast Commented May 18, 2021 at 10:36
  • @juanpa.arrivillaga Do you want to write this up as an answer or should I write it up and self answer?
    – Vringar
    Commented May 18, 2021 at 10:40

1 Answer 1

2

As @juanpa.arrivillaga mentioned in a comment, you can use typing.cast to cast the value as an element. Unfortunately mypy doesn't appear to do any checking on type.cast that the value actually fits the literal it is being cast to, but you can do that check for that yourself against typing.get_args.

a_or_b=Literal["a", "b"]
def foo(param: a_or_b) -> None:
    ...


i = input("A or B?").lower()
if not i in typing.get_args(a_or_b):
    raise ValueError()
else:
    i = typing.cast(i,a_or_b)
foo(i)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.