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

[flake8-pyi] Fix false negative for PYI046 with unused generic protocols #9405

Merged
merged 1 commit into from
Jan 5, 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
18 changes: 16 additions & 2 deletions crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI046.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import typing
from typing import Protocol
from typing import Protocol, TypeVar


class _Foo(Protocol):
Expand All @@ -10,9 +10,23 @@ class _Bar(typing.Protocol):
bar: int


_T = TypeVar("_T")


class _Baz(Protocol[_T]):
x: _T


# OK
class _UsedPrivateProtocol(Protocol):
bar: int


def uses__UsedPrivateProtocol(arg: _UsedPrivateProtocol) -> None: ...
# Also OK
class _UsedGenericPrivateProtocol(Protocol[_T]):
x: _T


def uses_some_private_protocols(
arg: _UsedPrivateProtocol, arg2: _UsedGenericPrivateProtocol[int]
) -> None: ...
18 changes: 16 additions & 2 deletions crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI046.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import typing
from typing import Protocol
from typing import Protocol, TypeVar


class _Foo(object, Protocol):
Expand All @@ -10,9 +10,23 @@ class _Bar(typing.Protocol):
bar: int


_T = TypeVar("_T")


class _Baz(Protocol[_T]):
x: _T


# OK
class _UsedPrivateProtocol(Protocol):
bar: int


def uses__UsedPrivateProtocol(arg: _UsedPrivateProtocol) -> None: ...
# Also OK
class _UsedGenericPrivateProtocol(Protocol[_T]):
x: _T


def uses_some_private_protocols(
arg: _UsedPrivateProtocol, arg2: _UsedGenericPrivateProtocol[int]
) -> None: ...
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::map_subscript;
use ruff_python_ast::{self as ast, Expr, Stmt};
use ruff_python_semantic::Scope;
use ruff_text_size::Ranged;
Expand Down Expand Up @@ -243,11 +244,11 @@ pub(crate) fn unused_private_protocol(
continue;
};

if !class_def
.bases()
.iter()
.any(|base| checker.semantic().match_typing_expr(base, "Protocol"))
{
if !class_def.bases().iter().any(|base| {
checker
.semantic()
.match_typing_expr(map_subscript(base), "Protocol")
}) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ PYI046.py:9:7: PYI046 Private protocol `_Bar` is never used
10 | bar: int
|

PYI046.py:16:7: PYI046 Private protocol `_Baz` is never used
|
16 | class _Baz(Protocol[_T]):
| ^^^^ PYI046
17 | x: _T
|


Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ PYI046.pyi:9:7: PYI046 Private protocol `_Bar` is never used
10 | bar: int
|

PYI046.pyi:16:7: PYI046 Private protocol `_Baz` is never used
|
16 | class _Baz(Protocol[_T]):
| ^^^^ PYI046
17 | x: _T
|


Loading