From ebd1433e683568cd0879cb8e61a9a583c81fa370 Mon Sep 17 00:00:00 2001 From: David C Ellis Date: Tue, 6 Aug 2024 11:13:28 +0100 Subject: [PATCH] Change default: gatherers by default do not leave default values - this is to make them compatible with the SlotMakerMeta by default. --- src/ducktools/classbuilder/__init__.py | 8 ++++---- tests/annotations/test_annotated.py | 14 ++++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/ducktools/classbuilder/__init__.py b/src/ducktools/classbuilder/__init__.py index 60e0a90..1777d3b 100644 --- a/src/ducktools/classbuilder/__init__.py +++ b/src/ducktools/classbuilder/__init__.py @@ -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 @@ -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)): @@ -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 @@ -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. diff --git a/tests/annotations/test_annotated.py b/tests/annotations/test_annotated.py index 0f5e287..d053cc9 100644 --- a/tests/annotations/test_annotated.py +++ b/tests/annotations/test_annotated.py @@ -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(): @@ -71,7 +72,7 @@ class NewField(Field): gatherer = make_annotation_gatherer( field_type=NewField, - leave_default_values=False, + leave_default_values=True, ) class ExampleAnnotated: @@ -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":