Skip to content

self.x: str = 1000. Why is this not an error? #7857

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

Closed
kurtgn opened this issue Nov 2, 2019 · 4 comments
Closed

self.x: str = 1000. Why is this not an error? #7857

kurtgn opened this issue Nov 2, 2019 · 4 comments

Comments

@kurtgn
Copy link

kurtgn commented Nov 2, 2019

The question is basically in the header.

Why is this an error:

count: str = 1000  # expression has type "int", variable has type "str"

and this is not:

class Counter:
    def __init__(self):
        self.count: str = 1000
c = Counter()

I have the latest mypy as of today:

mypy 0.740 
├── mypy-extensions 0.4.3 
├── typed-ast 1.4.0
└── typing-extensions 3.7.4.1
@emmatyping
Copy link
Member

Hi, it looks like __init__ is not annotated, thus mypy will skip it by default. I recommend that you check out the docs to learn more.

@JelleZijlstra
Copy link
Member

I feel like it'd be reasonable to change the behavior though. The intent behind mypy's behavior of not type checking unannotated methods is to enable gradual typing: users can gradually add more typing to their codebase, without worrying about type errors in functions that don't have annotations yet. But clearly if the user put a variable annotation in the function, they did intend to apply typing to the function and would expect mypy to typecheck it.

@emmatyping
Copy link
Member

But clearly if the user put a variable annotation in the function, they did intend to apply typing to the function and would expect mypy to typecheck it.

Yeah, I think this is a reasonable idea.

@emmatyping emmatyping reopened this Nov 3, 2019
@ilevkivskyi
Copy link
Member

Yeah, I think this is a reasonable idea.

This is then essentially a duplicate of #3948

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

4 participants