Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCH003: Fix false positive for singledispatchmethod #13941

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from __future__ import annotations

from collections.abc import MutableMapping, Mapping
from pathlib import Path
from functools import singledispatchmethod
from typing import Union


class Foo:
@singledispatchmethod
def foo(self, x: Union[MutableMapping, Mapping]) -> int:
raise NotImplementedError

@foo.register
def _(self, x: MutableMapping) -> int:
return 0

@foo.register
def _(self, x: Mapping) -> int:
return 0


class Foo2:

@singledispatchmethod
def process_path(self, a: Union[int, str]) -> int:
"""Convert arg to array or leaves it as sparse matrix."""
msg = f"Unhandled type {type(a)}"
raise NotImplementedError(msg)


@process_path.register
def _(self, a: int) -> int:
return a


@process_path.register
def _(self, a: str) -> int:
return len(a)


def _(self, p: Path) -> Path:
return p
10 changes: 6 additions & 4 deletions crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub(crate) fn is_dataclass_meta_annotation(annotation: &Expr, semantic: &Semanti
false
}

/// Returns `true` if a function is registered as a `singledispatch` interface.
/// Returns `true` if a function is registered as a `singledispatch` or `singledispatchmethod` interface.
///
/// For example, `fun` below is a `singledispatch` interface:
/// ```python
Expand All @@ -160,15 +160,17 @@ pub(crate) fn is_singledispatch_interface(
semantic
.resolve_qualified_name(&decorator.expression)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["functools", "singledispatch"])
matches!(
qualified_name.segments(),
["functools", "singledispatch" | "singledispatchmethod"]
)
})
})
}

/// Returns `true` if a function is registered as a `singledispatch` implementation.
/// Returns `true` if a function is registered as a `singledispatch` or `singledispatchmethod` implementation.
///
/// For example, `_` below is a `singledispatch` implementation:
/// For example:
/// ```python
/// from functools import singledispatch
///
Expand Down
4 changes: 4 additions & 0 deletions crates/ruff_linter/src/rules/flake8_type_checking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ mod tests {
#[test_case(Rule::TypingOnlyStandardLibraryImport, Path::new("init_var.py"))]
#[test_case(Rule::TypingOnlyStandardLibraryImport, Path::new("kw_only.py"))]
#[test_case(Rule::TypingOnlyStandardLibraryImport, Path::new("snapshot.py"))]
#[test_case(
Rule::TypingOnlyStandardLibraryImport,
Path::new("singledispatchmethod.py")
)]
#[test_case(Rule::TypingOnlyThirdPartyImport, Path::new("TCH002.py"))]
#[test_case(Rule::TypingOnlyThirdPartyImport, Path::new("quote.py"))]
#[test_case(Rule::TypingOnlyThirdPartyImport, Path::new("singledispatch.py"))]
Expand Down
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
---
singledispatchmethod.py:4:21: TCH003 [*] Move standard library import `pathlib.Path` into a type-checking block
|
3 | from collections.abc import MutableMapping, Mapping
4 | from pathlib import Path
| ^^^^ TCH003
5 | from functools import singledispatchmethod
6 | from typing import Union
|
= help: Move into type-checking block

ℹ Unsafe fix
1 1 | from __future__ import annotations
2 2 |
3 3 | from collections.abc import MutableMapping, Mapping
4 |-from pathlib import Path
5 4 | from functools import singledispatchmethod
6 |-from typing import Union
5 |+from typing import Union, TYPE_CHECKING
6 |+
7 |+if TYPE_CHECKING:
8 |+ from pathlib import Path
7 9 |
8 10 |
9 11 | class Foo:
Loading