Skip to content

make_class: accept Attribute instances in 'these' (#1424)#1581

Draft
HrachShah wants to merge 3 commits into
python-attrs:mainfrom
HrachShah:main
Draft

make_class: accept Attribute instances in 'these' (#1424)#1581
HrachShah wants to merge 3 commits into
python-attrs:mainfrom
HrachShah:main

Conversation

@HrachShah

Copy link
Copy Markdown

This PR fixes #1424.

When a class is rebuilt from attrs.fields() via attrs.make_class, the these mapping contains Attribute instances rather than _CountingAttr objects. from_counting_attr only read the private _default, _validator, and _converter attributes, which Attribute does not expose, so the rebuild crashed with 'Attribute' object has no attribute '_default'.

The fix detects when ca is already an Attribute and uses the public default, validator, and converter attributes directly.

Drafted with github-automation@zo.computer; reviewed by keenrose.

Test output

``$
tests/test_make.py::TestMakeClass::test_make_class_from_attrs_fields PASSED

</details>

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.
When a class is rebuilt from attrs.fields() via attrs.make_class, the
'these' mapping contains Attribute instances rather than _CountingAttr
objects. from_counting_attr was only reading the private _default,
_validator, and _converter attributes, which Attribute does not expose,
so the rebuild crashed with "'Attribute' object has no attribute
'_default'".

Detect an Attribute argument and read the public default/validator/
converter attributes directly. Add a regression test and a changelog
entry.
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.

Can only populate attrs classes with _CountingAttr instances

1 participant