Skip to content

Panic in PYI019 autofix #15849

@AlexWaygood

Description

@AlexWaygood

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.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingruleImplementing or modifying a lint rule

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions