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

Overloading function issues (incompatible with supertype). #3750

Open
cynecx opened this issue Jul 20, 2017 · 3 comments
Open

Overloading function issues (incompatible with supertype). #3750

cynecx opened this issue Jul 20, 2017 · 3 comments

Comments

@cynecx
Copy link

cynecx commented Jul 20, 2017

mypy 0.530-dev-417e925b16bcd63ba594f5a85c54b2407bd8831d
KeyTy = TypeVar("KeyTy")
ValTy = TypeVar("ValTy")
TempTy = TypeVar("TempTy")

class CIDict(Dict[KeyTy, ValTy]):
    @overload
    def pop(self, key: KeyTy) -> ValTy:
        pass

    @overload
    def pop(self, key: KeyTy, default: Union[ValTy, TempTy]) \
            -> Union[ValTy, TempTy]:
        pass

    def pop(self, key: KeyTy, default: Optional[Union[ValTy, TempTy]] = None) \
            -> Union[ValTy, TempTy]:
        pass

Running mypy with:

mypy test.py --strict-optional --ignore-missing-imports --disallow-untyped-calls --python-version 3.6

Issues:

CIDict.py:31: error: Signature of "pop" incompatible with supertype "MutableMapping"
CIDict.py:40: error: Overloaded function implementation does not accept all possible arguments of signature 2

Line number differ because the code above is only a snippet.

What am I missing here?

@cynecx
Copy link
Author

cynecx commented Jul 20, 2017

About that incompatible supertype error. Is it somehow possible to get mypy to print the difference between both types?

@ilevkivskyi
Copy link
Member

This actually looks like a bug, the original signature is:

    @overload
    def pop(self, k: _KT) -> _VT: ...
    @overload
    def pop(self, k: _KT, default: Union[_VT, _T] = ...) -> Union[_VT, _T]: ...

Also your idea to print the types seems reasonable, therefore I am also adding a usability label.

@ilevkivskyi
Copy link
Member

OK, I have finally found the crux here, mypy considers this unsafe because of arg kinds:

from typing import overload

class A:
    @overload
    def f(self) -> int: ...
    @overload
    def f(self, x: int = ...) -> float: ...

class B(A):
    @overload
    def f(self) -> int: ...
    @overload
    def f(self, x: int) -> float: ...

I am not sure how to convince mypy that it is actually a subtype, this would require a complex overload subtyping algorithm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants