Skip to content

Fix attribute completion for expressions with comparison operators#14898

Merged
krassowski merged 7 commits intoipython:mainfrom
Darshan808:improve-completer
May 24, 2025
Merged

Fix attribute completion for expressions with comparison operators#14898
krassowski merged 7 commits intoipython:mainfrom
Darshan808:improve-completer

Conversation

@Darshan808
Copy link
Copy Markdown
Collaborator

Fixes #14897

Description

Modified _attr_matches() to check if an expression contains comparison operators (=, ==, !=, etc.) outside of parentheses. If so, it extracts the right-hand side of the operator for evaluation, providing appropriate completions.
For parenthesized expressions like (x==y).<tab>, the code preserves the entire expression to ensure it's evaluated as a bool.

Fixing completions fully is quite tricky. The current implementation still has flaws, especially in cases where comparison operators are involved. For example:

{"==", "abc"}.<tab>

In this case, dictionary methods are not shown because the logic incorrectly separates the expression at the "==" operator.

While the current solution resolves some major issues, it also introduces new, albeit minor, ones.

Feedback on better approaches or alternative strategies is highly appreciated.

@Darshan808
Copy link
Copy Markdown
Collaborator Author

@krassowski
Thanks for the feedback. It's working well for the cases I’ve tried so far. Do you happen to have any edge cases in mind that I should test against?

@krassowski
Copy link
Copy Markdown
Member

Do you happen to have any edge cases in mind that I should test against?

I thought about checking against maths operators (+ - * / // % ** @), bitwise operators (& | ^ << >>) and assignment operators (+= -= *= /= //= %= **= @= &= |= ^= >>= <<=).

Given a, b = 1, 2.2 It looks like a + b.<tab> does not work currently, even though it used to work in older IPython versions; similar with other maths operators. Bitwise operators seem to work fine, but I only tested integers.

@Darshan808
Copy link
Copy Markdown
Collaborator Author

Do you think it might be a good idea to strip all operators except the dot operator? It seems to pass all the cases I can think of.

@Darshan808 Darshan808 requested a review from krassowski May 24, 2025 06:11
Copy link
Copy Markdown
Member

@krassowski krassowski left a comment

Choose a reason for hiding this comment

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

Do you think it might be a good idea to strip all operators except the dot operator? It seems to pass all the cases I can think of.

Makes sense. Can you add the a + b to test cases, and then run darker to make the CI pass?

Copy link
Copy Markdown
Member

@krassowski krassowski left a comment

Choose a reason for hiding this comment

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

Thank you @Darshan808!

@krassowski krassowski added this to the 8.37 milestone May 24, 2025
@krassowski krassowski merged commit b854379 into ipython:main May 24, 2025
18 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/ipython that referenced this pull request May 24, 2025
krassowski added a commit that referenced this pull request May 24, 2025
…sions with comparison operators) (#14905)

Backport PR #14898: Fix attribute completion for expressions with
comparison operators
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.

Tab completion fails with comparison operators

2 participants