make_class: accept Attribute instances in 'these' (#1424)#1581
Draft
HrachShah wants to merge 3 commits into
Draft
make_class: accept Attribute instances in 'these' (#1424)#1581HrachShah wants to merge 3 commits into
HrachShah wants to merge 3 commits into
Conversation
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.
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes #1424.
When a class is rebuilt from
attrs.fields()viaattrs.make_class, thethesemapping containsAttributeinstances rather than_CountingAttrobjects.from_counting_attronly read the private_default,_validator, and_converterattributes, whichAttributedoes not expose, so the rebuild crashed with'Attribute' object has no attribute '_default'.The fix detects when
cais already anAttributeand uses the publicdefault,validator, andconverterattributes directly.Drafted with github-automation@zo.computer; reviewed by keenrose.
Test output
``$
tests/test_make.py::TestMakeClass::test_make_class_from_attrs_fields PASSED