Skip to content

Commit

Permalink
Fix type hints for boto3
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelhwilliams committed Aug 29, 2024
1 parent 633a805 commit 731ed6b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ repos:
# args: [--show-error-codes]
exclude: ^(submit|find)/.*$
additional_dependencies: [
"boto3-stubs[s3,logs]",
pandas-stubs,
sqlalchemy-stubs,
types-beautifulsoup4,
Expand Down
2 changes: 1 addition & 1 deletion data_store/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def upload_file(file: Union[IO, FileStorage], bucket: str, object_name: str, met

file.seek(0)
_S3_CLIENT.upload_fileobj(
file,
file.stream if isinstance(file, FileStorage) else file,
bucket,
object_name,
ExtraArgs={"Metadata": metadata if metadata else {}, "ContentType": content_type},
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ pytest-playwright
# Code Quality
#-----------------------------------
mypy==1.11.1 # If bumping this, please also bump .pre-commit-config.yml
boto3-stubs[s3,logs]
pandas-stubs
sqlalchemy-stubs
types-beautifulsoup4
types-boto3
types-colorama
types-flask
types-Flask-Migrate
Expand Down
12 changes: 8 additions & 4 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ blinker==1.8.2
# sentry-sdk
boto3==1.34.151
# via -r requirements.txt
boto3-stubs==1.34.133
# via types-boto3
boto3-stubs[logs,s3]==1.34.133
# via -r requirements-dev.in
botocore==1.34.151
# via
# -r requirements.txt
Expand Down Expand Up @@ -231,6 +231,10 @@ mypy==1.11.1
# via
# -r requirements-dev.in
# sqlalchemy-stubs
mypy-boto3-logs==1.34.151
# via boto3-stubs
mypy-boto3-s3==1.34.162
# via boto3-stubs
mypy-extensions==1.0.0
# via
# -r requirements.txt
Expand Down Expand Up @@ -415,8 +419,6 @@ types-awscrt==0.20.12
# via botocore-stubs
types-beautifulsoup4==4.12.0.20240511
# via -r requirements-dev.in
types-boto3==1.0.2
# via -r requirements-dev.in
types-click==7.1.8
# via types-flask
types-colorama==0.4.15.20240311
Expand Down Expand Up @@ -473,6 +475,8 @@ typing-extensions==4.6.0
# alembic
# boto3-stubs
# mypy
# mypy-boto3-logs
# mypy-boto3-s3
# pydantic
# pyee
# sqlalchemy
Expand Down
9 changes: 5 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import pandas as pd
import pytest
from flask.testing import FlaskClient
from mypy_boto3_s3.type_defs import ObjectIdentifierTypeDef
from sqlalchemy import text
from werkzeug.test import TestResponse

Expand Down Expand Up @@ -731,10 +732,10 @@ def create_bucket(bucket: str):

def delete_bucket(bucket: str):
"""Helper function that deletes all objects in a specified bucket and then deletes the bucket."""
objects = _S3_CLIENT.list_objects_v2(Bucket=bucket)
if objects := objects.get("Contents"):
objects = list(map(lambda x: {"Key": x["Key"]}, objects))
_S3_CLIENT.delete_objects(Bucket=bucket, Delete={"Objects": objects})
s3_objects_response = _S3_CLIENT.list_objects_v2(Bucket=bucket)
if "Contents" in s3_objects_response:
s3_objects: list[ObjectIdentifierTypeDef] = [{"Key": obj["Key"]} for obj in s3_objects_response["Contents"]]
_S3_CLIENT.delete_objects(Bucket=bucket, Delete={"Objects": s3_objects})
_S3_CLIENT.delete_bucket(Bucket=bucket)


Expand Down

0 comments on commit 731ed6b

Please sign in to comment.