Skip to content

Commit

Permalink
Fix one more Validator.evolve issue for downstream Validators.
Browse files Browse the repository at this point in the history
Here for renamed attributes out of attrs-using classes.

Refs: '#982 (comment)'
  • Loading branch information
Julian committed Aug 17, 2022
1 parent c2e86ee commit a8c3b14
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v4.10.2
-------

* Fix a second place where subclasses may have added attrs attributes (#982).

v4.10.1
-------

Expand Down
4 changes: 3 additions & 1 deletion jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1488,12 +1488,14 @@ def test_evolve_with_subclass(self):
@attr.s
class OhNo(self.Validator):
foo = attr.ib(factory=lambda: [1, 2, 3])
_bar = attr.ib(default=37)

validator = OhNo({})
validator = OhNo({}, bar=12)
self.assertEqual(validator.foo, [1, 2, 3])

new = validator.evolve(schema={"type": "integer"})
self.assertEqual(new.foo, [1, 2, 3])
self.assertEqual(new._bar, 12)

def test_it_delegates_to_a_legacy_ref_resolver(self):
"""
Expand Down
10 changes: 6 additions & 4 deletions jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,14 @@ def check_schema(cls, schema):
raise exceptions.SchemaError.create_from(error)

def evolve(self, **changes):
schema = changes.setdefault("schema", self.schema)
NewValidator = validator_for(schema, default=self.__class__)

# Essentially reproduces attr.evolve, but may involve instantiating
# a different class than this one.
for field in attr.fields(Validator):
cls = self.__class__

schema = changes.setdefault("schema", self.schema)
NewValidator = validator_for(schema, default=cls)

for field in attr.fields(cls):
if not field.init:
continue
attr_name = field.name # To deal with private attributes.
Expand Down

0 comments on commit a8c3b14

Please sign in to comment.