From 234196d0531cbe3d4e08be92ebf7bf5a1e819d0b Mon Sep 17 00:00:00 2001 From: Matthew Smith Date: Thu, 20 Mar 2025 12:36:41 -0500 Subject: [PATCH 1/2] don't duplicate taggable object when trying to add already-existing tag --- pytools/tag.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pytools/tag.py b/pytools/tag.py index afb9557b..9089d8f8 100644 --- a/pytools/tag.py +++ b/pytools/tag.py @@ -276,8 +276,11 @@ def tagged(self, tags: ToTagSetConvertible) -> Self: :arg tags: An instance of :class:`~pytools.tag.Tag` or an iterable with instances therein. """ - return self._with_new_tags( - tags=check_tag_uniqueness(normalize_tags(tags) | self.tags)) + new_tags = check_tag_uniqueness(normalize_tags(tags) | self.tags) + if new_tags != self.tags: + return self._with_new_tags(tags=new_tags) + else: + return self def without_tags(self, tags: ToTagSetConvertible, verify_existence: bool = True @@ -297,7 +300,10 @@ def without_tags(self, if verify_existence and len(new_tags) > len(self.tags) - len(to_remove): raise ValueError("A tag specified for removal was not present.") - return self._with_new_tags(tags=check_tag_uniqueness(new_tags)) + if new_tags != self.tags: + return self._with_new_tags(tags=check_tag_uniqueness(new_tags)) + else: + return self @memoize_method def tags_of_type(self, tag_t: type[TagT]) -> frozenset[TagT]: From 82d4e00812e1036ea74c8cfed89cde609c1f6ada Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Thu, 27 Mar 2025 17:21:55 -0500 Subject: [PATCH 2/2] Use cheaper no-op check in Taggable.tagged/without_tags --- pytools/tag.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pytools/tag.py b/pytools/tag.py index 9089d8f8..be620365 100644 --- a/pytools/tag.py +++ b/pytools/tag.py @@ -276,11 +276,12 @@ def tagged(self, tags: ToTagSetConvertible) -> Self: :arg tags: An instance of :class:`~pytools.tag.Tag` or an iterable with instances therein. """ - new_tags = check_tag_uniqueness(normalize_tags(tags) | self.tags) - if new_tags != self.tags: - return self._with_new_tags(tags=new_tags) - else: + normalized = normalize_tags(tags) + new_tags = check_tag_uniqueness(normalized | self.tags) + if normalized <= self.tags: return self + else: + return self._with_new_tags(tags=new_tags) def without_tags(self, tags: ToTagSetConvertible, verify_existence: bool = True @@ -300,7 +301,7 @@ def without_tags(self, if verify_existence and len(new_tags) > len(self.tags) - len(to_remove): raise ValueError("A tag specified for removal was not present.") - if new_tags != self.tags: + if to_remove & self.tags: return self._with_new_tags(tags=check_tag_uniqueness(new_tags)) else: return self