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

[FAST002] FastAPI dependency without Annotated unsafe fix error #12982

Closed
mbrulatout opened this issue Aug 19, 2024 · 2 comments · Fixed by #13133
Closed

[FAST002] FastAPI dependency without Annotated unsafe fix error #12982

mbrulatout opened this issue Aug 19, 2024 · 2 comments · Fixed by #13133
Labels
bug Something isn't working fixes Related to suggested fixes for violations good first issue Good for newcomers preview Related to preview mode features

Comments

@mbrulatout
Copy link

mbrulatout commented Aug 19, 2024

Hello,

Using ruff 0.6.1, I keep getting an error on FAST002 autofix.

I tried to simplify as much as i could both the code and config

datacenter_state_router = APIRouter()

@datacenter_state_router.patch(
    "/{name}",
)
async def datacenter_state_patch(
    current_state: DatacenterState = PermissionDepends(Permission.WRITE, get_datacenter_state),
    session: Session = Depends(database_manager.get_session),
) -> DatacenterState:
    pass

pyproject.toml

[tool.ruff]
target-version = 'py311'
line-length = 120
exclude = [
    "criteo/ingress/api/lib/models/alembic/"
]

[tool.ruff.format]
preview = true

[tool.ruff.lint]
# See complete list : https://beta.ruff.rs/docs/rules
select = [
    "FAST",  # fastapi
]

fixable = [
    "FAST002",
]

Removing that PermissionDepends arg (a wrapper around an actual dependency) fixes it.

class Permission(Enum):
    READ = "read"
    WRITE = "write"

def permission_dependency(
    permission: Permission, resource: Callable[..., Any], user: User = Depends(get_current_user)
) -> Any:
    dependable_resource = Depends(resource)

    def resource_call(_resource: OwnedResource = dependable_resource, _user: User = user) -> Any:
        return _resource

    return Depends(resource_call)

# An alias to mimic FastAPI Dependency (https://fastapi.tiangolo.com/reference/dependencies/#fastapi.Depends)
# This is usable with the additional permission, the contract being the following:
# PermissionDepends(Permission, Callable[..., OwnedResource])
PermissionDepends = permission_dependency
@mbrulatout mbrulatout changed the title [Fix error] FAST002 FastAPI dependency without Annotated unsafe fix [FAST002] FastAPI dependency without Annotated unsafe fix Aug 19, 2024
@mbrulatout mbrulatout changed the title [FAST002] FastAPI dependency without Annotated unsafe fix [FAST002] FastAPI dependency without Annotated unsafe fix error Aug 19, 2024
@MichaReiser
Copy link
Member

For reproduction, it's important that the code has the relevant imports:

from fastapi import Depends, FastAPI, APIRouter

datacenter_state_router = APIRouter()

@datacenter_state_router.patch(
    "/{name}",
)
async def datacenter_state_patch(
    current_state: DatacenterState = PermissionDepends(
        Permission.WRITE, get_datacenter_state
    ),
    session: Session = Depends(database_manager.get_session),
) -> DatacenterState:
    pass

Playground

@MichaReiser
Copy link
Member

The fix transforms the example into

from fastapi import Depends, FastAPI, APIRouter
from typing import Annotated

datacenter_state_router = APIRouter()

@datacenter_state_router.patch()
async def datacenter_state_patch(
    current_state: DatacenterState = PermissionDepends(
        Permission.WRITE, get_datacenter_state
    ),
    session: Annotated[Session, Depends(database_manager.get_session)],
) -> DatacenterState:
    pass

and the problem is that session now no longer has a default value but comes after a parameter with a default value.

The easiest fix is to disable the fix if the parameter comes after a parameter with a default value.

@MichaReiser MichaReiser added bug Something isn't working fixes Related to suggested fixes for violations preview Related to preview mode features good first issue Good for newcomers labels Aug 19, 2024
arkuhn added a commit to arkuhn/ruff that referenced this issue Aug 28, 2024
  - Context: this rule swaps out default function argument values
for type annotations
  - Problem: When this swap occurs after a default argument value is
    already present, the function signature is no longer valid python
code (astral-sh#12982)
  - Fix: Track default arguments and bail if we hit this condition,
    making this fix only sometimes possible.
arkuhn added a commit to arkuhn/ruff that referenced this issue Aug 28, 2024
  - Context: this rule swaps out default function argument values
for type annotations
  - Problem: When this swap occurs after a default argument value is
    already present, the function signature is no longer valid python
code (astral-sh#12982)
  - Fix: Track default arguments and bail if we hit this condition,
    making this fix only sometimes possible.
arkuhn added a commit to arkuhn/ruff that referenced this issue Aug 28, 2024
  - Context: this rule swaps out default function argument values
for type annotations
  - Problem: When this swap occurs after a default argument value is
    already present, the function signature is no longer valid python
code (astral-sh#12982)
  - Fix: Track default arguments and bail if we hit this condition,
    making this fix only sometimes possible.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations good first issue Good for newcomers preview Related to preview mode features
Projects
None yet
2 participants