Skip to content

Commit 5ee62a9

Browse files
authored
Merge pull request #14 from jowilf/dev
Add py.typed(PEP 561)
2 parents a4274d5 + cd21a49 commit 5ee62a9

File tree

8 files changed

+58
-32
lines changed

8 files changed

+58
-32
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
66
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.1.5] - 2022-10-15
9+
10+
---
11+
12+
### Added
13+
14+
- Add py.typed(PEP 561) by @jowilf https://github.com/jowilf/sqlalchemy-file/pull/14
15+
816
## [0.1.4] - 2022-08-30
917

1018
---

docs/changelog.md

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.1.5] - 2022-10-15
8+
9+
---
10+
11+
### Added
12+
13+
- Add py.typed(PEP 561) in [#14](https://github.com/jowilf/sqlalchemy-file/pull/14)
14+
715
## [0.1.4] - 2022-08-30
816

917
---

examples/fastapi/app.py

+32-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from typing import List, Optional, Union
2+
from typing import Generator, List, Optional, Union
33

44
import uvicorn
55
from fastapi import Depends, FastAPI
@@ -26,8 +26,7 @@
2626
StreamingResponse,
2727
)
2828

29-
engine = create_engine("sqlite:////tmp/example.db?check_same_thread=False", echo=True)
30-
29+
engine = create_engine("sqlite:///example.db?check_same_thread=False", echo=True)
3130

3231
os.makedirs("/tmp/storage", 0o777, exist_ok=True)
3332
driver = get_driver(Provider.LOCAL)("/tmp/storage")
@@ -80,7 +79,7 @@ class CategoryOut(CategoryBase):
8079

8180

8281
def category_form(
83-
name: str = Form(...),
82+
name: str = Form(..., min_length=3),
8483
image: Optional[UploadFile] = FormFile(None),
8584
):
8685
return Category(name=name, image=image)
@@ -89,37 +88,45 @@ def category_form(
8988
app = FastAPI(title="SQLAlchemy-file Example", debug=True)
9089

9190

91+
def get_session() -> Generator[Session, None, None]:
92+
session: Session = Session(engine, expire_on_commit=False)
93+
try:
94+
yield session
95+
except Exception as e:
96+
session.rollback()
97+
raise e
98+
finally:
99+
session.close()
100+
101+
92102
@app.get("/categories", response_model=List[CategoryOut])
93-
def get_all():
94-
with Session(engine) as session:
95-
return session.execute(select(Category)).all()
103+
async def get_all(session: Session = Depends(get_session)):
104+
return session.execute(select(Category)).scalars().all()
96105

97106

98107
@app.get("/categories/{id}", response_model=CategoryOut)
99-
def get_one(id: int = Path(...)):
100-
with Session(engine) as session:
101-
category = session.get(Category, id)
102-
if category is not None:
103-
return category
104-
return JSONResponse({"detail": "Not found"}, status_code=404)
108+
async def get_one(id: int = Path(...), session: Session = Depends(get_session)):
109+
category = session.get(Category, id)
110+
if category is not None:
111+
return category
112+
return JSONResponse({"detail": "Not found"}, status_code=404)
105113

106114

107115
@app.post("/categories", response_model=CategoryOut)
108-
def create_new(category: Category = Depends(category_form)):
109-
with Session(engine) as session:
110-
try:
111-
session.add(category)
112-
session.commit()
113-
session.refresh(category)
114-
return category
115-
except ValidationError as e:
116-
return JSONResponse(
117-
dict(error={"key": e.key, "msg": e.msg}), status_code=422
118-
)
116+
async def create_new(
117+
category: Category = Depends(category_form), session: Session = Depends(get_session)
118+
):
119+
try:
120+
session.add(category)
121+
session.commit()
122+
session.refresh(category)
123+
return category
124+
except ValidationError as e:
125+
return JSONResponse(dict(error={"key": e.key, "msg": e.msg}), status_code=422)
119126

120127

121128
@app.get("/medias/{storage}/{file_id}", response_class=FileResponse)
122-
def serve_files(storage: str = Path(...), file_id: str = Path(...)):
129+
async def serve_files(storage: str = Path(...), file_id: str = Path(...)):
123130
try:
124131
file = StorageManager.get_file(f"{storage}/{file_id}")
125132
if isinstance(file.object.driver, LocalStorageDriver):

examples/flask/app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Book(db.Model):
3030
ImageField(
3131
upload_storage="images",
3232
thumbnail_size=(50, 50),
33-
validators=[SizeValidator("16M")],
33+
validators=[SizeValidator("1M")],
3434
)
3535
)
3636
document = db.Column(

pyproject.toml

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "sqlalchemy-file"
3-
version = "0.1.4"
3+
version = "0.1.5"
44
description = "SQLAlchemy-file is a SQLAlchemy extension for attaching files to SQLAlchemy model and uploading them to various storage."
55
authors = ["Jocelin Hounon <[email protected]>"]
66
license = "MIT"
@@ -29,27 +29,28 @@ classifiers = [
2929
[tool.poetry.dependencies]
3030
python = "^3.7"
3131
SQLAlchemy = ">=1.4,<1.5.0"
32-
apache-libcloud = "^3.6.0"
32+
apache-libcloud = ">=3.6.0,<3.7"
3333

3434
[tool.poetry.dev-dependencies]
3535
pytest = "^7.1.2"
3636
sqlmodel = "^0.0.8"
3737
Pillow = "^9.2.0"
38-
fasteners = "^0.17.3"
38+
fasteners = "^0.18"
3939
black = "^22.6.0"
4040
coverage = { extras = ["toml"], version = "^6.4.2" }
4141
flake8 = "^3.9.2"
42-
mypy = "^0.971"
42+
mypy = "^0.982"
4343
isort = "^5.10.1"
4444
mkdocs-material = "^8.4.3"
4545
PyMySQL = { extras = ["rsa"], version = "^1.0.2" }
4646
psycopg2-binary = "^2.9.3"
4747
mkdocstrings = { extras = ["python"], version = "^0.19.0" }
48-
fastapi = "^0.82.0"
48+
fastapi = "^0.85.1"
4949
uvicorn = "^0.18.2"
5050
python-multipart = "^0.0.5"
5151
Flask = "^2.2.2"
5252
Flask-SQLAlchemy = "^2.5.1"
53+
importlib-metadata = { version = "<5.0", python = "<=3.7" }
5354

5455
[tool.coverage.report]
5556
fail_under = 95

sqlalchemy_file/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.1.4"
1+
__version__ = "0.1.5"
22

33
from .file import File as File
44
from .types import FileField as FileField

sqlalchemy_file/py.typed

Whitespace-only changes.

sqlalchemy_file/types.py

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class ImageField(FileField):
103103
but also validates that the uploaded object is a valid image.
104104
"""
105105

106+
cache_ok = True
107+
106108
def __init__(
107109
self,
108110
*args: Tuple[Any],

0 commit comments

Comments
 (0)