GH-93521: For dataclasses, filter out __weakref__ slot if present in bases (GH-93535)

(cherry picked from commit 5849af7a80)

Co-authored-by: Bluenix <bluenixdev@gmail.com>
This commit is contained in:
Miss Islington (bot) 2022-06-07 18:17:56 -07:00 committed by GitHub
parent f26d1b5b53
commit 121ab58e03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 4 deletions

View file

@ -1156,11 +1156,16 @@ def _add_slots(cls, is_frozen, weakref_slot):
itertools.chain.from_iterable(map(_get_slots, cls.__mro__[1:-1]))
)
# The slots for our class. Remove slots from our base classes. Add
# '__weakref__' if weakref_slot was given.
# '__weakref__' if weakref_slot was given, unless it is already present.
cls_dict["__slots__"] = tuple(
itertools.chain(
itertools.filterfalse(inherited_slots.__contains__, field_names),
("__weakref__",) if weakref_slot else ())
itertools.filterfalse(
inherited_slots.__contains__,
itertools.chain(
# gh-93521: '__weakref__' also needs to be filtered out if
# already present in inherited_slots
field_names, ('__weakref__',) if weakref_slot else ()
)
),
)
for field_name in field_names: