Skip to content

type inference depends on method definition order #4727

Open
@matejcik

Description

@matejcik

With current mypy master and python 3.5.2, the following fails type checking:

class A:
    def hello(self) -> None:
        if self.x:
            self.x = False

    def world(self) -> None:
        self.x = True

poc.py:3: error: Cannot determine type of 'x'

The following passes:

class A:
    def world(self) -> None:
        self.x = True

    def hello(self) -> None:
        if self.x:
            self.x = False

The following also passes:

class A:
    def hello(self) -> None:
        if self.x:
            self.x = False  # type: bool

    def world(self) -> None:
        self.x = True

despite the fact that at the line where I'm putting the annotation, it's already redundant. However, annotating the usage in world() doesn't help.

(Aside: I suspect the underlying reason for this is the same as why mypy won't infer types for None variables set in __init__ ? That is a major paint point, it would be super nice if that was resolved.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions