Skip to content

attrs plugin: support the auto_detect option #10328

@hynek

Description

@hynek

Feature

attrs has had an option to automatically detect dunder methods that are directly on the current class. It's called auto_detect and is off by default in old-school APIs (attr.s/attrs) and on in NG (define).

mypy currently doesn't support that.

Pitch

This feature is really nice at preventing a common gotcha of writing an own method and forgetting to set init=False and attrs silently overwriting that code.

Here's an example that works as expected but doesn't pass mypy:

@attr.define
class SortableTableHeaders:
    headers: dict[str, str]
    default: str

    def __init__(self, names: tuple[str, ...]) -> None:
        def normalize(s: str) -> str:
            return s.replace(" ", "-").lower()

        self.default = normalize(names[0])
        self.headers = {normalize(n): n for n in names}

It does it for all dunders but I think only __init__ is relevant. To stress once more: only the current class is considered, not anything inherited.

Ref python-attrs/attrs#793

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions