Skip to content

Support newline parameter in FURB101 for Python 3.13+#23754

Merged
ntBre merged 3 commits intoastral-sh:mainfrom
xvchris:fix/furb101-newline-py313
Mar 6, 2026
Merged

Support newline parameter in FURB101 for Python 3.13+#23754
ntBre merged 3 commits intoastral-sh:mainfrom
xvchris:fix/furb101-newline-py313

Conversation

@xvchris
Copy link
Contributor

@xvchris xvchris commented Mar 6, 2026

Summary

Fixes #23748.

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. This PR makes the suppression conditional on the target Python version: newline is only rejected for target-version < py313.

Changes

  • helpers.rs: Updated match_open_keywords to allow newline in read mode when target_version >= PY313, mirroring how write mode already conditionally allows newline for >= PY310.
  • FURB101_0.py: Updated comments for the existing newline test cases (now detected as errors since the default test uses PythonVersion::latest()).
  • FURB101_3.py: New test fixture exercising newline with 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.12

Test Plan

cargo insta test -p ruff_linter --accept -- refurb::tests
# 45 passed; 0 failed

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
@astral-sh-bot astral-sh-bot bot requested a review from ntBre March 6, 2026 02:10
@ntBre ntBre added bug Something isn't working rule Implementing or modifying a lint rule preview Related to preview mode features labels Mar 6, 2026
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

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.

@astral-sh-bot
Copy link

astral-sh-bot bot commented Mar 6, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

xvchris and others added 2 commits March 7, 2026 02:19
- 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
Copy link
Contributor

@ntBre ntBre 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!

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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Oops, I see why clippy didn't complain 😅 I'll just put this back.

@ntBre ntBre enabled auto-merge (squash) March 6, 2026 18:44
@ntBre ntBre merged commit 7365268 into astral-sh:main Mar 6, 2026
42 checks passed
@xvchris xvchris deleted the fix/furb101-newline-py313 branch March 11, 2026 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FURB101 does not support the newline parameter in Python 3.13

2 participants