Skip to content

Introduce ImplicitConcat - #4411

Closed
harupy wants to merge 2 commits into
RustPython:mainfrom
harupy:implicit-concatenation
Closed

harupy wants to merge 2 commits into
RustPython:mainfrom
harupy:implicit-concatenation

Conversation

@harupy

@harupy harupy commented Jan 3, 2023 •

Copy link
Copy Markdown
Contributor

I don't think this proposal is acceptable because the proposed node doesn't exist in Python. I just wanted to hear thoughts on it. This PR adds a new AST node ImplicitConcat that represents implicit concatenation (e.g. "a" "b"). Currently, implicitly-concatenated strings are merged into a single Constant or JoinedStr node. This makes it difficult to analyze each string component. Here's an example:

(
  f"a"    # useless f-string
  ""      # useless empty string
  f"{x}"
)

This code is parsed as:

JoinedStr {
  values: [
    Constant,
    FormattedValue
  ]
}

This is equivalent to f"a{x}", which is a valid f-string.

If the python code above was parsed as:

ImplicitConcat {
  values: [
    JoinedStr { values: [Constant] },
    Constnat,
    JoinedStr { values: [FormattedValue] },
  ]
}

that would allow a linter such as Ruff to analyze each token and provide more helpful errors.

The following video is a quick demo that utilizes ImplicitConcat:

Screen.Recording.2023-01-03.at.15.50.09.mov

@harupy harupy closed this Jan 3, 2023
@DimitrisJim

Copy link
Copy Markdown
Member

I can see the idea here. How do other linters deal with this? Have you looked up if this has been proposed on python-dev or python-ideas?

@harupy

harupy commented Jan 3, 2023 •

Copy link
Copy Markdown
Contributor Author

@DimitrisJim Thanks for the comment!

How do other linters deal with this?

I think other linters just analyze a merged string.

Have you looked up if this has been proposed on python-dev or python-ideas?

I haven't yet.

@harupy

harupy commented Jan 3, 2023 •

Copy link
Copy Markdown
Contributor Author

@harupy

harupy commented Jan 3, 2023

Copy link
Copy Markdown
Contributor Author

pyright seems to have a node for an implicitly-concatenated string (StringList): https://github.com/microsoft/pyright/search?q=StringList

@harupy

harupy commented Jan 3, 2023 •

Copy link
Copy Markdown
Contributor Author

It's actually possible to break down an implicitly-concatenated string, but it requires re-tokenization and re-parsing, which can degrade the performance.

a = "a" "b"
    ^^^^^^^

# 1. By tokenizing the ^^^ range, we can get two string tokens.
# 2. By parsing each string token, we can get two Constant nodes.

@DimitrisJim

Copy link
Copy Markdown
Member

which can degrade the performance.

yeah I thought that would be the issue. Not sure why the implicit joining is done before the AST phase, there could be some reason behind it which I'm unaware of. In general, I'd probably ask around other projects to see if something like this would be a feature many would find useful, then a proposal might be made to CPython.

If that doesn't lead anywhere, sealing it behind a feature might be acceptable but @youknowone and @coolreader18 would have to give the ok for it.

@youknowone

Copy link
Copy Markdown
Member

To change ast, as @DimitrisJim said, I think a proposal for CPython would be the best option. Now language developers understand tooling support is really important for the language ecosystem. It will be worth.

In short term, I am open to add features to support toolings. But I also think we have to keep the update cost as small as possible. I'd like to accept it if the patch can make ast updating easy enough.
e.g. we might need to do something like #4042 in future again.

@harupy

harupy commented Jan 4, 2023

Copy link
Copy Markdown
Contributor Author

@youknowone Thanks for the comment!

But I also think we have to keep the update cost as small as possible. I'd like to accept it if the patch can make ast updating easy enough.

Yeah, this is what I'm concerned the most. I don't want this node to be a blocker for RustPython's development.

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.

3 participants