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

Subclass may unsoundly restrict an instance variable type #13828

Closed
andersk opened this issue Oct 6, 2022 · 1 comment
Closed

Subclass may unsoundly restrict an instance variable type #13828

andersk opened this issue Oct 6, 2022 · 1 comment
Labels
bug mypy got something wrong

Comments

@andersk
Copy link
Contributor

andersk commented Oct 6, 2022

Bug Report

Mypy allows an instance variable annotation on a subclass to restrict the variable’s type in a way that’s incompatible with the superclass. This is unsound and should be forbidden.

To Reproduce

class A:
    def __init__(self, x: object) -> None:
        self.x = x

class B(A):
    x: int

B("string").x + 1

https://mypy-play.net/?mypy=latest&python=3.10&gist=00ef5dbb43039cac3d1672b3ece3029a&flags=strict

Mypy doesn’t give any errors, but should, because this fails at runtime with TypeError: can only concatenate str (not "int") to str.

Expected Behavior

mypy should complain about x: int.

Actual Behavior

No mypy errors.

Your Environment

  • Mypy version used: 0.981
  • Mypy command-line flags: --strict
  • Python version used: 3.10.7
@andersk andersk added the bug mypy got something wrong label Oct 6, 2022
@hauntsaninja
Copy link
Collaborator

Duplicate of #3208

@hauntsaninja hauntsaninja marked this as a duplicate of #3208 Oct 6, 2022
@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Oct 6, 2022
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

No branches or pull requests

2 participants