Skip to content

Commit

Permalink
Change default: gatherers by default do not leave default values - th…
Browse files Browse the repository at this point in the history
…is is to make them compatible with the SlotMakerMeta by default.
  • Loading branch information
DavidCEllis committed Aug 6, 2024
1 parent 295b8c2 commit ebd1433
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/ducktools/classbuilder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ def field_slot_gatherer(cls_or_ns):

def make_annotation_gatherer(
field_type=Field,
leave_default_values=True,
leave_default_values=False,
):
"""
Create a new annotation gatherer that will work with `Field` instances
Expand Down Expand Up @@ -809,7 +809,7 @@ def field_annotation_gatherer(cls_or_ns):

def make_field_gatherer(
field_type=Field,
leave_default_values=True,
leave_default_values=False,
):
def field_attribute_gatherer(cls_or_ns):
if isinstance(cls_or_ns, (_MappingProxyType, dict)):
Expand Down Expand Up @@ -842,7 +842,7 @@ def field_attribute_gatherer(cls_or_ns):

def make_unified_gatherer(
field_type=Field,
leave_default_values=True,
leave_default_values=False,
):
"""
Create a gatherer that will work via first slots, then
Expand Down Expand Up @@ -892,7 +892,7 @@ def field_unified_gatherer(cls_or_ns):

# The unified gatherer used for slot classes must remove default
# values for slots to work correctly.
unified_gatherer = make_unified_gatherer(leave_default_values=False)
unified_gatherer = make_unified_gatherer()


# Now the gatherers have been defined, add __repr__ and __eq__ to Field.
Expand Down
14 changes: 8 additions & 6 deletions tests/annotations/test_annotated.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ class ExampleAnnotated:
for key in "defgh":
assert key not in annos

# Instance variables not removed from class
# Field replaced with default value on class
assert modifications["c"] == "c"
# Instance variables to be removed from class
assert modifications["a"] is NOTHING
assert modifications["b"] is NOTHING
assert modifications["c"] is NOTHING


def test_make_annotation_gatherer():
Expand All @@ -71,7 +72,7 @@ class NewField(Field):

gatherer = make_annotation_gatherer(
field_type=NewField,
leave_default_values=False,
leave_default_values=True,
)

class ExampleAnnotated:
Expand All @@ -91,10 +92,11 @@ class ExampleAnnotated:

assert annos["blank_field"] == NewField(type=str)

# ABC should be present in annos but removed from the class
# ABC should be present in annos and in the class
for key in "abc":
assert annos[key] == NewField(default=key, type=annotations[key])
assert modifications[key] is NOTHING

assert modifications["c"] == "c"

# Opposite for classvar
for key in "defgh":
Expand Down

0 comments on commit ebd1433

Please sign in to comment.