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

Overriding callable attribute with method not caught by [mutable-override] #18052

Closed
brianschubert opened this issue Oct 26, 2024 · 0 comments · Fixed by #18058
Closed

Overriding callable attribute with method not caught by [mutable-override] #18052

brianschubert opened this issue Oct 26, 2024 · 0 comments · Fixed by #18058
Labels
bug mypy got something wrong

Comments

@brianschubert
Copy link
Collaborator

Bug Report

Follows from #12569, #3208

Mypy has an optional error code mutable-override that checks for unsafe overrides of mutable attributes:

class Parent:
    x: float

class Child(Parent):
    x: int  # E: Covariant override of a mutable attribute (base class "Parent" defined the type as "float", expression has type "int")  [mutable-override]

However, this error isn't emitted when overriding a callable attribute with a method:

from typing import Callable

class Parent:
    func: Callable[[str], None]

class Child(Parent):
    def func(self, x: object) -> None: pass  # no error

This allows dubious code like the following to pass type checking:

def foo(x: str) -> None:
    assert isinstance(x, str)
    
def modify_parent(x: Parent) -> None:
    x.func = foo 
    
c = Child()
modify_parent(c)
c.func(1)  # boom!

Your Environment

  • Mypy version used: master (1.14.0+dev.2b033cbd7)
  • Mypy command-line flags: --strict --enable-error-code=mutable-override
  • Mypy configuration options from mypy.ini (and other config files): N/A (empty)
  • Python version used: 3.12.7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant