You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Bug Report
When the function parameter is annotated to have some combination of multiple types with |, mypy validation warns
if a variable is passed to the called function.
Like many mutable generic containers, dict is invariant with respect to its type parameters. That means that even though int is a subtype of int | str, dict[int, V] is not a subtype of dict[int | str, V]. To see why that would be unsafe, consider this example:
The reason the second example works is because bidirectional inference allows mypy to infer a better type for the dict literal by peeking at the type of the parameter that its being passed to. To make the first example type check, you can try adding an explicit type annotation for data or modifying the signature of show to accept a type that's compatible with dict[int, dict[str, str]].
Bug Report
When the function parameter is annotated to have some combination of multiple types with
|
, mypy validation warnsif a variable is passed to the called function.
To Reproduce
Gist url: https://mypy-play.net/?mypy=latest&python=3.12&gist=2e3c8b121347ac9547e8e84eba7f1017
Expected Behavior
There should be no warning as it's a valid code
Actual Behavior
Your Environment
mypy.ini
(and other config files): NoneThe text was updated successfully, but these errors were encountered: