Skip to content

Issue with attrs custom __init__ defined #20818

@George-Ogden

Description

@George-Ogden

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:

init = _get_decorator_bool_argument(ctx, "init", True)

as it does not check whether the __init__ method exists, it only checks that init=False when attrs.define is called.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions