Skip to content

mypy doesn't complain about wrong attribute access in unannotated method #1514

Closed

Description

@The-Compiler

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?

Activity

  1. gvanrossum commented on May 10, 2016

    @gvanrossum
    Member

    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.

  2. gvanrossum commented on May 25, 2016

    @gvanrossum
    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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions