Check that attribute exists [attr-defined]

Mypy checks that an attribute is defined in the target class or modulewhen using the dot operator. This applies to both getting and settingan attribute. New attributes are defined by assignments in the classbody, or assignments to self.x in methods. These assignments don’tgenerate attr-defined errors.

Example:

  1. class Resource:
  2. def __init__(self, name: str) -> None:
  3. self.name = name
  4.  
  5. r = Resouce('x')
  6. print(r.name) # OK
  7. print(r.id) # Error: "Resource" has no attribute "id" [attr-defined]
  8. r.id = 5 # Error: "Resource" has no attribute "id" [attr-defined]

This error code is also generated if an imported name is not definedin the module in a from … import statement (as long as thetarget module can be found):

  1. # Error: Module 'os' has no attribute 'non_existent' [attr-defined]
  2. from os import non_existent

A reference to a missing attribute is given the Any type. In theabove example, the type of non_existent will be Any, which canbe important if you silence the error.