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

Panic in PYI019 autofix #15849

Closed
AlexWaygood opened this issue Jan 31, 2025 · 1 comment · Fixed by #15851
Closed

Panic in PYI019 autofix #15849

AlexWaygood opened this issue Jan 31, 2025 · 1 comment · Fixed by #15851
Assignees
Labels
bug Something isn't working rule Implementing or modifying a lint rule

Comments

@AlexWaygood
Copy link
Member

Description

Running ruff check --select=PYI019 --preview foo.pyi triggers a panic if foo.pyi has this contents:

class F:
    @classmethod
    def m[S](cls: type[S], /) -> S[int]: ...

The panic occurs at this line here:

// The return annotation is guaranteed to be a name,
// as verified by `uses_custom_var()`.
let typevar_name = returns.as_name_expr().unwrap().id();

And the reason is that, contrary to what the comment says, uses_custom_typevar does not guarantee that the return Expr will be an ExprName, due to the map_subscript call here:

let Expr::Name(return_annotation) = map_subscript(self.returns) else {
return false;
};

(The map_subscript call also looks buggy, FWIW.)

We should switch to a more type-safe code pattern here. Note that although the panic occurs in autofix logic, the panic occurs even if you run Ruff without --fix, since we create fixes for diagnostics even if --fix is not specified.

@AlexWaygood AlexWaygood added bug Something isn't working rule Implementing or modifying a lint rule labels Jan 31, 2025
@AlexWaygood AlexWaygood self-assigned this Jan 31, 2025
@AlexWaygood
Copy link
Member Author

This also triggers a panic at the same location in the Rust code:

class F:
    def m[S](self: S) -> S[int]: ...

Because there's another map_subscript call here, which is also incorrect:

let Expr::Name(ast::ExprName {
id: return_type, ..
}) = map_subscript(self.returns)
else {
return false;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant