-
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.
Add support for @functools.singledispatch
- Loading branch information
1 parent
b2638c6
commit 1a4c324
Showing
5 changed files
with
183 additions
and
40 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
crates/ruff_linter/resources/test/fixtures/flake8_type_checking/singledispatch.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,34 @@ | ||
"""Test module.""" | ||
from __future__ import annotations | ||
|
||
from functools import singledispatch | ||
from typing import TYPE_CHECKING | ||
|
||
from numpy import asarray | ||
from numpy.typing import ArrayLike | ||
from scipy.sparse import spmatrix | ||
from pandas import DataFrame | ||
|
||
if TYPE_CHECKING: | ||
from numpy import ndarray | ||
|
||
|
||
@singledispatch | ||
def to_array_or_mat(a: ArrayLike | spmatrix) -> ndarray | spmatrix: | ||
"""Convert arg to array or leaves it as sparse matrix.""" | ||
msg = f"Unhandled type {type(a)}" | ||
raise NotImplementedError(msg) | ||
|
||
|
||
@to_array_or_mat.register | ||
def _(a: ArrayLike) -> ndarray: | ||
return asarray(a) | ||
|
||
|
||
@to_array_or_mat.register | ||
def _(a: spmatrix) -> spmatrix: | ||
return a | ||
|
||
|
||
def _(a: DataFrame) -> DataFrame: | ||
return a |
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
27 changes: 27 additions & 0 deletions
27
...rules__flake8_type_checking__tests__typing-only-third-party-import_singledispatch.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,27 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs | ||
--- | ||
singledispatch.py:10:20: TCH002 [*] Move third-party import `pandas.DataFrame` into a type-checking block | ||
| | ||
8 | from numpy.typing import ArrayLike | ||
9 | from scipy.sparse import spmatrix | ||
10 | from pandas import DataFrame | ||
| ^^^^^^^^^ TCH002 | ||
11 | | ||
12 | if TYPE_CHECKING: | ||
| | ||
= help: Move into type-checking block | ||
|
||
ℹ Unsafe fix | ||
7 7 | from numpy import asarray | ||
8 8 | from numpy.typing import ArrayLike | ||
9 9 | from scipy.sparse import spmatrix | ||
10 |-from pandas import DataFrame | ||
11 10 | | ||
12 11 | if TYPE_CHECKING: | ||
12 |+ from pandas import DataFrame | ||
13 13 | from numpy import ndarray | ||
14 14 | | ||
15 15 | | ||
|
||
|