Commit 10bda3d
authored
## Summary
Type default for Type parameter was added in Python 3.13 (PEP 696).
`typing_extensions.TypeVar` backports the default argument to earlier
versions.
`UP046` & `UP047` were getting triggered when
`typing_extensions.TypeVar` with `default` argument was used on python
version < 3.13
It shouldn't be triggered for python version < 3.13
This commit fixes the bug by adding a python version check before
triggering them.
Fixes #20929.
## Test Plan
### Manual testing 1
As the issue author pointed out in
#20929 (comment),
ran the following on `main` branch:
> % cargo run -p ruff -- check ../efax/ --target-version py312
--no-cache
<details><summary>Output</summary>
```zsh
Compiling ruff_linter v0.14.1 (/Users/prakhar/ruff/crates/ruff_linter)
Compiling ruff v0.14.1 (/Users/prakhar/ruff/crates/ruff)
Compiling ruff_graph v0.1.0 (/Users/prakhar/ruff/crates/ruff_graph)
Compiling ruff_workspace v0.0.0 (/Users/prakhar/ruff/crates/ruff_workspace)
Compiling ruff_server v0.2.2 (/Users/prakhar/ruff/crates/ruff_server)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 6.72s
Running `target/debug/ruff check ../efax/ --target-version py312 --no-cache`
UP046 Generic class `ExpectationParametrization` uses `Generic` subclass instead of type parameters
--> /Users/prakhar/efax/efax/_src/expectation_parametrization.py:17:48
|
17 | class ExpectationParametrization(Distribution, Generic[NP]):
| ^^^^^^^^^^^
18 | """The expectation parametrization of an exponential family distribution.
|
help: Use type parameters
UP046 Generic class `ExpToNat` uses `Generic` subclass instead of type parameters
--> /Users/prakhar/efax/efax/_src/mixins/exp_to_nat/exp_to_nat.py:27:68
|
26 | @DataClass
27 | class ExpToNat(ExpectationParametrization[NP], SimpleDistribution, Generic[NP]):
| ^^^^^^^^^^^
28 | """This mixin implements the conversion from expectation to natural parameters.
|
help: Use type parameters
UP046 Generic class `HasEntropyEP` uses `Generic` subclass instead of type parameters
--> /Users/prakhar/efax/efax/_src/mixins/has_entropy.py:25:20
|
23 | HasEntropy,
24 | JaxAbstractClass,
25 | Generic[NP]):
| ^^^^^^^^^^^
26 | @abstract_jit
27 | @AbstractMethod
|
help: Use type parameters
UP046 Generic class `HasEntropyNP` uses `Generic` subclass instead of type parameters
--> /Users/prakhar/efax/efax/_src/mixins/has_entropy.py:64:20
|
62 | class HasEntropyNP(NaturalParametrization[EP],
63 | HasEntropy,
64 | Generic[EP]):
| ^^^^^^^^^^^
65 | @jit
66 | @Final
|
help: Use type parameters
UP046 Generic class `NaturalParametrization` uses `Generic` subclass instead of type parameters
--> /Users/prakhar/efax/efax/_src/natural_parametrization.py:43:30
|
41 | class NaturalParametrization(Distribution,
42 | JaxAbstractClass,
43 | Generic[EP, Domain]):
| ^^^^^^^^^^^^^^^^^^^
44 | """The natural parametrization of an exponential family distribution.
|
help: Use type parameters
UP046 Generic class `Structure` uses `Generic` subclass instead of type parameters
--> /Users/prakhar/efax/efax/_src/structure/structure.py:31:17
|
30 | @DataClass
31 | class Structure(Generic[P]):
| ^^^^^^^^^^
32 | """This class generalizes the notion of type for Distribution objects.
|
help: Use type parameters
UP046 Generic class `DistributionInfo` uses `Generic` subclass instead of type parameters
--> /Users/prakhar/efax/tests/distribution_info.py:20:24
|
20 | class DistributionInfo(Generic[NP, EP, Domain]):
| ^^^^^^^^^^^^^^^^^^^^^^^
21 | def __init__(self, dimensions: int = 1, safety: float = 0.0) -> None:
22 | super().__init__()
|
help: Use type parameters
Found 7 errors.
No fixes available (7 hidden fixes can be enabled with the `--unsafe-fixes` option).
```
</details>
Running it after the changes:
```zsh
ruff % cargo run -p ruff -- check ../efax/ --target-version py312 --no-cache
Compiling ruff_linter v0.14.1 (/Users/prakhar/ruff/crates/ruff_linter)
Compiling ruff v0.14.1 (/Users/prakhar/ruff/crates/ruff)
Compiling ruff_graph v0.1.0 (/Users/prakhar/ruff/crates/ruff_graph)
Compiling ruff_workspace v0.0.0 (/Users/prakhar/ruff/crates/ruff_workspace)
Compiling ruff_server v0.2.2 (/Users/prakhar/ruff/crates/ruff_server)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 7.86s
Running `target/debug/ruff check ../efax/ --target-version py312 --no-cache`
All checks passed!
```
---
### Manual testing 2
Ran the check on the following script (mainly to verify `UP047`):
```py
from __future__ import annotations
from typing import Generic
from typing_extensions import TypeVar
T = TypeVar("T", default=int)
def generic_function(var: T) -> T:
return var
Q = TypeVar("Q", default=str)
class GenericClass(Generic[Q]):
var: Q
```
On `main` branch:
> ruff % cargo run -p ruff -- check ~/up046.py --target-version py312
--preview --no-cache
<details><summary>Output</summary>
```zsh
Compiling ruff_linter v0.14.1 (/Users/prakhar/ruff/crates/ruff_linter)
Compiling ruff v0.14.1 (/Users/prakhar/ruff/crates/ruff)
Compiling ruff_graph v0.1.0 (/Users/prakhar/ruff/crates/ruff_graph)
Compiling ruff_workspace v0.0.0 (/Users/prakhar/ruff/crates/ruff_workspace)
Compiling ruff_server v0.2.2 (/Users/prakhar/ruff/crates/ruff_server)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 7.43s
Running `target/debug/ruff check /Users/prakhar/up046.py --target-version py312 --preview --no-cache`
UP047 Generic function `generic_function` should use type parameters
--> /Users/prakhar/up046.py:10:5
|
10 | def generic_function(var: T) -> T:
| ^^^^^^^^^^^^^^^^^^^^^^^^
11 | return var
|
help: Use type parameters
UP046 Generic class `GenericClass` uses `Generic` subclass instead of type parameters
--> /Users/prakhar/up046.py:17:20
|
17 | class GenericClass(Generic[Q]):
| ^^^^^^^^^^
18 | var: Q
|
help: Use type parameters
Found 2 errors.
No fixes available (2 hidden fixes can be enabled with the `--unsafe-fixes` option).
```
</details>
After the fix (this branch):
```zsh
ruff % cargo run -p ruff -- check ~/up046.py --target-version py312 --preview --no-cache
Compiling ruff_linter v0.14.1 (/Users/prakhar/ruff/crates/ruff_linter)
Compiling ruff v0.14.1 (/Users/prakhar/ruff/crates/ruff)
Compiling ruff_graph v0.1.0 (/Users/prakhar/ruff/crates/ruff_graph)
Compiling ruff_workspace v0.0.0 (/Users/prakhar/ruff/crates/ruff_workspace)
Compiling ruff_server v0.2.2 (/Users/prakhar/ruff/crates/ruff_server)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 7.40s
Running `target/debug/ruff check /Users/prakhar/up046.py --target-version py312 --preview --no-cache`
All checks passed!
```
Signed-off-by: Prakhar Pratyush <[email protected]>
1 parent e55bc94 commit 10bda3d
File tree
9 files changed
+67
-14
lines changed- crates/ruff_linter
- resources/test/fixtures/pyupgrade
- src/rules/pyupgrade
- rules/pep695
- snapshots
9 files changed
+67
-14
lines changedLines changed: 13 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
| 114 | + | |
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
128 | 144 | | |
129 | 145 | | |
130 | 146 | | |
| |||
144 | 160 | | |
145 | 161 | | |
146 | 162 | | |
147 | | - | |
| 163 | + | |
148 | 164 | | |
149 | 165 | | |
150 | 166 | | |
| |||
Lines changed: 9 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
| 9 | + | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
369 | 369 | | |
370 | 370 | | |
371 | 371 | | |
372 | | - | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
373 | 375 | | |
374 | 376 | | |
375 | 377 | | |
376 | 378 | | |
377 | 379 | | |
378 | | - | |
| 380 | + | |
| 381 | + | |
379 | 382 | | |
380 | | - | |
| 383 | + | |
| 384 | + | |
381 | 385 | | |
382 | 386 | | |
383 | 387 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
| 41 | + | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
| 65 | + | |
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | | - | |
| 83 | + | |
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
| 103 | + | |
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
0 commit comments