Skip to content

No error if derived type is different from annotated type #3945

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
henryJack opened this issue Sep 12, 2017 · 6 comments
Closed

No error if derived type is different from annotated type #3945

henryJack opened this issue Sep 12, 2017 · 6 comments

Comments

@henryJack
Copy link

henryJack commented Sep 12, 2017

Take this incorrectly annotated example:

x: bool = 5

Gives no mypy error. Surely if mypy knows that Type(int) is different to Type(bool), we could have a new mypy warning of Annotated type different to derived type?

@JukkaL
Copy link
Collaborator

JukkaL commented Sep 12, 2017

Is this within an annotated function? This works correctly for me when I type check a file containing just x: bool = 5:

t.py:1: error: Incompatible types in assignment (expression has type "int", variable has type "bool")

@henryJack
Copy link
Author

Ah yes, sorry. It is inside an annotated function!

@JukkaL
Copy link
Collaborator

JukkaL commented Sep 12, 2017

Can you give a more complete example and the mypy command line that you use that fails to generate the error? This should work.

@henryJack
Copy link
Author

henryJack commented Sep 12, 2017

I get no error if I run mypy on the following code:

    def new_func(self):
        a: bool = 5

However, If I annotate it, I get the following error -> error: Incompatible types in assignment (expression has type "int", variable has type "bool"):

    def new_func(self) -> None:
        a: bool = 5

@JukkaL
Copy link
Collaborator

JukkaL commented Sep 12, 2017

Mypy doesn't type check functions that don't have a signature (in this case, -> None). This is clearly kind of confusing in case you have a variable annotation. You can use --check-untyped-defs to force mypy to type check all functions.

@ilevkivskyi
Copy link
Member

IIUC this is tracked in #3948 so I am closing this now.

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

3 participants