Skip to content

PLR6301 doesn't detect @override from TYPE_CHECKING #24713

@ItsDrike

Description

@ItsDrike

Summary

The preview rule PLR6301 is meant to detect when a method should instead become a staticmethod, class method or a fucntion, and triggering when the self argument in the function body is unused.

The documentation for this rule explains that it doesn't trigger for methods decorated with @typing.override, which is good, however, this detection doesn't work when the override symbol is imported within a TYPE_CHECKING block, and has a fallback in the else block after to just a no-op decorator.

This pattern is pretty important for libraries that need to handle backwards compatibility and don't want to have typing_extensions become a runtime dependency.

E.g.

if TYPE_CHECKING:
    from typing_extensions import override
else:
    override = lambda f: f

# Ruff doesn't seem to understand that `override` in here is actually the `typing.override`
# and when PLR6301 violation is detected, even if decorated with `@override`, it triggers.

Playground link: https://play.ruff.rs/45ead6a4-9511-49be-bf76-010f6d325a80

Version

ruff 0.15.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions