-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Labels
Description
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.