Skip to content

[mypyc] Fix property assignment when the setter accepts a wider type than the getter - #22118

Merged
JukkaL merged 1 commit into
python:masterfrom
VaggelisD:mypyc-asymmetric-property-setter
Oct 7, 2026
Merged

JukkaL merged 1 commit into
python:masterfrom
VaggelisD:mypyc-asymmetric-property-setter

Conversation

@VaggelisD

@VaggelisD VaggelisD commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

When a property setter accepts a wider type than the getter returns, mypyc coerced the assigned value to the getter's type, so valid assignments failed at runtime:

from typing import Union

class C:
    def __init__(self) -> None:
        self._x = 0

    @property
    def x(self) -> int:
        return self._x

    @x.setter
    def x(self, value: Union[int, str]) -> None:
        self._x = int(value)

def set_x(c: C) -> int:
    c.x = "1"  # TypeError: int object expected; got str
    return c.x

This uses the setter's argument type instead, both when coercing the assigned value and in the generated CPY_SET_ATTR call.

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, looks good!

@JukkaL
JukkaL merged commit 6b387c7 into python:master Oct 7, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants