-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid marking
InitVar
as a typing-only annotation (#9688)
## Summary Given: ```python from dataclasses import InitVar, dataclass @DataClass class C: i: int j: int = None database: InitVar[DatabaseType] = None def __post_init__(self, database): if self.j is None and database is not None: self.j = database.lookup('j') c = C(10, database=my_database) ``` We should avoid marking `InitVar` as typing-only, since it _is_ required by the dataclass at runtime. Note that by default, we _already_ don't flag this, since the `@dataclass` member is needed at runtime too -- so it's only a problem with `strict` mode. Closes #9666.
- Loading branch information
1 parent
4ccbacd
commit 11449ac
Showing
7 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
crates/ruff_linter/resources/test/fixtures/flake8_type_checking/init_var.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""Test: avoid marking an `InitVar` as typing-only.""" | ||
|
||
from __future__ import annotations | ||
|
||
from dataclasses import FrozenInstanceError, InitVar, dataclass | ||
from pathlib import Path | ||
|
||
|
||
@dataclass | ||
class C: | ||
i: int | ||
j: int = None | ||
database: InitVar[Path] = None | ||
|
||
err: FrozenInstanceError = None | ||
|
||
def __post_init__(self, database): | ||
... |
This file contains 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
This file contains 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
This file contains 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
50 changes: 50 additions & 0 deletions
50
..._flake8_type_checking__tests__strict_typing-only-standard-library-import_init_var.py.snap
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs | ||
--- | ||
init_var.py:5:25: TCH003 [*] Move standard library import `dataclasses.FrozenInstanceError` into a type-checking block | ||
| | ||
3 | from __future__ import annotations | ||
4 | | ||
5 | from dataclasses import FrozenInstanceError, InitVar, dataclass | ||
| ^^^^^^^^^^^^^^^^^^^ TCH003 | ||
6 | from pathlib import Path | ||
| | ||
= help: Move into type-checking block | ||
|
||
ℹ Unsafe fix | ||
2 2 | | ||
3 3 | from __future__ import annotations | ||
4 4 | | ||
5 |-from dataclasses import FrozenInstanceError, InitVar, dataclass | ||
5 |+from dataclasses import InitVar, dataclass | ||
6 6 | from pathlib import Path | ||
7 |+from typing import TYPE_CHECKING | ||
8 |+ | ||
9 |+if TYPE_CHECKING: | ||
10 |+ from dataclasses import FrozenInstanceError | ||
7 11 | | ||
8 12 | | ||
9 13 | @dataclass | ||
|
||
init_var.py:6:21: TCH003 [*] Move standard library import `pathlib.Path` into a type-checking block | ||
| | ||
5 | from dataclasses import FrozenInstanceError, InitVar, dataclass | ||
6 | from pathlib import Path | ||
| ^^^^ TCH003 | ||
| | ||
= help: Move into type-checking block | ||
|
||
ℹ Unsafe fix | ||
3 3 | from __future__ import annotations | ||
4 4 | | ||
5 5 | from dataclasses import FrozenInstanceError, InitVar, dataclass | ||
6 |-from pathlib import Path | ||
6 |+from typing import TYPE_CHECKING | ||
7 |+ | ||
8 |+if TYPE_CHECKING: | ||
9 |+ from pathlib import Path | ||
7 10 | | ||
8 11 | | ||
9 12 | @dataclass | ||
|
||
|
File renamed without changes.
25 changes: 25 additions & 0 deletions
25
..._rules__flake8_type_checking__tests__typing-only-standard-library-import_init_var.py.snap
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs | ||
--- | ||
init_var.py:6:21: TCH003 [*] Move standard library import `pathlib.Path` into a type-checking block | ||
| | ||
5 | from dataclasses import FrozenInstanceError, InitVar, dataclass | ||
6 | from pathlib import Path | ||
| ^^^^ TCH003 | ||
| | ||
= help: Move into type-checking block | ||
|
||
ℹ Unsafe fix | ||
3 3 | from __future__ import annotations | ||
4 4 | | ||
5 5 | from dataclasses import FrozenInstanceError, InitVar, dataclass | ||
6 |-from pathlib import Path | ||
6 |+from typing import TYPE_CHECKING | ||
7 |+ | ||
8 |+if TYPE_CHECKING: | ||
9 |+ from pathlib import Path | ||
7 10 | | ||
8 11 | | ||
9 12 | @dataclass | ||
|
||
|