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 doesn't seem to enforce instance attributes Dict types #6986

Closed
fpagnoux opened this issue Jun 13, 2019 · 1 comment
Closed

mypy doesn't seem to enforce instance attributes Dict types #6986

fpagnoux opened this issue Jun 13, 2019 · 1 comment

Comments

@fpagnoux
Copy link

fpagnoux commented Jun 13, 2019

I would expect the following snippet to raise an error when checked by mypy, yet nothing is detected.

class Test:
    def __init__(self):
        self.data: Dict[str, int] = {}

    def get(self, key) -> str:  # inconsistent with self.data type, should be int
        return self.data[key]

I'm using Python 3.7.0 and mypy 0.701
I'm still seeing the issue on master
I'm only using the flag ignore_missing_imports = True

Am I doing something wrong?

@ilevkivskyi
Copy link
Member

This is a funny one. The problem is that you don't import Dict. Moreover __init__ doesn't have any annotations, so errors aren't reported there, see PEP 484.

You can use --disallow-untyped-defs to avoid such mistakes in future, or alternatively --warn-return-any to warn you when type checking passes because the thing you return is Any.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants