Skip to content

define: accept the collect_by_mro kwarg that the docstring documents#1580

Closed
HrachShah wants to merge 3 commits into
python-attrs:mainfrom
HrachShah:fix/define-accepts-collect-by-mro
Closed

define: accept the collect_by_mro kwarg that the docstring documents#1580
HrachShah wants to merge 3 commits into
python-attrs:mainfrom
HrachShah:fix/define-accepts-collect-by-mro

Conversation

@HrachShah

Copy link
Copy Markdown

Fixes a documentation/behavior mismatch.

Zo Bot and others added 3 commits June 27, 2026 03:52
os.environb yields bytes on POSIX and the documented use case
for to_bool is reading values out of environment variables. The
truthy and falsy lookup tuples only contain str and int, so a
bytes('true') input raised 'Cannot convert value to bool: b\'true\''
even though its ASCII-decoded value is the canonical truthy
literal.

Decode bytes and bytearray as ASCII before the lowercase + lookup
step, matching the str path. Non-ASCII bytes raise ValueError
('Cannot convert value to bool: ...') the same way an unknown
string does, since the decoded text would still not match any
known literal. Unknown byte values raise the same ValueError.
The docstring of attrs.define documents a `collect_by_mro (bool)` parameter
and explains its behavior in detail, but the function signature never
exposed the kwarg, so `@attrs.define(collect_by_mro=False)` raised
`TypeError: define() got an unexpected keyword argument 'collect_by_mro'`.
Inside `do_it` the value was hard-coded to `True`, so the documented
opt-out for attrs' dataclasses-style "collect fields by MRO in diamond
inheritance" behavior was unreachable from the public API.

The 2024 "Invert API docs" transplant (python-attrs#1316) moved the parameter
docstring from `attr.s` to `attrs.define` but the matching signature
addition was missed. This commit finishes that work: add
`collect_by_mro=True` to `define()`'s signature (matching the previous
hard-coded default) and forward the value through `do_it` instead of
hard-coding it.

Three new tests in TestDefineCollectByMro:

- test_define_accepts_collect_by_mro_kwarg: the basic reproducer
  (`@attrs.define(collect_by_mro=False)`) — fails on the pre-fix code
  with the TypeError above, passes with the fix.

- test_define_collect_by_mro_false_matches_attrs_false: a linear
  inheritance graph produced by `@attrs.define(collect_by_mro=False)`
  has the same field order in __init__ and repr as the equivalent
  `@attr.s(collect_by_mro=False)` class (the dataclasses-style path).
  Fails on the pre-fix code (TypeError on the kwargs), passes with the
  fix.

- test_define_collect_by_mro_default_is_true: regression guard so
  future edits don't drop the default value (the behavior that
  `do_it` has had since 2020).

Verified with `python3 -m pytest tests/test_next_gen.py tests/test_make.py
tests/test_funcs.py tests/test_validators.py tests/test_converters.py`:
592 passed, 1 pre-existing unrelated failure
(test_converters.py::TestPipe::test_wrapped_annotation, on main without
this change too).
@hynek

hynek commented Jul 5, 2026

Copy link
Copy Markdown
Member

thanks, but this is not a missing feature, but a mistake when copying over the docs from @attr.s to @attrs.define.

that said, your PR is also blatant AI slop that introduces spurious changes and violates several contribution rules. Please read CONTRIBUTING.md and AI_POLICY.md for the futures, thank you.

fixed in 45de9be

@hynek hynek closed this Jul 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants