Skip to content

Commit 0744776

Browse files
committed
fix: resolve linting issues in File parameter implementation
- Add missing __future__ annotations imports - Remove unused pytest imports from test files - Remove unused json import from example - Fix line length violations in test files - All File parameter tests continue to pass (13/13) Addresses ruff linting violations: - FA102: Missing future annotations for PEP 604 unions - F401: Unused imports - E501: Line too long violations
1 parent 475c7f4 commit 0744776

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

aws_lambda_powertools/event_handler/middlewares/openapi_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def _parse_multipart_data(self, app: EventHandlerInstance, content_type: str) ->
192192
"input": {},
193193
"ctx": {"error": str(e)},
194194
},
195-
]
195+
],
196196
) from e
197197

198198
def _decode_request_body(self, app: EventHandlerInstance) -> bytes:

examples/event_handler_rest/src/file_parameter_example.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
"""
2-
Example demonstrating File parameter usage in AWS Lambda Powertools Python Event Handler.
3-
4-
This example shows how to use the File parameter for handling multipart/form-data file uploads
5-
with OpenAPI validation and automatic schema generation.
2+
Example demonstrating File parameter usage for handling file uploads.
63
"""
74

5+
from __future__ import annotations
6+
87
from typing import Annotated
98

109
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
1110
from aws_lambda_powertools.event_handler.openapi.params import File, Form
1211

13-
1412
# Initialize resolver with OpenAPI validation enabled
1513
app = APIGatewayRestResolver(enable_validation=True)
1614

tests/functional/event_handler/_pydantic/test_file_form_validation.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import json
66
from typing import Annotated
77

8-
import pytest
9-
108
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
119
from aws_lambda_powertools.event_handler.openapi.params import File, Form
1210

@@ -57,15 +55,19 @@ def test_form_parameter_validation():
5755

5856
@app.post("/contact")
5957
def contact_form(
60-
name: Annotated[str, Form(description="Contact name")], email: Annotated[str, Form(description="Contact email")]
58+
name: Annotated[str, Form(description="Contact name")],
59+
email: Annotated[str, Form(description="Contact email")],
6160
):
6261
return {"message": f"Hello {name}, we'll contact you at {email}"}
6362

6463
# Create form data request
6564
body = "name=John+Doe&email=john%40example.com"
6665

6766
event = make_request_event(
68-
method="POST", path="/contact", body=body, headers={"content-type": "application/x-www-form-urlencoded"}
67+
method="POST",
68+
path="/contact",
69+
body=body,
70+
headers={"content-type": "application/x-www-form-urlencoded"},
6971
)
7072

7173
response = app.resolve(event, {})

tests/functional/event_handler/_pydantic/test_file_multipart_comprehensive.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
Comprehensive tests for File parameter multipart parsing and validation.
33
"""
44

5+
from __future__ import annotations
6+
57
import base64
68
import json
79
from typing import Annotated
810

9-
import pytest
10-
1111
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
1212
from aws_lambda_powertools.event_handler.openapi.params import File, Form
1313

@@ -23,7 +23,7 @@ def make_multipart_event(boundary="----WebKitFormBoundary7MA4YWxkTrZu0gW", body_
2323
body_lines.append(f"--{boundary}")
2424
body_lines.append(
2525
f'Content-Disposition: form-data; name="{part["name"]}"'
26-
+ (f'; filename="{part["filename"]}"' if part.get("filename") else "")
26+
+ (f'; filename="{part["filename"]}"' if part.get("filename") else ""),
2727
)
2828
if part.get("content_type"):
2929
body_lines.append(f"Content-Type: {part['content_type']}")
@@ -75,7 +75,7 @@ def upload_file(file: Annotated[bytes, File(description="File to upload")]):
7575

7676
# Create a simple file upload
7777
event = make_multipart_event(
78-
body_parts=[{"name": "file", "filename": "test.txt", "content_type": "text/plain", "content": "Hello, world!"}]
78+
body_parts=[{"name": "file", "filename": "test.txt", "content_type": "text/plain", "content": "Hello, world!"}],
7979
)
8080

8181
response = app.resolve(event, {})
@@ -109,7 +109,7 @@ def upload_with_metadata(
109109
},
110110
{"name": "title", "content": "Important Document"},
111111
{"name": "description", "content": "This is a test document upload"},
112-
]
112+
],
113113
)
114114

115115
response = app.resolve(event, {})
@@ -134,7 +134,7 @@ def upload_file(file: Annotated[bytes, File()]):
134134
event = make_multipart_event(
135135
boundary=webkit_boundary,
136136
body_parts=[
137-
{"name": "file", "filename": "test.jpg", "content_type": "image/jpeg", "content": "fake image data"}
137+
{"name": "file", "filename": "test.jpg", "content_type": "image/jpeg", "content": "fake image data"},
138138
],
139139
)
140140

@@ -180,7 +180,7 @@ def upload_files(file1: Annotated[bytes, File(alias="file1")], file2: Annotated[
180180
body_parts=[
181181
{"name": "file1", "filename": "first.txt", "content": "First file content"},
182182
{"name": "file2", "filename": "second.txt", "content": "Second file content is longer"},
183-
]
183+
],
184184
)
185185

186186
response = app.resolve(event, {})
@@ -263,8 +263,12 @@ def upload_file(file: Annotated[bytes, File(description="Small file", max_length
263263
# Test file that's too large
264264
event = make_multipart_event(
265265
body_parts=[
266-
{"name": "file", "filename": "large.txt", "content": "This file content is way too long for the constraint"}
267-
]
266+
{
267+
"name": "file",
268+
"filename": "large.txt",
269+
"content": "This file content is way too long for the constraint",
270+
},
271+
],
268272
)
269273

270274
response = app.resolve(event, {})
@@ -312,8 +316,8 @@ def upload_file(file: Annotated[bytes, File()]):
312316
"name": "file",
313317
"filename": "empty.txt",
314318
"content": "", # Empty file
315-
}
316-
]
319+
},
320+
],
317321
)
318322

319323
response = app.resolve(event, {})

0 commit comments

Comments
 (0)