Accessing property through a Union doesn't work
Bug Report
Accessing a property typed to the object we access , e.g. a property defining __get__(self, obj: B, ...) through a Union[A,B] complains that the argument to __get__ has union type, despite being defined in B.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=e7d68cb2fa9867b0c21c66f7b33637e5
Expected Behavior
No errors
Actual Behavior
main.py:35: error: Argument 1 to "__get__" of "SingleValueProperty" has incompatible type "Union[A, B]"; expected "B" [arg-type]
main.py:39: error: Argument 1 to "__get__" of "SingleValueProperty" has incompatible type "Union[A, B]"; expected "B" [arg-type]
Found 2 errors in 1 file (checked 1 source file)
Your Environment
The mypy playground.
- Mypy version used: 0.991, 0.971
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.11
Technically I'd want A and B to have a parent type with the properties in it, but that also doesn't work, because despite SingleValueProperty being a read-write/str property, I can't declare prop: str in the parent class, hence the union at the moment.
The parent base class turns out to be irrelevant, but the error doesn't change.
This is still an issue in 1.7.1
This is still an issue in 1.8.0
I've reduced the example further to avoid the dictionary and stuff in https://mypy-play.net/?mypy=master&python=3.12&gist=d8cfb3e1a6529102f01ead3c3099a1ae to make validation easier. Note that a fix for that may not be a fix for the original version because that doesn't actually access anything in B.
(edit 1: link updated)