Allow union-with-callable attributes to be overridden by methods by brianschubert · Pull Request #18018 · python/mypy (original) (raw)

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation10 Commits7 Checks18 Files changed

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

brianschubert

Fixes #12569

Before

from typing import Callable

class Parent: f1: Callable[[str], str] f2: Callable[[str], str] | str

class Child(Parent): def f1(self, x: str) -> str: return x # ok def f2(self, x: str) -> str: return x # Signature of "f2" incompatible with supertype "Parent"

After

from typing import Callable

class Parent: f1: Callable[[str], str] f2: Callable[[str], str] | str

class Child(Parent): def f1(self, x: str) -> str: return x # ok def f2(self, x: str) -> str: return x # ok

@brianschubert

@github-actions GitHub Actions

This comment has been minimized.

JelleZijlstra

@JelleZijlstra

@pre-commit-ci

@github-actions GitHub Actions

This comment has been minimized.

hauntsaninja

Choose a reason for hiding this comment

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

@brianschubert

@github-actions GitHub Actions

Diff from mypy_primer, showing the effect of this PR on open source code:

packaging (https://github.com/pypa/packaging)

steam.py (https://github.com/Gobot1234/steam.py)

hauntsaninja

Choose a reason for hiding this comment

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

Thanks!

I kind of want to try ignore_pos_arg_names=False. This is something we probably want to eventually do anyway and should be fairly harmless in this case. But it's likely wiser to be consistent.

@hauntsaninja

I was thinking about this PR some more and I think it introduces some unsoundness. We should have a Liskov error in the following:

import typing

class X:
    @property
    def x(self) -> typing.Optional[bool]: ...

    @x.setter
    def x(self, value: typing.Optional[bool]) -> None: ...

class Y(X):
    @property
    def x(self) -> bool: ...

    @x.setter
    def x(self, value: bool) -> None: ...

Input to setter should be wider, output of getter should be narrower.

@brianschubert

Hmm, I think that setup is currently caught by mutable-override, is it not? Is the behavior after this PR different from what mypy does for this setup?

class X: x: bool | None

class Y(X): x: bool

@hauntsaninja

Oh looks like I must have messed up my test. It does error with mutable-override, sorry for the noise!

@hauntsaninja

I think what happened is I messed up the branches I tested. I think I probably did this, which mypy doesn't error on:

class AA:
    @property
    def a(self) -> int: ...
    @a.setter
    def a(self, value: int | str) -> None: ...

class BB(AA):
    @property
    def a(self) -> int: ...
    @a.setter
    def a(self, value: int) -> None: ...

This would be unsound, except that mypy doesn't support this at all and will error if you attempt to set a to str. This is the (very popular) issue for that: #3004