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 complain about wrong attribute access in unannotated method #1514

Closed
The-Compiler opened this issue May 10, 2016 · 3 comments
Closed
Labels
bug mypy got something wrong priority-2-low

Comments

@The-Compiler
Copy link
Contributor

With this code:

class Klass:

    def method1(self):
        print(self.attr)

    def method2(self) -> None:
        print(self.attr)

    def method3(self, arg: int):
        print(self.attr)

mypy complains about method2 and method3, but not method1:

x.py: note: In member "method2" of class "Klass":
x.py:7: error: "Klass" has no attribute "attr"
x.py: note: In member "method3" of class "Klass":
x.py:10: error: "Klass" has no attribute "attr"

Now looking at #1415 I thought adding --check-untyped-defs would help ("type check the interior of functions without type annotations"), but the output doesn't change with that argument. Am I missing something?

@gvanrossum
Copy link
Member

gvanrossum commented May 10, 2016

The default behavior is as intended, but it looks like the flag may be
causing the type of self to be Any, which would explain it makes no
difference.

@gvanrossum gvanrossum added the bug mypy got something wrong label May 10, 2016
@gvanrossum
Copy link
Member

Yes, that's a good idea. After all that's the type given to self when there
are annotations. Maybe you can even figure out how to do it and submit a
PR?

ddfisher pushed a commit that referenced this issue May 27, 2016
…1514) (#1586)

Only non-static/non-class methods have their first parameter inferred.
gvanrossum pushed a commit that referenced this issue May 28, 2016
…1514) (#1586)

Only non-static/non-class methods have their first parameter inferred.
gvanrossum pushed a commit that referenced this issue May 30, 2016
…thods (#1514) (#1586)"

This broke one of our repos ("client").

This reverts commit e45ac10.
msullivan added a commit that referenced this issue Sep 19, 2019
)

This is accomplished by updating the helper that constructs a callable type
from a func def to fill in the self argument.

Also suppress errors about `need type annotation` in most cases in
a checked but untyped method.

This will make check_untyped_defs a lot more useful.

Closes #7309, #5401, #4637, #1514
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-2-low
Projects
None yet
Development

No branches or pull requests

5 participants
@msullivan @The-Compiler @gvanrossum @ilevkivskyi and others