Skip to content
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 and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorated method type-incompatibility with superclass #5836

Closed
joelberkeley opened this issue Oct 25, 2018 · 1 comment · Fixed by #8350
Closed

Decorated method type-incompatibility with superclass #5836

joelberkeley opened this issue Oct 25, 2018 · 1 comment · Fixed by #8350
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal

Comments

@joelberkeley
Copy link

joelberkeley commented Oct 25, 2018

I'm raising a bug.

It's also posted on SO at https://stackoverflow.com/q/52744713/5986907

I get

$ mypy file.py
python.py:34: error: Signature of "fn" incompatible with supertype "B"

for file.py

from typing import Callable, TypeVar, cast

T = TypeVar('T')


def dec(f: Callable[..., T]) -> T:
    def p(self):
        return getattr(self, '_' + f.__name__)

    return cast(T, property(p))


class X:
    pass


class Y(X):
    pass


class A:
    _fn = Y()


class B(A):
    @dec
    def fn(self) -> X:
        return X()


class C(B):
    @dec
    def fn(self) -> Y:
        return Y()

I'd expect there to be no error as Y is a subclass of X.

I've tried this with mypy 0.630 and Python 3.5.2, 3.6.6 and 3.7.0. Also with mypy git master for python 3.5.2 and 3.7.0

It's possible these are related but I couldn't quite connect them
#220
#4430

@ilevkivskyi
Copy link
Member

Hm, it still fails on master, this is because of this code:

            elif is_equivalent(original_type, typ):
                # Assume invariance for a non-callable attribute here. Note
                # that this doesn't affect read-only properties which can have
                # covariant overrides.
                #
                # TODO: Allow covariance for read-only attributes?
                pass
            else:
                # ...give error

I think this is easy to fix by replacing is_equivalent with is_subtype at least for effectively read-only attributes. Anyway mypy currently allows covariant overriding for mutable attributes although this is technically unsafe, see #3208

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants