Aw: Re: mypy question
Karsten Hilbert
Karsten.Hilbert at gmx.net
Sat Dec 30 06:23:53 EST 2023
Hi Greg,
> dict[str, str] is not a subtype of dict[str, str | something_else]
> because you can assign a value of type something_else to the latter
> but not the former.
I understand what you are saying but I do not yet understand why this
applies to my situation.
I don't have Python at hand currently, so I'll write untested pseudocode:
def print_greeting(greeting:int|str):
print(greeting)
print_greeting('hello')
The above snippet should be equivalent to my more complicated code over
which mypy complains to the equivalent of
"input" is of type "str"
but expected type "Union[str,int]
I do understand that "str" is formally more narrow than "Union [str,int]" and
the type system has every right to not consider them equivalent.
However, this seems like a very common use case: "allow passing in either str or int
and have type checking pass either input as valid" -- yet mypy doesn't seem
to share that idea.
Or else there's something I haven't wrapped my head around yet. But what ?
Karsten
More information about the Python-list
mailing list