Check that type of target is known [has-type]

Mypy sometimes generates an error when it hasn’t inferred any type fora variable being referenced. This can happen for references tovariables that are initialized later in the source file, and forreferences across modules that form an import cycle. When thishappens, the reference gets an implicit Any type.

In this example the definitions of x and y are circular:

  1. class Problem:
  2. def set_x(self) -> None:
  3. # Error: Cannot determine type of 'y' [has-type]
  4. self.x = self.y
  5.  
  6. def set_y(self) -> None:
  7. self.y = self.x

To work around this error, you can add an explicit type annotation tothe target variable or attribute. Sometimes you can also reorganizethe code so that the definition of the variable is placed earlier thanthe reference to the variable in a source file. Untangling cyclicimports may also help.

We add an explicit annotation to the y attribute to work aroundthe issue:

  1. class Problem:
  2. def set_x(self) -> None:
  3. self.x = self.y # OK
  4.  
  5. def set_y(self) -> None:
  6. self.y: int = self.x # Added annotation here