Skip to content

Commit

Permalink
[flake8-pyi] Fix more complex cases (PYI019) (#15821)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
  • Loading branch information
InSyncWithFoo and AlexWaygood authored Feb 2, 2025
1 parent 770b7f3 commit 3c09100
Show file tree
Hide file tree
Showing 9 changed files with 276 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def n(self: S) -> S[int]: ...

class UsesFullyQualifiedType:
@classmethod
def m[S](cls: builtins.type[S]) -> S: ... # PYI019
def m[S](cls: builtins.type[S]) -> S: ... # False negative (#15821)


def shadowed_type():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ import builtins

class UsesFullyQualifiedType:
@classmethod
def m[S](cls: builtins.type[S]) -> S: ... # PYI019
def m[S](cls: builtins.type[S]) -> S: ... # False negative (#15821)


def shadowed_type():
Expand All @@ -116,3 +116,26 @@ class SubscriptReturnType:

class PEP695TypeParameterAtTheVeryEndOfTheList:
def f[T, S](self: S) -> S: ...


class PEP695Again:
def mixing_and_nested[T](self: _S695, a: list[_S695], b: dict[_S695, str | T | set[_S695]]) -> _S695: ...
def also_uses_s695_but_should_not_be_edited(self, v: set[tuple[_S695]]) -> _S695: ...

@classmethod
def comment_in_fix_range[T, S](
cls: type[ # Lorem ipsum
S
],
a: T,
b: tuple[S, T]
) -> S: ...

def comment_outside_fix_range[T, S](
self: S,
a: T,
b: tuple[
# Lorem ipsum
S, T
]
) -> S: ...
8 changes: 8 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub(crate) fn bindings(checker: &mut Checker) {
Rule::UsedDummyVariable,
Rule::PytestUnittestRaisesAssertion,
Rule::ForLoopWrites,
Rule::CustomTypeVarReturnType,
]) {
return;
}
Expand Down Expand Up @@ -115,5 +116,12 @@ pub(crate) fn bindings(checker: &mut Checker) {
checker.diagnostics.push(diagnostic);
}
}
if checker.enabled(Rule::CustomTypeVarReturnType) {
if let Some(diagnostic) =
flake8_pyi::rules::custom_type_var_return_type(checker, binding)
{
checker.diagnostics.push(diagnostic);
}
}
}
}
3 changes: 0 additions & 3 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,6 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
if checker.enabled(Rule::GeneratorReturnFromIterMethod) {
flake8_pyi::rules::bad_generator_return_type(function_def, checker);
}
if checker.enabled(Rule::CustomTypeVarReturnType) {
flake8_pyi::rules::custom_type_var_return_type(checker, function_def);
}
if checker.source_type.is_stub() {
if checker.enabled(Rule::StrOrReprDefinedInStub) {
flake8_pyi::rules::str_or_repr_defined_in_stub(checker, stmt);
Expand Down
Loading

0 comments on commit 3c09100

Please sign in to comment.