Skip to content

Commit 3a5cdb1

Browse files
committed
fix: ensure Python version compatibility for union types
- Replace bytes | None with Union[bytes, None] for broader compatibility - Replace str | None with Union[str, None] in examples - Add noqa: UP007 comments to suppress linter preference for newer syntax - Ensures compatibility with Python environments that don't support PEP 604 unions - Fixes test failure: 'Unable to evaluate type annotation bytes | None' All File parameter tests continue to pass (13/13) across Python versions.
1 parent 0744776 commit 3a5cdb1

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

examples/event_handler_rest/src/file_parameter_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from __future__ import annotations
66

7-
from typing import Annotated
7+
from typing import Annotated, Union
88

99
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
1010
from aws_lambda_powertools.event_handler.openapi.params import File, Form
@@ -23,7 +23,7 @@ def upload_single_file(file: Annotated[bytes, File(description="File to upload")
2323
def upload_file_with_metadata(
2424
file: Annotated[bytes, File(description="File to upload")],
2525
description: Annotated[str, Form(description="File description")],
26-
tags: Annotated[str | None, Form(description="Optional tags")] = None,
26+
tags: Annotated[Union[str, None], Form(description="Optional tags")] = None, # noqa: UP007
2727
):
2828
"""Upload a file with additional form metadata."""
2929
return {
@@ -63,7 +63,7 @@ def upload_small_file(file: Annotated[bytes, File(description="Small file only",
6363
@app.post("/upload-optional")
6464
def upload_optional_file(
6565
message: Annotated[str, Form(description="Required message")],
66-
file: Annotated[bytes | None, File(description="Optional file")] = None,
66+
file: Annotated[Union[bytes, None], File(description="Optional file")] = None, # noqa: UP007
6767
):
6868
"""Upload with an optional file parameter."""
6969
return {

tests/functional/event_handler/_pydantic/test_file_multipart_comprehensive.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import base64
88
import json
9-
from typing import Annotated
9+
from typing import Annotated, Union
1010

1111
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
1212
from aws_lambda_powertools.event_handler.openapi.params import File, Form
@@ -286,7 +286,7 @@ def test_optional_file_parameter():
286286
@app.post("/upload")
287287
def upload_file(
288288
message: Annotated[str, Form(description="Required message")],
289-
file: Annotated[bytes | None, File(description="Optional file")] = None,
289+
file: Annotated[Union[bytes, None], File(description="Optional file")] = None, # noqa: UP007
290290
):
291291
return {"has_file": file is not None, "file_size": len(file) if file else 0, "message": message}
292292

0 commit comments

Comments
 (0)