Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Stdlib] Use PySequence_Contains for Python membership#6609

Open
puneetdixit200 wants to merge 5 commits into
modular:mainfrom
puneetdixit200:fix-3806-pysequence-contains
Open

[Stdlib] Use PySequence_Contains for Python membership#6609
puneetdixit200 wants to merge 5 commits into
modular:mainfrom
puneetdixit200:fix-3806-pysequence-contains

Conversation

@puneetdixit200

Copy link
Copy Markdown

Linked issue

Fixes #3806

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • Performance improvement (includes benchmark results below)
  • Documentation update
  • New feature or public API (requires prior proposal or issue approval)
  • Refactor / internal cleanup (no user-visible change)
  • Build, CI, or tooling change

Motivation

PythonObject.__contains__ currently implements membership in Mojo by checking __contains__ and otherwise iterating. The linked issue and maintainer guidance call for delegating membership to CPython through PySequence_Contains, which uses CPython sequence slots and fallback behavior directly.

What changed

  • Added a PySequence_Contains CPython binding, loader field, and wrapper.
  • Replaced PythonObject.__contains__ with a single PySequence_Contains call and Python error propagation on -1.
  • Added coverage for the raw binding, list/dict membership, a Python __contains__ implementation, iterator fallback, and non-iterable error propagation.

Testing

  • git diff --check HEAD~1..HEAD
  • ./bazelw test //mojo/stdlib/test/python:test_python_object.mojo.test //mojo/stdlib/test/python:test_python_cpython.mojo.test could not run on this machine because Bazel exits before target analysis: error: a fully Xcode install must be selected globally, run sudo xcode-select -s /path/to/Xcode.app. This machine only has /Library/Developer/CommandLineTools selected and no /Applications/Xcode*.app.
  • ./bazelw run format is blocked by the same Xcode preflight error.

Checklist

  • The linked issue above has been reviewed by a maintainer and is agreed-upon, or this is a trivial fix that does not need prior approval
  • PR is small and focused -- I have split larger changes into a sequence of smaller PRs where possible
  • I ran ./bazelw run format to format my changes
  • I added or updated tests to cover my changes
  • If AI tools assisted with this contribution, I have included an Assisted-by: trailer in my commit message or this PR description

Assisted-by: AI

BEGIN_PUBLIC
[Stdlib] Use PySequence_Contains for Python membership

Bind CPython PySequence_Contains and use it for PythonObject membership checks so list, dict, custom __contains__, and iterator fallback behavior stays delegated to CPython.

Add Python interop coverage for the raw binding, __contains__, iterator fallback, and non-iterable error propagation.
END_PUBLIC

Assisted-by: AI
Signed-off-by: Puneet Dixit <[email protected]>
@puneetdixit200 puneetdixit200 requested a review from a team as a code owner May 26, 2026 11:47
Copilot AI review requested due to automatic review settings May 26, 2026 11:47

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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR updates Mojo’s Python interop membership testing (in) to use CPython’s PySequence_Contains, and expands test coverage to validate correct behavior across __contains__ and __iter__ paths.

Changes:

  • Implement PythonObject.__contains__ via PySequence_Contains with proper error propagation.
  • Add a CPython binding/wrapper for PySequence_Contains.
  • Extend tests for membership semantics and adjust an overly-specific error substring assertion.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
mojo/stdlib/test/python/test_python_object.mojo Adds new membership tests for __contains__ and __iter__; loosens brittle error message match.
mojo/stdlib/test/python/test_python_cpython.mojo Adds assertions covering PySequence_Contains results before/after list mutation.
mojo/stdlib/std/python/python_object.mojo Replaces manual membership emulation with PySequence_Contains and raises on -1.
mojo/stdlib/std/python/_cpython.mojo Introduces dynamic binding + CPython wrapper method for PySequence_Contains.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread mojo/stdlib/std/python/python_object.mojo
Add coverage that a Python __contains__ exception propagates through Mojo membership.

Assisted-by: AI
Signed-off-by: Puneet Dixit <[email protected]>
@JoeLoser JoeLoser self-assigned this May 28, 2026
Comment thread mojo/stdlib/test/python/test_python_object.mojo Outdated
Comment thread mojo/stdlib/test/python/test_python_object.mojo Outdated
Comment thread mojo/stdlib/test/python/test_python_object.mojo Outdated
@JoeLoser JoeLoser added waiting for response Needs action/response from contributor before a PR can proceed Team: Mojo Libraries Relevant to the Mojo libraries team, which will review and decide on the issue or PR. labels May 28, 2026
Tighten the non-iterable membership assertion, broaden the test name, and keep the embedded Python source runtime-only.

Assisted-by: AI

Signed-off-by: Puneet Dixit <[email protected]>
@github-actions github-actions Bot added mojo-stdlib Tag for issues related to standard library waiting-on-review labels May 28, 2026
@github-actions

Copy link
Copy Markdown


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@puneetdixit200

Copy link
Copy Markdown
Author

Pushed cab7db0e9e84 with the review follow-up.

Changes:

  • tightened the non-iterable assertion to argument of type 'int' is not iterable\n- renamed test_contains_dunder to test_contains now that it covers dunder, iterator fallback, and propagation cases\n- changed CONTAINS_SOURCE from comptime to runtime var\n\nLocal verification: git diff --check. I still cannot run the stdlib test locally in this environment because the Mojo/Bazel/Pixi toolchain is not available here, so CI should validate the Mojo test run.

Apply the formatter's single-line style for the PySequence_Contains loader.

Assisted-by: AI

Signed-off-by: Puneet Dixit <[email protected]>
@puneetdixit200

Copy link
Copy Markdown
Author

Follow-up after CI lint: pushed 2f3e5e1dddf0 to apply the formatter diff for PySequence_Contains.load(self.lib.borrow()).

Current status: review threads are resolved, PR remains approved, lint/title/target/label checks pass. Build and Test on large-oss-linux is still pending, and CLAAssistant is failing on the external CLA signature gate.

Match the stable prefix of the non-container membership error so the test covers both current Python 3.14 wording and older Python wording.

Assisted-by: AI
Signed-off-by: Puneet Dixit <[email protected]>
@puneetdixit200

Copy link
Copy Markdown
Author

Pushed 2944d30234 to fix the branch-owned stdlib failure from the large build: Python 3.14 reports the int membership error as argument of type 'int' is not a container or iterable, so the test now matches the stable prefix shared with older Python wording.

Verification:

  • git diff --check

I could not run the Mojo/Bazel test locally because this environment has no mojo, pixi, bazel, or bazelisk executable. The other failed row in the previous large build was the unrelated max/tests/integration/cli:test_pipelines_cli_help_3.11 performance threshold at 1050.4 ms vs 1000 ms.

@JoeLoser JoeLoser removed the Team: Mojo Libraries Relevant to the Mojo libraries team, which will review and decide on the issue or PR. label May 28, 2026
@JoeLoser

Copy link
Copy Markdown
Member

Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.

I have read the CLA Document and I hereby sign the CLA

You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

Hi @puneetdixit200 - you'll need to sign our CLA before I can accept this contribution, please see the above.

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

Labels

mojo-stdlib Tag for issues related to standard library waiting for response Needs action/response from contributor before a PR can proceed waiting-on-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Optimize PythonObject.__contains__ with a CPython API function

3 participants