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

Protocols with self break super #12344

Closed
DevilXD opened this issue Mar 13, 2022 · 1 comment · Fixed by #14082
Closed

Protocols with self break super #12344

DevilXD opened this issue Mar 13, 2022 · 1 comment · Fixed by #14082
Labels
bug mypy got something wrong topic-protocols topic-self-types Types for self

Comments

@DevilXD
Copy link

DevilXD commented Mar 13, 2022

Bug Report

Sometimes there's a need to use a typing.Protocol to specify self typing for cases like mixin classes, where the methods aren't there at class definition, but will be there at runtime, once the mixin is inherited from. This generally works fine, except it breaks in one silly case, a short repro of which is available below.

In short, it looks like super doesn't like protocols as it's second argument, which isn't even explicitly passed here.

To Reproduce

from typing import Protocol

class MixinProt(Protocol):
    def func(self) -> None:
        ...

class Mixin:
    def func(self: MixinProt) -> None:
        return super().func()

Expected Behavior

No error, duh.

Actual Behavior

From the very last line of the code above:

Argument 2 for "super" not an instance of argument 1

Your Environment

  • Mypy version used: 0.931
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.8.10
  • Operating system and version: Win7x64
@DevilXD DevilXD added the bug mypy got something wrong label Mar 13, 2022
@henryiii
Copy link
Contributor

FYI, PyLance seems to handle this correctly; I'm not seeing this mypy error in VSCode. MyPy still does this in 0.891.

@ilevkivskyi ilevkivskyi added the topic-self-types Types for self label Nov 13, 2022
ilevkivskyi added a commit that referenced this issue Nov 16, 2022
Fixes #12344 

FWIW this is unsafe (since we don't know where the mixin will appear in
the MRO of the actual implementation), but the alternative is having
annoying false positives like this issue and e.g.
#4335
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-protocols topic-self-types Types for self
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants