Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle Python concatenated strings in Rust dependency inference parser #22050

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

tdyas
Copy link
Contributor

@tdyas tdyas commented Mar 6, 2025

As reported in #20324, the Rust-based Python import parser does not handle certain valid source patterns including concatenated strings.

Fix the Rust-based parser to handle concatenated strings correctly. Strings are parsed out of Nodes via the new extract_string helper. The insert_import function is refactored to avoid double parsing of strings.

Added some tests as well for concatenated strings.

@tdyas tdyas changed the title [WIP] handle Python concatenated strings in Rust dependency inference parser handle Python concatenated strings in Rust dependency inference parser Mar 7, 2025
@tdyas tdyas requested review from huonw and tgolsson March 7, 2025 04:44
@tdyas tdyas marked this pull request as ready for review March 7, 2025 04:45
@tdyas tdyas requested a review from benjyw March 7, 2025 05:19
Comment on lines -107 to -109
self.code_at(range)
.trim_start_matches(|c| "rRuUfFbB".contains(c))
.trim_matches(|c| "'\"".contains(c))
Copy link
Member

Choose a reason for hiding this comment

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

Based on your comments, I think you handled the r"...", u"...", f"...", and b"..." based on the nodes in a concatenated string, right? Or at least f-strings.

What about excluding strings that have quote marks? Are you relying on the python-side post processing with isidentifier() to exclude that?

Comment on lines +261 to +262
if !text.contains(|c: char| c.is_ascii_whitespace() || c == '\\')
&& !self.is_pragma_ignored_recursive(node)
Copy link
Member

Choose a reason for hiding this comment

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

I think this would be the place to exclude strings with quote marks or any other expressions we might want to add on the rust side. (See my other comment) Right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:internal CI, fixes for not-yet-released features, etc. needs-cherrypick
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants