mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-17 19:17:47 +00:00
Add t-string fixtures for rules that do not need to be modified (#19146)
I used a script to attempt to identify those rules with the following property: changing f-strings to t-strings in the corresponding fixture altered the number of lint errors emitted. In other words, those rules for which f-strings and t-strings are not treated the same in the current implementation. This PR documents the subset of such rules where this is fine and no changes need to be made to the implementation of the rule. Mostly these are the rules where it is relevant that an f-string evaluates to type `str` at runtime whereas t-strings do not. In theory many of these fixtures are not super necessary - it's unlikely t-strings would be used for most of these. However, the internal handling of t-strings is tightly coupled with that of f-strings, and may become even more so as we implement the upcoming changes due to https://github.com/python/cpython/pull/135996 . So I'd like to keep these around as regression tests. Note: The `flake8-bandit` fixtures were already added during the original t-string implementation. | Rule(s) | Reason | | --- | --- | | [`unused-method-argument` (`ARG002`)](https://docs.astral.sh/ruff/rules/unused-method-argument/#unused-method-argument-arg002) | f-strings exempted for msg in `NotImplementedError` not relevant for t-strings | | [`logging-f-string` (`G004`)](https://docs.astral.sh/ruff/rules/logging-f-string/#logging-f-string-g004) | t-strings cannot be used here | | [`f-string-in-get-text-func-call` (`INT001`)](https://docs.astral.sh/ruff/rules/f-string-in-get-text-func-call/#f-string-in-get-text-func-call-int001) | rule justified by eager evaluation of interpolations | | [`flake8-bandit`](https://docs.astral.sh/ruff/rules/#flake8-bandit-s)| rules justified by eager evaluation of interpolations | | [`single-string-slots` (`PLC0205`)](https://docs.astral.sh/ruff/rules/single-string-slots/#single-string-slots-plc0205) | t-strings cannot be slots in general | | [`unnecessary-encode-utf8` (`UP012`)](https://docs.astral.sh/ruff/rules/unnecessary-encode-utf8/#unnecessary-encode-utf8-up012) | cannot encode t-strings | | [`no-self-use` (`PLR6301`)](https://docs.astral.sh/ruff/rules/no-self-use/#no-self-use-plr6301) | f-strings exempted for msg in NotImplementedError not relevant for t-strings | | [`pytest-raises-too-broad` (`PT011`)](https://docs.astral.sh/ruff/rules/pytest-raises-too-broad/) / [`pytest-fail-without-message` (`PT016`)](https://docs.astral.sh/ruff/rules/pytest-fail-without-message/#pytest-fail-without-message-pt016) / [`pytest-warns-too-broad` (`PT030`)](https://docs.astral.sh/ruff/rules/pytest-warns-too-broad/#pytest-warns-too-broad-pt030) | t-strings cannot be empty or used as messages | | [`assert-on-string-literal` (`PLW0129`)](https://docs.astral.sh/ruff/rules/assert-on-string-literal/#assert-on-string-literal-plw0129) | t-strings are not strings and cannot be empty | | [`native-literals` (`UP018`)](https://docs.astral.sh/ruff/rules/native-literals/#native-literals-up018) | t-strings are not native literals |
This commit is contained in:
parent
8a217e5920
commit
2a2cc37158
19 changed files with 112 additions and 3 deletions
|
|
@ -1,6 +1,5 @@
|
|||
---
|
||||
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
|
||||
snapshot_kind: text
|
||||
---
|
||||
B018.py:11:5: B018 Found useless expression. Either assign it to a variable or remove it.
|
||||
|
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
---
|
||||
source: crates/ruff_linter/src/rules/flake8_gettext/mod.rs
|
||||
snapshot_kind: text
|
||||
---
|
||||
INT001.py:1:3: INT001 f-string is resolved before function call; consider `_("string %s") % arg`
|
||||
|
|
||||
1 | _(f"{'value'}")
|
||||
| ^^^^^^^^^^^^ INT001
|
||||
2 |
|
||||
3 | # Don't trigger for t-strings
|
||||
|
|
||||
|
|
|
|||
|
|
@ -52,4 +52,6 @@ G004.py:15:6: G004 Logging statement uses f-string
|
|||
14 | info(f"{name}")
|
||||
15 | info(f"{__name__}")
|
||||
| ^^^^^^^^^^^^^ G004
|
||||
16 |
|
||||
17 | # Don't trigger for t-strings
|
||||
|
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
---
|
||||
source: crates/ruff_linter/src/rules/flake8_pytest_style/mod.rs
|
||||
snapshot_kind: text
|
||||
---
|
||||
PT016.py:19:5: PT016 No message passed to `pytest.fail()`
|
||||
|
|
||||
|
|
@ -67,4 +66,6 @@ PT016.py:25:5: PT016 No message passed to `pytest.fail()`
|
|||
24 | pytest.fail(reason="")
|
||||
25 | pytest.fail(reason=f"")
|
||||
| ^^^^^^^^^^^ PT016
|
||||
26 |
|
||||
27 | # Skip for t-strings
|
||||
|
|
||||
|
|
|
|||
|
|
@ -66,3 +66,13 @@ ARG.py:216:24: ARG002 Unused method argument: `x`
|
|||
| ^ ARG002
|
||||
217 | print("Hello, world!")
|
||||
|
|
||||
|
||||
ARG.py:255:20: ARG002 Unused method argument: `y`
|
||||
|
|
||||
253 | ###
|
||||
254 | class C:
|
||||
255 | def f(self, x, y):
|
||||
| ^ ARG002
|
||||
256 | """Docstring."""
|
||||
257 | msg = t"{x}..."
|
||||
|
|
||||
|
|
|
|||
|
|
@ -75,3 +75,21 @@ no_self_use.py:140:9: PLR6301 Method `unused_message_2` could be a function, cla
|
|||
141 | msg = ""
|
||||
142 | raise NotImplementedError(x)
|
||||
|
|
||||
|
||||
no_self_use.py:145:9: PLR6301 Method `developer_greeting` could be a function, class method, or static method
|
||||
|
|
||||
144 | class TPerson:
|
||||
145 | def developer_greeting(self, name): # [no-self-use]
|
||||
| ^^^^^^^^^^^^^^^^^^ PLR6301
|
||||
146 | print(t"Greetings {name}!")
|
||||
|
|
||||
|
||||
no_self_use.py:151:9: PLR6301 Method `tstring` could be a function, class method, or static method
|
||||
|
|
||||
149 | print(t"Hello from {self.name} !")
|
||||
150 |
|
||||
151 | def tstring(self, x):
|
||||
| ^^^^^^^ PLR6301
|
||||
152 | msg = t"{x}"
|
||||
153 | raise NotImplementedError(msg)
|
||||
|
|
||||
|
|
|
|||
|
|
@ -576,6 +576,8 @@ UP012.py:86:5: UP012 [*] Unnecessary call to `encode` as UTF-8
|
|||
85 | # Refer: https://github.com/astral-sh/ruff/issues/11736
|
||||
86 | x: '"foo".encode("utf-8")'
|
||||
| ^^^^^^^^^^^^^^^^^^^^^ UP012
|
||||
87 |
|
||||
88 | # AttributeError for t-strings so skip lint
|
||||
|
|
||||
= help: Rewrite as bytes literal
|
||||
|
||||
|
|
@ -585,3 +587,6 @@ UP012.py:86:5: UP012 [*] Unnecessary call to `encode` as UTF-8
|
|||
85 85 | # Refer: https://github.com/astral-sh/ruff/issues/11736
|
||||
86 |-x: '"foo".encode("utf-8")'
|
||||
86 |+x: 'b"foo"'
|
||||
87 87 |
|
||||
88 88 | # AttributeError for t-strings so skip lint
|
||||
89 89 | (t"foo{bar}").encode("utf-8")
|
||||
|
|
|
|||
|
|
@ -660,6 +660,7 @@ UP018.py:90:1: UP018 [*] Unnecessary `int` call (rewrite as a literal)
|
|||
90 |+1 and None
|
||||
91 91 | float(1.)and None
|
||||
92 92 | bool(True)and()
|
||||
93 93 |
|
||||
|
||||
UP018.py:91:1: UP018 [*] Unnecessary `float` call (rewrite as a literal)
|
||||
|
|
||||
|
|
@ -678,6 +679,8 @@ UP018.py:91:1: UP018 [*] Unnecessary `float` call (rewrite as a literal)
|
|||
91 |-float(1.)and None
|
||||
91 |+1. and None
|
||||
92 92 | bool(True)and()
|
||||
93 93 |
|
||||
94 94 |
|
||||
|
||||
UP018.py:92:1: UP018 [*] Unnecessary `bool` call (rewrite as a literal)
|
||||
|
|
||||
|
|
@ -694,3 +697,6 @@ UP018.py:92:1: UP018 [*] Unnecessary `bool` call (rewrite as a literal)
|
|||
91 91 | float(1.)and None
|
||||
92 |-bool(True)and()
|
||||
92 |+True and()
|
||||
93 93 |
|
||||
94 94 |
|
||||
95 95 | # t-strings are not native literals
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue