feat: extend f-string rules to t-strings (#3613) - #3702
Conversation
| """Used to define `TemplateStr` nodes in `python3.14+`.""" | ||
|
|
||
| values: list[ast.expr] | ||
| values: list[ast.expr] # noqa: WPS110 |
There was a problem hiding this comment.
Please do not suppress the WPS rules, look at https://github.com/wemake-services/wemake-python-styleguide/blob/master/wemake_python_styleguide/constants.py
|
|
||
| @final | ||
| class WrongFormatStringVisitor(base.BaseNodeVisitor): | ||
| class WrongFormatStringVisitor(base.BaseNodeVisitor): # noqa: WPS214 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3702 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 369 371 +2
Lines 12425 12556 +131
Branches 858 870 +12
==========================================
+ Hits 12425 12556 +131 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
yangfan-yf-yf
left a comment
There was a problem hiding this comment.
The 3.10–3.13 jobs are failing only because the new Python 3.14-only paths reduce total coverage below the repository's required 100% threshold.
The job output identifies the skipped t-string test bodies in test_formatted_string.py (lines 280–285 and 302–307), plus the new visit_tstring_start methods in comments.py and primitives.py. Those methods cannot execute before Python 3.14, while the existing version-specific coverage convention already uses covdefaults pragmas such as # pragma: >=3.12 cover.
Please mark the t-string-only test functions and the two visit_tstring_start methods with the corresponding # pragma: >=3.14 cover condition (or use an equivalent existing version-gated test layout). That keeps the 3.14 execution covered while excluding paths that older interpreters cannot reach. The 3.14 job already passes; this should restore the 3.10–3.13 matrix without weakening the coverage requirement.
|
Added This excludes these 3.14-only execution paths on Python 3.10–3.13 while keeping 100% coverage requirement intact on 3.14+. |
|
Updated the testing suite and violation documentation:
|
| "foo = t'test {a} # testing'", | ||
| ], | ||
| ) | ||
| def test_correct_t_string_comments( # pragma: >=3.14 cover |
There was a problem hiding this comment.
here we should do the same with PREFIXES parametrization :)
| assert_errors(visitor, [MultilineFormattedStringViolation]) | ||
|
|
||
|
|
||
| @pytest.mark.skipif( |
| @@ -19,11 +20,15 @@ def is_doc_string(node: ast.AST) -> bool: | |||
|
|
|||
|
|
|||
| def has_fstring_conversion(component: ast.AST) -> bool: | |||
There was a problem hiding this comment.
This needs to be renamed, it is not about fstring only anymore.
| """Performs check for t-strings.""" | ||
| self._check_fstring_is_multi_lined(token) | ||
|
|
||
| def _check_fstring_is_multi_lined(self, token: tokenize.TokenInfo) -> None: |
There was a problem hiding this comment.
this should also be renamed, _check_fstring_is_multi_lined name is not correct anymore, it is not just about fstring now
| Don't write comments inside formatted strings. | ||
|
|
||
| Example:: | ||
|
|
There was a problem hiding this comment.
Please don't forget to update the versionchanged entries for all violations that were changed :)
Extend style rules for f-strings to t-strings (Python 3.14+)
Checklist
CHANGELOG.mdRelated issues
Summary of Changes
Python 3.14 introduces Template Strings (
t-strings) via PEP 750. Sincet-stringsshare formatting and interpolation syntax withf-strings, this PR extends our existing formatted string style rules to covert-stringsas well.1. AST Compatibility Shims (
compat/nodes.py)ast.TemplateStrandast.Interpolation. On Python versions< 3.14, these fallback to safe stub classes inheriting fromast.exprso type checking and AST traversal work cleanly across all Python versions without raisingAttributeError.2. AST Visitor Extensions (
visitors/ast/)builtins.py: Addedvisit_TemplateStrhandler and generalized_check_complex_formatted_stringto inspect bothast.JoinedStrandnodes.TemplateStr, enforcing complexity restrictions (WPS237) ont-stringinterpolation expressions.complexity/jones.py: AddedTemplateStrandInterpolationto ignored node types so internal formatting nodes do not inflate inline Jones line complexity scores.complexity/overuses.py: Updated_check_string_constantto ignore string constants nested insidenodes.TemplateStr.operators.py: AddedTemplateStrto string classes for math operator checking.3. Tokenize Visitor Extensions (
visitors/tokenize/)primitives.py: Addedvisit_tstring_starthandler and updated_multiline_fstring_patternregex to matchfandtstring prefixes ((?:[ft]r?|r[ft])(['"])), enforcing triple-quote requirements for multiline formatted strings (WPS479).comments.py: Addedvisit_tstring_starthandler and updated_comment_in_fstringregex to forbid inline comments insidet-stringexpressions (WPS480).4. Violation Documentation (
violations/)TooComplexFormattedStringViolation), WPS479 (MultilineFormattedStringViolation), and WPS480 (CommentInFormattedStringViolation) to explicitly state they forbid complex expressions, multiline quotes, and inline comments in bothf-stringsandt-strings.5. Automated Testing (
tests/)t-stringusages across AST and tokenize visitors intest_formatted_string.py,test_mulitiline_formatted_string312.py, andtest_comment_in_formatted_string312.py.t-stringtests with@pytest.mark.skipif(sys.version_info < (3, 14), reason='t-strings are only in Python 3.14+').🙏 Please, if you or your company is finding wemake-python-styleguide valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/wemake-python-styleguide. As a thank you, your profile/company logo will be added to our main README which receives hundreds of unique visitors per day.