Skip to content

Types of class member variables are not checked #2243

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
kencoken opened this issue Oct 12, 2016 · 5 comments
Closed

Types of class member variables are not checked #2243

kencoken opened this issue Oct 12, 2016 · 5 comments

Comments

@kencoken
Copy link

Running mypy on the following class, using Python 3.6 support (PEP 526):

class TestClass:

    a: str = 9

    def __init__(self):

        self.b: str = 8
        self.b = 12

    def test_method(self):
        self.b = 50

Results in the following output:

another_test.py: note: In class "TestClass":
another_test.py:3: error: Incompatible types in assignment (expression has type "int", variable has type "str")

It seems as if the type of the member variable b is not checked.

@gvanrossum
Copy link
Member

Try adding -> None to the signatures of both methods. Unannotated functions/methods are not checked at all.

@kencoken
Copy link
Author

Works great – thanks for the pointer!

Is the behaviour you describe intentional? Wasn't able to find anywhere where this was discussed.

@refi64
Copy link
Contributor

refi64 commented Oct 13, 2016

#2215 #1370 #1334

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 13, 2016

It's described in the documentation: https://mypy.readthedocs.io/en/latest/basics.html (maybe we should make the wording somehow more explicit, as users commonly miss this point).

@gvanrossum
Copy link
Member

gvanrossum commented Oct 13, 2016

I've added a section on this to common_issues.html.

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