From 7add0973bacc8f6f8a13a69d82bfbc633cd857c7 Mon Sep 17 00:00:00 2001 From: Nicco Kunzmann Date: Wed, 1 Nov 2023 10:24:55 +0000 Subject: [PATCH] do not copy() subcomponents --- src/icalendar/cal.py | 6 ----- src/icalendar/tests/test_equality.py | 38 +++++++++++++--------------- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/icalendar/cal.py b/src/icalendar/cal.py index 4fbe4c4f..9bfed800 100644 --- a/src/icalendar/cal.py +++ b/src/icalendar/cal.py @@ -465,12 +465,6 @@ def __eq__(self, other): return True - def copy(self): - """Create a copy of the component.""" - copy = super().copy() - copy.subcomponents = self.subcomponents[:] - return copy - ####################################### # components defined in RFC 5545 diff --git a/src/icalendar/tests/test_equality.py b/src/icalendar/tests/test_equality.py index 269fdf00..782e4338 100644 --- a/src/icalendar/tests/test_equality.py +++ b/src/icalendar/tests/test_equality.py @@ -15,17 +15,28 @@ def test_parsed_calendars_are_equal_if_parsed_again(ics_file): def test_parsed_calendars_are_equal_if_from_same_source(ics_file): """Ensure that a calendar equals the same calendar.""" - same_calendar = ics_file.__class__.from_ical(ics_file.raw_ics) - assert same_calendar == ics_file - assert not same_calendar != ics_file + cal1 = ics_file.__class__.from_ical(ics_file.raw_ics) + cal2 = ics_file.__class__.from_ical(ics_file.raw_ics) + assert cal1 == cal2 + assert not cal1 != cal2 def test_copies_are_equal(ics_file): """Ensure that copies are equal.""" - assert ics_file.copy() == ics_file.copy() - assert ics_file.copy() == ics_file - assert not ics_file.copy() != ics_file.copy() - assert not ics_file.copy() != ics_file + copy1 = ics_file.copy(); copy1.subcomponents = ics_file.subcomponents + copy2 = ics_file.copy(); copy2.subcomponents = ics_file.subcomponents[:] + assert copy1 == copy2 + assert copy1 == ics_file + assert copy2 == ics_file + assert not copy1 != copy2 + assert not copy1 != ics_file + assert not copy2 != ics_file + + +def test_copy_does_not_copy_subcomponents(calendars): + """If we copy the subcomponents, assumptions around copies will be broken.""" + assert calendars.timezoned.subcomponents + assert not calendars.timezoned.copy().subcomponents def test_deep_copies_are_equal(ics_file): @@ -41,19 +52,6 @@ def test_deep_copies_are_equal(ics_file): pass -def test_a_components_copy_also_copies_subcomponents(calendars): - """A calendar's copy does not have the identical subcompoenets! - - We expect to be able to modify a copy but not its values. - """ - cal = calendars.timezoned - copy = cal.copy() - assert copy is not cal - assert copy.subcomponents - assert copy.subcomponents is not cal.subcomponents - assert copy.subcomponents == cal.subcomponents - - def test_vGeo(): """Check the equality of vGeo.""" assert vGeo(("100", "12.33")) == vGeo(("100.00", "12.330"))