Support newline parameter in FURB101 for Python 3.13+#23754
Merged
ntBre merged 3 commits intoastral-sh:mainfrom Mar 6, 2026
Merged
Support newline parameter in FURB101 for Python 3.13+#23754ntBre merged 3 commits intoastral-sh:mainfrom
newline parameter in FURB101 for Python 3.13+#23754ntBre merged 3 commits intoastral-sh:mainfrom
Conversation
Python 3.13 added the `newline` parameter to `pathlib.Path.read_text()`, so `open(..., newline=...) + read()` can now be replaced by `Path(...).read_text(newline=...)` when targeting Python 3.13 or later. Previously, the presence of `newline` in read mode would unconditionally suppress the FURB101 diagnostic. Now it is only suppressed for target-version < py313. Fixes astral-sh#23748
ntBre
approved these changes
Mar 6, 2026
Contributor
ntBre
left a comment
There was a problem hiding this comment.
Thanks! This looks great to me overall, I just had one nit about collapsing an if and a suggestion about a slightly different test setup.
|
- Collapse nested `if read_mode { if target_version < PY313` into
`if read_mode && target_version < PY313` as suggested
- Replace two separate version-specific tests with a single
`assert_diagnostics_diff!` test comparing PY313 vs PY312 behavior
- Update snapshots accordingly
ntBre
approved these changes
Mar 6, 2026
| if read_mode && target_version < PythonVersion::PY313 { | ||
| // `pathlib.Path.read_text` doesn't support `newline` until Python 3.13. | ||
| return None; | ||
| } else if !read_mode && target_version < PythonVersion::PY310 { |
Contributor
There was a problem hiding this comment.
Oops, I see why clippy didn't complain 😅 I'll just put this back.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #23748.
Python 3.13 added the
newlineparameter topathlib.Path.read_text(), soopen(..., newline=...) + read()can now be replaced byPath(...).read_text(newline=...)when targeting Python 3.13 or later.Previously, the presence of
newlinein read mode would unconditionally suppress the FURB101 diagnostic. This PR makes the suppression conditional on the target Python version:newlineis only rejected fortarget-version < py313.Changes
helpers.rs: Updatedmatch_open_keywordsto allownewlinein read mode whentarget_version >= PY313, mirroring how write mode already conditionally allowsnewlinefor>= PY310.FURB101_0.py: Updated comments for the existing newline test cases (now detected as errors since the default test usesPythonVersion::latest()).FURB101_3.py: New test fixture exercisingnewlinewith various values ("\r\n","",None).mod.rs: Added two new tests:read_whole_file_newline_python_313— verifies FURB101 fires on Python 3.13+read_whole_file_newline_python_312— verifies FURB101 is suppressed on Python 3.12Test Plan