Skip to content

FAST002 (FastAPI dependency without Annotated) could be made even smarter #22188

@tuttle

Description

@tuttle

Summary

FAST002 autofix rewrites code substantially and is currently pretty smart, indeed saves a lot of work.

Anyway I see two possible improvements, yet I'm not sure how complicated they would be to implement:

1. FAST002 creates too long lines.

The autofix generates this:

    query: Annotated[str | None, Query(description="Optional filtering of websites by registered domain name.")] = None,

which immediately triggers E501 Line too long in the same run.

This would be great to generate instead if it is possible to do:

    query: Annotated[
        str | None,
        Query(description="Optional filtering of websites by registered domain name."),
    ] = None,

with possible wrap of too long strings.

2. FAST002 silently skips autofix of badly ordered arguments.

In our code we had this endpoint argument as the last one:

    website_ids: str = Query(
        ...,
        description="Comma separated list of website IDs",
    ),

It appears to me the FAST002's autofix detects it WOULD create:

    website_ids: Annotated[str, Query(description="Comma separated list of website IDs")],

That means, from the Python POV, it would turn the argument with default value to argument without default value. But because the argument is placed AFTER other arguments with default value which is impossible in Python, the autofix is disabled and the code is left with unconverted arguments that still trigger FAST002.

Would it be possible to either of these?

a. Give the ruff user some kind hint to reorder the arguments manually before the autofix is applied.
b. Reorder the arguments appropriately as part of the autofix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    diagnosticsRelated to reporting of diagnostics.fixesRelated to suggested fixes for violations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions