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

mypy incorrectly marks a @staticmethod definition of an attribute A to be incompatible with A's signature in a parent class. #12569

Closed
pawelrubin opened this issue Apr 12, 2022 · 3 comments · Fixed by #18018
Labels
bug mypy got something wrong topic-calls Function calls, *args, **kwargs, defaults topic-descriptors Properties, class vs. instance attributes

Comments

@pawelrubin
Copy link

Bug Report
When a class C defines an attribute a to be a union type with Callable as one of its types (a: Callable | ...), mypy incorrectly marks a @staticmethod definition of a in a child class that inherits from C to be incompatible with a's signature in C.

Worth noting that the following code produces no errors:

from typing import Callable


class Parent:
    foo: Callable[[str], str]

class Child2(Parent):
    @staticmethod
    def foo(x: str) -> str:
        return x.lower()

but the following code does

from typing import Callable


class Parent:
    foo: Callable[[str], str] | str 


class Child1(Parent):
    foo = "bar"  # OK


class Child2(Parent):
    @staticmethod
    def foo(x: str) -> str:  # Signature of "foo" incompatible with supertype "Parent"
        return x.lower()

Expected Behavior
No errors.

Actual Behavior

example.py: note: In class "Child2":
example.py:14: error: Signature of "foo" incompatible with supertype "Parent"  [override]
Found 1 error in 1 file (checked 1 source file)

Environment

  • Mypy version used: 0.941
  • Mypy command-line flags: --strict, --show-error-codes, --show-error-context
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.10.3
  • Operating system and version: MacOS 12.3
@pawelrubin pawelrubin added the bug mypy got something wrong label Apr 12, 2022
@pawelrubin pawelrubin changed the title False positive mypy incorrectly marks a @staticmethod definition of an attribute A to be incompatible with A's signature in C. Apr 12, 2022
@pawelrubin pawelrubin changed the title mypy incorrectly marks a @staticmethod definition of an attribute A to be incompatible with A's signature in C. mypy incorrectly marks a @staticmethod definition of an attribute A to be incompatible with A's signature in a parent class. Apr 12, 2022
@AlexWaygood AlexWaygood added topic-calls Function calls, *args, **kwargs, defaults topic-descriptors Properties, class vs. instance attributes labels Apr 12, 2022
@JelleZijlstra
Copy link
Member

This is a bit dubious, because the definition on the parent class would allow code like Parent().foo = "some str", but the child class defines a method that shouldn't be settable. It appears we already allow some similar unsafety with callable types, but the proposed solution would extend this to cases involving non-callables.

@brianschubert
Copy link
Collaborator

That's a good point. It seems like overriding an attribute with a method is something that should be flagged by [mutable-override], but currently isn't. I opened a new issue to track that, #18052.

From what I've seen, mypy is currently pretty tolerant about allowing unsafe overrides in other cases. Do you think it's ok to let this case fly too, provided that users can opt-in to protection with [mutable-override] or the like?

@JelleZijlstra
Copy link
Member

I don't feel too strongly, it's probably OK to do as you suggest.

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-calls Function calls, *args, **kwargs, defaults topic-descriptors Properties, class vs. instance attributes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants