Inconsistent "Incompatible return type" error when List[int]
is returned for List[Union[str, int]]
when layered up with Tuple/Union
#17868
Labels
Bug Report
When layering up some Union/Tuple checks, at some point the type checking suddenly gets a lot more strict and starts failing, as if it's unable to fully parse the type checks anymore.
It's possibly related to this, but it doesn't explain why the behaviour changes only once it's wrapped under other things: #3351
To Reproduce
Here's a minimal reproducible example that shows how it works fine until it suddenly doesn't.
https://mypy-play.net/?mypy=latest&python=3.12&gist=9477b2d4dab01919bef1aa6d2911f648
Expected Behavior
It passes the test.
Actual Behavior
In the code above. It complains that
List[int]
is not a validList[Union[int | str]]
. According to the issue linked above, this may not be wrong, but the behaviour is inconsistent as to when it flags it as wrong.Your Environment
mypy.ini
(and other config files): I just runpy -m mypy
without any config files.Edit
This is nothing to do with the bug report, but in case anyone finds this issue with a similar use case, this is the fully working solution I've ended up with:
The text was updated successfully, but these errors were encountered: