Skip to content

Use f-string interpolation where possible#12140

Merged
jacobtomlinson merged 1 commit intodask:mainfrom
DimitriPapadopoulos:f-string
Nov 17, 2025
Merged

Use f-string interpolation where possible#12140
jacobtomlinson merged 1 commit intodask:mainfrom
DimitriPapadopoulos:f-string

Conversation

@DimitriPapadopoulos
Copy link
Copy Markdown
Contributor

@DimitriPapadopoulos DimitriPapadopoulos commented Nov 8, 2025

It is faster and in some case more readable than string addition.

All changes applied manually.

  • Passes pre-commit run --all-files

@DimitriPapadopoulos DimitriPapadopoulos changed the title MAINT - use f-string interpolation where possible Use f-string interpolation where possible Nov 8, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Nov 8, 2025

Unit Test Results

See test report for an extended history of previous test failures. This is useful for diagnosing flaky tests.

      9 files  ±0        9 suites  ±0   3h 7m 53s ⏱️ - 1m 49s
 18 154 tests ±0   16 939 ✅ ±0   1 215 💤 ±0  0 ❌ ±0 
162 614 runs  ±0  150 521 ✅ +1  12 093 💤  - 1  0 ❌ ±0 

Results for commit 114fc08. ± Comparison against base commit 1ab0d43.

♻️ This comment has been updated with latest results.

@DimitriPapadopoulos DimitriPapadopoulos force-pushed the f-string branch 3 times, most recently from 7c0db9a to e0e3846 Compare November 8, 2025 11:37
@DimitriPapadopoulos DimitriPapadopoulos marked this pull request as draft November 8, 2025 11:37
@DimitriPapadopoulos DimitriPapadopoulos force-pushed the f-string branch 4 times, most recently from fa90cc2 to 676f12c Compare November 10, 2025 12:50
It is faster and in some case more readable than string addition.
@jacobtomlinson
Copy link
Copy Markdown
Member

This is still a draft, but overall the changes seem fine. Is it ready for review?

@DimitriPapadopoulos DimitriPapadopoulos marked this pull request as ready for review November 12, 2025 12:31
@DimitriPapadopoulos
Copy link
Copy Markdown
Contributor Author

Rebased and ready for review...

Copy link
Copy Markdown
Member

@jacobtomlinson jacobtomlinson left a comment

Choose a reason for hiding this comment

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

+1 for readability generally.

My understanding of the performance differences are that string concatenation of two elements is marginally faster than using an f-string, but if an f-string includes 3 or more variables then it performs better.

a = "A"
b = "B"

c = a + b  # Faster
c = f"{a}{b}"

d = a + b + c
d = f"{a}{b}{c}" # Faster

Overall I think the performance difference is so small it doesn't really matter.

Copy link
Copy Markdown
Member

@jacobtomlinson jacobtomlinson left a comment

Choose a reason for hiding this comment

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

I don't see any changes to linting rules. Was this done by a tool or manually?

@DimitriPapadopoulos
Copy link
Copy Markdown
Contributor Author

DimitriPapadopoulos commented Nov 14, 2025

This was done entirely manually — typically detecting issues with the likes of grep .format(. I'm not happy with this situation, but I take comfort in the thought that linters aren't able to identify these issues yet, but might be able someday.

By the way, the FLY rule static-join-to-f-string (FLY002) is loosely related:

Checks for str.join calls that can be replaced with f-strings.

f-strings are more readable and generally preferred over str.join calls.

Any interest in enabling it?

Copy link
Copy Markdown
Member

@jacobtomlinson jacobtomlinson left a comment

Choose a reason for hiding this comment

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

My primary concern with PRs like this is being friendly to novice users. Generally using modern conventions like f-strings is a novice-friendly thing to do (especially compared to older format strings conventions that are less common). That being said if I'm being completely honest I'm not sure that replacing things like a = "foo-" + b + "-bar" with a = f"foo-{b}-bar" really adds much value for anyone. The performance difference in negligible and IMHO the readability is the same. You spent a bunch of time doing it, I've spent a bunch of time reviewing it, and the project isn't really any different as a result. Given that you've done the work I'm happy to merge it, but I would caution that PRs like this might not be the best use of time for any of us.

It's also possible to write ninja f-strings which border on being unreadable, especially for novice contributors. I don't see any of those in this PR, but I would reject a PR that had them in and favour something more traditional and readable.

Overall I'm +1 on things that linters can catch and fix themselves. With regards to FLY002 (and any other rule for that matter) if a novice user tries to commit something and pre-commit is always shouting at them and they have to fix it themselves that increases the friction to contribute, so I wouldn't be supportive. But if pre-commit runs and auto-fixes a bunch of stuff then I'm totally onboard with that. It sounds like FLY002 can always be auto-fixed so feel free to open a PR to add it.

@jacobtomlinson jacobtomlinson merged commit cd5e429 into dask:main Nov 17, 2025
23 of 24 checks passed
@DimitriPapadopoulos DimitriPapadopoulos deleted the f-string branch November 17, 2025 22:19
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