Skip to content

Accessing property through a Union doesn't work #14439

Description

@julian-klode

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

Activity

  1. julian-klode commented on Jan 12, 2023

    @julian-klode
    Author

    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.

  2. julian-klode commented on Dec 13, 2023

    @julian-klode
    Author

    This is still an issue in 1.7.1

  3. julian-klode commented on Jan 8, 2024

    @julian-klode
    Author

    This is still an issue in 1.8.0

  4. julian-klode commented on Jan 8, 2024

    @julian-klode
    Author

    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)

  5. ilevkivskyi commented on Feb 10, 2025

    @ilevkivskyi
    Member

    This now works correctly (i.e. without any errors).

  6. added a commit that references this issue on Sep 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions