KEMBAR78
[PEP 695] Allow covariance with attribute that has "_" name prefix by JukkaL · Pull Request #17782 · python/mypy · GitHub
Skip to content

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Sep 18, 2024

Fix this conformance test:

class ShouldBeCovariant5[T]:
    def __init__(self, x: T) -> None:
        self._x = x

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

vo5_1: ShouldBeCovariant5[float] = ShouldBeCovariant5[int](1)  # OK
vo5_2: ShouldBeCovariant5[int] = ShouldBeCovariant5[float](1)  # E

My fix is to treat such attributes as not settable when inferring variance.

Link:
https://github.com/python/typing/blob/main/conformance/tests/generics_variance_inference.py#L79

Fix this conformance test:
```
class ShouldBeCovariant5[T]:
    def __init__(self, x: T) -> None:
        self._x = x

    @Property
    def x(self) -> T:
        return self._x

vo5_1: ShouldBeCovariant5[float] = ShouldBeCovariant5[int](1)  # OK
vo5_2: ShouldBeCovariant5[int] = ShouldBeCovariant5[float](1)  # E
```

My fix is to treat such attributes a not settable when inferring variance.

Link:
https://github.com/python/typing/blob/main/conformance/tests/generics_variance_inference.py#L79
@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@JukkaL JukkaL merged commit fce14a0 into master Sep 18, 2024
19 checks passed
@JukkaL JukkaL deleted the variance-underscore branch September 18, 2024 13:42
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