Issue with `attrs` custom `__init__` defined
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
__init__ definition is ignored when generating attrs.define custom version.
To Reproduce
import attrs
@attrs.define
class A:
x: int
def __init__(self, x: str) -> None: ...
@attrs.define(init=False)
class B:
x: int
def __init__(self, x: str) -> None: ...
Expected Behavior
A(1) # error: Argument 1 to "A" has incompatible type "int"; expected "str" [arg-type]
A("1")
B(1) # error: Argument 1 to "B" has incompatible type "int"; expected "str" [arg-type]
B("1")
Actual Behavior
A("1") # error: Argument 1 to "A" has incompatible type "str"; expected "int" [arg-type]
B(1) # error: Argument 1 to "B" has incompatible type "int"; expected "str" [arg-type]
B("1")
Your Environment
- Mypy version used: mypy 1.20.0+dev
- Mypy command-line flags: file.py --show-traceback
- Mypy configuration options from
mypy.ini(and other config files): NA - Python version used: Python 3.13.9
Notes
I think the issue comes from:
https://github.com/python/mypy/blob/a86f7e3ab0ce17b9839239951430348e69c3284b/mypy/plugins/attrs.py#L345
as it does not check whether the __init__ method exists, it only checks that init=False when attrs.define is called.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in mypy/plugins/attrs.py around line 345 and trace how attrs.define determines the generated constructor when a class already defines init. Reproduce the A and B examples from the issue, then add regression coverage showing that both custom constructors are respected and that the expected argument-type errors are reported.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 62/100