Skip to content

gh-155102: Preserve TypeError raised during set discard/remove comparison - #155104

Closed
BHUVANSH855 wants to merge 2 commits into
python:mainfrom
BHUVANSH855:fix-0120-set-discard-typeerror
Closed

gh-155102: Preserve TypeError raised during set discard/remove comparison#155104
BHUVANSH855 wants to merge 2 commits into
python:mainfrom
BHUVANSH855:fix-0120-set-discard-typeerror

Conversation

@BHUVANSH855

@BHUVANSH855 BHUVANSH855 commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Fixes issue gh-155102.

set.discard() and set.remove() could incorrectly suppress or replace
exceptions raised during equality comparison when the lookup key was a
hashable subclass of set.

The issue was caused by the special set/frozenset fallback path treating
all TypeError cases from hashing as unhashable-key handling. This could
clear an exception raised by __eq__ and retry the lookup, resulting in
different behavior compared to normal membership testing.

This change separates the hash failure path from comparison errors:

  • Preserve the existing fallback behavior for unhashable set/frozenset
    keys.
  • Propagate exceptions raised during equality comparison consistently from
    set.discard() and set.remove().

A regression test has been added to cover a hashable set subclass whose
__eq__ raises TypeError.

Tests:

  • ./python -m test test_set

@ZeroIntensity

Copy link
Copy Markdown
Member

See my comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants