Skip to content

Conversation

@guan404ming
Copy link
Member

Why

Python arrays lack native in operator support, requiring manual iteration to check if a value exists.

How

  • Add ffi.ArrayContains C++ FFI function
  • Implement __contains__ method in Python Array class
  • Add tests and type stub for Python

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @guan404ming, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the Python Array class by adding native support for the in operator. This is achieved by implementing the __contains__ method, which leverages a newly introduced C++ FFI function, ffi.ArrayContains. The change improves the usability and Pythonic nature of array membership checks, making the code cleaner and more intuitive for developers. Comprehensive tests have been added to ensure the correctness and reliability of this new functionality.

Highlights

  • Native 'in' operator support: Introduced native support for the 'in' operator for Python Array objects, addressing the previous lack of direct membership checking without manual iteration.
  • C++ FFI Function: Added a new C++ FFI function, ffi.ArrayContains, which performs the underlying element existence check within arrays.
  • Python __contains__ method: Implemented the __contains__ dunder method in the Python Array class, delegating the check to the new ffi.ArrayContains C++ function.
  • Comprehensive Testing: Included new C++ and Python tests to thoroughly validate the ArrayContains functionality across various scenarios, including integer and string arrays, and edge cases like empty arrays.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds support for the in operator (__contains__) to the Python Array class by introducing a C++ FFI function, ffi.ArrayContains. The overall approach is sound, and the Python-side implementation and tests are well-executed. I have a couple of suggestions to improve the C++ implementation and its corresponding test for better code quality and correctness.

Comment on lines 296 to 341
TEST(Array, Contains) {
Array<int> arr = {1, 2, 3, 4, 5};
AnyEqual eq;

// Test element is present
bool found = false;
for (const auto& elem : *arr.GetArrayObj()) {
if (eq(elem, Any(3))) {
found = true;
break;
}
}
EXPECT_TRUE(found);

// Test element is not present
found = false;
for (const auto& elem : *arr.GetArrayObj()) {
if (eq(elem, Any(10))) {
found = true;
break;
}
}
EXPECT_FALSE(found);

// Test empty array
Array<int> empty_arr;
found = false;
for (const auto& elem : *empty_arr.GetArrayObj()) {
if (eq(elem, Any(1))) {
found = true;
break;
}
}
EXPECT_FALSE(found);

// Test with strings
Array<String> str_arr = {String("hello"), String("world")};
found = false;
for (const auto& elem : *str_arr.GetArrayObj()) {
if (eq(elem, Any(String("world")))) {
found = true;
break;
}
}
EXPECT_TRUE(found);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The TEST(Array, Contains) test case currently re-implements the search logic manually. This means it's testing the search algorithm itself, rather than verifying that the ffi.ArrayContains function is correctly registered and behaves as expected.

To make this a more effective integration test, you should fetch the registered ffi.ArrayContains function from the FFI registry and call it directly. This will ensure the entire FFI pathway is working correctly.

Additionally, the test can be made more concise and readable by removing the repetitive found variable and manual loops.

TEST(Array, Contains) {
  Function f = Function::Get("ffi.ArrayContains");
  ASSERT_TRUE(f.defined());

  Array<int> arr = {1, 2, 3, 4, 5};
  EXPECT_TRUE(f(arr, 3));
  EXPECT_TRUE(f(arr, 1));
  EXPECT_TRUE(f(arr, 5));
  EXPECT_FALSE(f(arr, 10));
  EXPECT_FALSE(f(arr, 0));

  Array<int> empty_arr;
  EXPECT_FALSE(f(empty_arr, 1));

  Array<String> str_arr = {String("hello"), String("world")};
  EXPECT_TRUE(f(str_arr, String("hello")));
  EXPECT_TRUE(f(str_arr, String("world")));
  EXPECT_FALSE(f(str_arr, String("foo")));
}

Comment on lines 73 to 82
.def("ffi.ArrayContains",
[](const ffi::ArrayObj* n, const Any& value) -> bool {
AnyEqual eq;
for (const Any& elem : *n) {
if (eq(elem, value)) {
return true;
}
}
return false;
})
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The implementation of ffi.ArrayContains is correct, but it can be made more concise and idiomatic by using std::any_of from the <algorithm> header. This improves readability and aligns with modern C++ practices. The <algorithm> header is already available through existing includes.

      .def("ffi.ArrayContains",
           [](const ffi::ArrayObj* n, const Any& value) -> bool {
             AnyEqual eq;
             return std::any_of(n->begin(), n->end(),
                                [&](const Any& elem) { return eq(elem, value); });
           })

@guan404ming guan404ming marked this pull request as ready for review January 2, 2026 08:39
Copy link
Member

@junrushao junrushao left a comment

Choose a reason for hiding this comment

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

Looking great!

@junrushao junrushao merged commit 5bc7fcd into apache:main Jan 2, 2026
7 checks passed
@guan404ming guan404ming deleted the feat-array-contains branch January 3, 2026 05:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants