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

[FIX] Using Optional from typing instead of | operation #498

Open
wants to merge 2 commits into
base: 16.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions fastapi/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2022 ACSONE SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/LGPL).

from typing import TYPE_CHECKING, Annotated
from typing import TYPE_CHECKING, Annotated, Optional

from odoo.api import Environment
from odoo.exceptions import AccessDenied
Expand All @@ -19,15 +19,15 @@
from .models.fastapi_endpoint import FastapiEndpoint


def company_id() -> int | None:
def company_id() -> Optional[int]:
"""This method may be overriden by the FastAPI app to set the allowed company
in the Odoo env of the endpoint. By default, the company defined on the
endpoint record is used.
"""
return None


def odoo_env(company_id: Annotated[int | None, Depends(company_id)]) -> Environment:
def odoo_env(company_id: Annotated[Optional[int], Depends(company_id)]) -> Environment:
env = odoo_env_ctx.get()
if company_id is not None:
env = env(context=dict(env.context, allowed_company_ids=[company_id]))
Expand All @@ -43,7 +43,7 @@ def authenticated_partner_impl() -> Partner:
See the fastapi_endpoint_demo for an example"""


def optionally_authenticated_partner_impl() -> Partner | None:
def optionally_authenticated_partner_impl() -> Optional[Partner]:
"""This method has to be overriden when you create your fastapi app
and you need to get an optional authenticated partner into your endpoint.
"""
Expand All @@ -57,7 +57,9 @@ def authenticated_partner_env(


def optionally_authenticated_partner_env(
partner: Annotated[Partner | None, Depends(optionally_authenticated_partner_impl)],
partner: Annotated[
Optional[Partner], Depends(optionally_authenticated_partner_impl)
],
env: Annotated[Environment, Depends(odoo_env)],
) -> Environment:
"""Return an environment with the authenticated partner id in the context if
Expand Down Expand Up @@ -85,9 +87,11 @@ def authenticated_partner(


def optionally_authenticated_partner(
partner: Annotated[Partner | None, Depends(optionally_authenticated_partner_impl)],
partner: Annotated[
Optional[Partner], Depends(optionally_authenticated_partner_impl)
],
partner_env: Annotated[Environment, Depends(optionally_authenticated_partner_env)],
) -> Partner | None:
) -> Optional[Partner]:
"""If you need to get access to the authenticated partner if the call is
authenticated, you can add a dependency into the endpoint definition on this
method.
Expand Down Expand Up @@ -155,7 +159,7 @@ def fastapi_endpoint(

def accept_language(
accept_language: Annotated[
str | None,
Optional[str],
Header(
alias="Accept-Language",
description="The Accept-Language header is used to specify the language "
Expand Down