Skip to content

Python: fix(core): keep booleans distinct from numbers inside sets - #8681

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
anishmehta24:fix/filter-values-equal-sets
Sep 24, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
anishmehta24:fix/filter-values-equal-sets

Conversation

@anishmehta24

Copy link
Copy Markdown
Contributor

Motivation & Context

filter_values_equal keeps booleans distinct from numbers for scalars, sequences and mappings, but not for sets — and require_filter_collection accepts sets and frozensets as filter values:

filter_values_equal([True], [1])   # False, correct
filter_values_equal({True}, {1})   # True,  the bug

A set cannot be walked pairwise the way the sequence branch walks a list, so it fell through to plain ==, and {1} == {True} is true in Python because True hashes and compares equal to 1.

It reaches real filtering: _evaluate_filter uses this for eq, ne, in, not_in, contains, contains_any and contains_all, so a record whose field is {1} matched a filter looking for {True}.

Description & Review Guide

  • What are the major changes? A set branch in filter_values_equal that compares members by _set_member_key, which tags bools and recurses through the hashable containers a set can hold (tuples, frozensets). When only one side is a set, native == still decides, so a set never equals a list or a mapping.
  • What is the impact of these changes? {True} no longer equals {1}. Everything else is unchanged: {1} still equals {1.0} (ints and floats stay interchangeable, matching the existing scalar case), and {1} still equals frozenset({1}), which Python compares equal by contents.
  • What do you want reviewers to focus on? Whether set-versus-frozenset should stay equal. I kept Python's own answer for consistency with the "preserve native container semantics" comment on the sequence branch, and added both cases to the table so the choice is pinned either way.

Tests: ten cases added to the existing test_filter_values_equal_preserves_nested_types table, which had no set or frozenset case at all — the bool/int pairs for set, frozenset and a nested tuple, plus the unchanged behaviour ({1}/{1.0}, ordering, length mismatch, empty, set vs frozenset, set vs list). Five fail on main.

test_vectors.py 194 passed. poe fmt, poe lint, poe typing -P core clean.

Related Issue

Fixes #8679

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

🤖 Generated with Claude Code

filter_values_equal distinguishes True from 1 for scalars, sequences and
mappings, but sets fell through to plain `==`, where `{1} == {True}` is
true in Python. require_filter_collection accepts sets, and
_evaluate_filter uses this for eq/ne/in/contains, so a record holding
{1} matched a filter looking for {True}.

Compare set members by a key that marks bools, recursing through the
hashable containers a set can hold. Ints and floats still compare equal,
and a set still equals an equal frozenset.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WyCUJcDPnCnQMmuDwsnDmD

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Sep 23, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anish Mehta (@anishmehta24) The current head is green, and the dedicated full-diff review found no remaining actionable issues with the strict boolean/number set comparison change.

Merged via the queue into microsoft:main with commit 717557a Sep 24, 2026
49 checks passed

This branch was successfully deployed

1 active deployment
github-app-auth — 5c5edb84 Deployed Sep 23, 2026 by anishmehta24 via team_check #5149
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: filter_values_equal equates booleans with numbers inside sets

3 participants