Skip to content

Commit

Permalink
[flake8-pyi] Significantly improve accuracy of PYI019 if preview …
Browse files Browse the repository at this point in the history
…mode is enabled
  • Loading branch information
AlexWaygood committed Feb 3, 2025
1 parent dfe1b84 commit 4e2dc61
Show file tree
Hide file tree
Showing 9 changed files with 839 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,27 @@ def m[S](cls: type[S]) -> S: ... # no error here

class SubscriptReturnType:
@classmethod
def m[S](cls: type[S]) -> type[S]: ... # PYI019, but no autofix (yet)
def m[S](cls: type[S]) -> type[S]: ... # PYI019


class SelfNotUsedInReturnAnnotation:
def m[S](self: S, other: S) -> int: ...
@classmethod
def n[S](cls: type[S], other: S) -> int: ...


class _NotATypeVar: ...

# Our stable-mode logic uses heuristics and thinks this is a `TypeVar`
# because `self` and the return annotation use the same name as their annotation,
# but our preview-mode logic is smarter about this.
class Foo:
def x(self: _NotATypeVar) -> _NotATypeVar: ...
@classmethod
def y(self: type[_NotATypeVar]) -> _NotATypeVar: ...


class NoReturnAnnotations:
def m[S](self: S, other: S): ...
@classmethod
def n[S](cls: type[S], other: S): ...
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def shadowed_type():

class SubscriptReturnType:
@classmethod
def m[S](cls: type[S]) -> type[S]: ... # PYI019, but no autofix (yet)
def m[S](cls: type[S]) -> type[S]: ... # PYI019


class PEP695TypeParameterAtTheVeryEndOfTheList:
Expand Down Expand Up @@ -139,3 +139,25 @@ class PEP695Again:
S, T
]
) -> S: ...


class SelfNotUsedInReturnAnnotation:
def m[S](self: S, other: S) -> int: ...
@classmethod
def n[S](cls: type[S], other: S) -> int: ...


class _NotATypeVar: ...

# Our stable-mode logic uses heuristics and thinks this is a `TypeVar`
# because `self` and the return annotation use the same name as their annotation,
# but our preview-mode logic is smarter about this.
class Foo:
def x(self: _NotATypeVar) -> _NotATypeVar: ...
@classmethod
def y(self: type[_NotATypeVar]) -> _NotATypeVar: ...

class NoReturnAnnotations:
def m[S](self: S, other: S): ...
@classmethod
def n[S](cls: type[S], other: S): ...
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/flake8_pyi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ mod tests {
Ok(())
}

#[test_case(Rule::CustomTypeVarForSelf, Path::new("PYI019_0.py"))]
#[test_case(Rule::CustomTypeVarForSelf, Path::new("PYI019_0.pyi"))]
#[test_case(Rule::CustomTypeVarForSelf, Path::new("PYI019_1.pyi"))]
fn custom_classmethod_rules_preview(rule_code: Rule, path: &Path) -> Result<()> {
Expand Down
Loading

0 comments on commit 4e2dc61

Please sign in to comment.