KEMBAR78
Better support for default arguments to new-style attrs API · Issue #15639 · python/mypy · GitHub
Skip to content

Better support for default arguments to new-style attrs API #15639

@ljden

Description

@ljden

Bug Report
attrs.define has slots=True by default. Mypy does not use the default value when analysing

To Reproduce

# demo.py
import attr

@attr.define
class A:
   x: int

   def __init__(self, x :int) -> None:
       self.x = x  # ok
       self.y = 0  # slots error

A(1)

Expected Behavior

$ mypy --strict demo.py 
demo.py:9: error: Trying to assign name "y" that is not in "__slots__" of type "demo.A"  [misc]
Found 1 error in 1 file (checked 1 source file)

Actual Behavior

$ mypy --strict demo.py 
Success: no issues found in 1 source file
$ python3 demo.py 
Traceback (most recent call last):
  File "demo.py", line 11, in <module>
    A(1)
  File "demo.py", line 9, in __init__
    self.y = 0  # slots error
AttributeError: 'A' object has no attribute 'y'

Adding (slots=True) to the define decorator results in mypy correctly diagnosing the slots violation:

# demo.py
import attr

@attr.define(slots=True)
class A:
   x: int

   def __init__(self, x :int) -> None:
       self.x = x  # ok
       self.y = 0  # slots error

A(1)
$ mypy --strict demo.py 
demo.py:9: error: Trying to assign name "y" that is not in "__slots__" of type "demo.A"  [misc]
Found 1 error in 1 file (checked 1 source file)

Your Environment

$ mypy --version
mypy 1.4.1 (compiled: yes)
$ python3 --version
Python 3.10.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions