Skip to content

False positive error "incompatible type" #13566

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

Open
Mirraz opened this issue Aug 31, 2022 · 2 comments
Open

False positive error "incompatible type" #13566

Mirraz opened this issue Aug 31, 2022 · 2 comments
Labels
bug mypy got something wrong topic-inheritance Inheritance and incompatible overrides

Comments

@Mirraz
Copy link

Mirraz commented Aug 31, 2022

For this code:

from typing import Any, Generic, TypeVar

T = TypeVar('T')


class EtcdValueWatcherTyped(Generic[T]):
    def __init__(self):
        pass


class EtcdValueWatcher(EtcdValueWatcherTyped[dict[str, Any]]):
    pass


class SettingsWatcher(EtcdValueWatcher):
    def __init__(self, arg1: str):
        self.__arg1 = arg1
        EtcdValueWatcher.__init__(self)


def main():
    watcher = SettingsWatcher('/v1/qwe')

mypy outputs error:

test.py: note: In member "__init__" of class "SettingsWatcher":
test.py:19: error: Argument 1 to "__init__" has incompatible type "SettingsWatcher"; expected "EtcdValueWatcherTyped[T]"  [arg-type]
            EtcdValueWatcher.__init__(self)
                                      ^
Found 1 error in 1 file (checked 1 source file)

which is false positive.

  • Mypy version used: mypy 0.971 (compiled: yes)
  • Mypy command-line flags: mypy --config-file ./mypy.ini ./test.py
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
ignore_missing_imports = True
show_error_codes = True
show_error_context = True
pretty = True
no_site_packages = False
  • Python version used: Python 3.10.6
  • Operating system and version: Fedora 35
@Mirraz Mirraz added the bug mypy got something wrong label Aug 31, 2022
@AlexWaygood AlexWaygood added the topic-inheritance Inheritance and incompatible overrides label Sep 2, 2022
@asqui
Copy link

asqui commented Nov 25, 2022

We've run in to this problem too...

Is there an easy way to work around this in the meantime until a fix is released?

@leandro-lucarella-frequenz
Copy link

leandro-lucarella-frequenz commented Feb 24, 2023

Same here, I think this is a more reduced test case (mypy-playground):

EDIT: Is not the same case actually, my case involves constraints.

from typing import Generic, TypeVar

T = TypeVar("T", float, int)


class C(Generic[T]):
    def __init__(self, i: T) -> None:
        self.i: T = i


class B(C[T]):
    def __init__(self, i: T) -> None:
        super().__init__(i)
main.py: note: In member "__init__" of class "B":
main.py:13:26: error: Argument 1 to "__init__" of "C" has incompatible type "float"; expected "T"  [arg-type]
main.py:13:26: error: Argument 1 to "__init__" of "C" has incompatible type "int"; expected "T"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-inheritance Inheritance and incompatible overrides
Projects
None yet
Development

No branches or pull requests

4 participants