Skip to content

Commit 16271bb

Browse files
authored
Merge pull request #217 from benavlabs/update-fastcrud-0-19-1
update to fastcrud 0.19.1, better typing
2 parents ad86b28 + 730d1ac commit 16271bb

File tree

8 files changed

+24
-58
lines changed

8 files changed

+24
-58
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ dependencies = [
2828
"arq>=0.25.0",
2929
"bcrypt>=4.1.1",
3030
"psycopg2-binary>=2.9.9",
31-
"fastcrud>=0.19.0",
31+
"fastcrud>=0.19.1",
3232
"crudadmin>=0.4.2",
3333
"gunicorn>=23.0.0",
3434
"ruff>=0.11.13",

src/app/api/dependencies.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Annotated, Any, cast
1+
from typing import Annotated, Any
22

33
from fastapi import Depends, HTTPException, Request
44
from sqlalchemy.ext.asyncio import AsyncSession
@@ -83,14 +83,14 @@ async def rate_limiter_dependency(
8383
user_id = user["id"]
8484
tier = await crud_tiers.get(db, id=user["tier_id"], schema_to_select=TierRead)
8585
if tier:
86-
tier = cast(TierRead, tier)
87-
rate_limit = await crud_rate_limits.get(db=db, tier_id=tier.id, path=path, schema_to_select=RateLimitRead)
86+
rate_limit = await crud_rate_limits.get(
87+
db=db, tier_id=tier["id"], path=path, schema_to_select=RateLimitRead
88+
)
8889
if rate_limit:
89-
rate_limit = cast(RateLimitRead, rate_limit)
90-
limit, period = rate_limit.limit, rate_limit.period
90+
limit, period = rate_limit["limit"], rate_limit["period"]
9191
else:
9292
logger.warning(
93-
f"User {user_id} with tier '{tier.name}' has no specific rate limit for path '{path}'. \
93+
f"User {user_id} with tier '{tier['name']}' has no specific rate limit for path '{path}'. \
9494
Applying default rate limit."
9595
)
9696
limit, period = DEFAULT_LIMIT, DEFAULT_PERIOD

src/app/api/v1/posts.py

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Annotated, Any, cast
1+
from typing import Annotated, Any
22

33
from fastapi import APIRouter, Depends, Request
44
from fastcrud import PaginatedListResponse, compute_offset, paginated_response
@@ -28,8 +28,6 @@ async def write_post(
2828
if db_user is None:
2929
raise NotFoundException("User not found")
3030

31-
db_user = cast(dict[str, Any], db_user)
32-
3331
if current_user["id"] != db_user["id"]:
3432
raise ForbiddenException()
3533

@@ -46,7 +44,7 @@ async def write_post(
4644
if post_read is None:
4745
raise NotFoundException("Created post not found")
4846

49-
return cast(dict[str, Any], post_read)
47+
return post_read
5048

5149

5250
@router.get("/{username}/posts", response_model=PaginatedListResponse[PostRead])
@@ -66,7 +64,6 @@ async def read_posts(
6664
if not db_user:
6765
raise NotFoundException("User not found")
6866

69-
db_user = cast(dict[str, Any], db_user)
7067
posts_data = await crud_posts.get_multi(
7168
db=db,
7269
offset=compute_offset(page, items_per_page),
@@ -88,15 +85,13 @@ async def read_post(
8885
if db_user is None:
8986
raise NotFoundException("User not found")
9087

91-
db_user = cast(dict[str, Any], db_user)
92-
9388
db_post = await crud_posts.get(
9489
db=db, id=id, created_by_user_id=db_user["id"], is_deleted=False, schema_to_select=PostRead
9590
)
9691
if db_post is None:
9792
raise NotFoundException("Post not found")
9893

99-
return cast(dict[str, Any], db_post)
94+
return db_post
10095

10196

10297
@router.patch("/{username}/post/{id}")
@@ -113,17 +108,13 @@ async def patch_post(
113108
if db_user is None:
114109
raise NotFoundException("User not found")
115110

116-
db_user = cast(dict[str, Any], db_user)
117-
118111
if current_user["id"] != db_user["id"]:
119112
raise ForbiddenException()
120113

121114
db_post = await crud_posts.get(db=db, id=id, is_deleted=False, schema_to_select=PostRead)
122115
if db_post is None:
123116
raise NotFoundException("Post not found")
124117

125-
db_post = cast(dict[str, Any], db_post)
126-
127118
await crud_posts.update(db=db, object=values, id=id)
128119
return {"message": "Post updated"}
129120

@@ -141,17 +132,13 @@ async def erase_post(
141132
if db_user is None:
142133
raise NotFoundException("User not found")
143134

144-
db_user = cast(dict[str, Any], db_user)
145-
146135
if current_user["id"] != db_user["id"]:
147136
raise ForbiddenException()
148137

149138
db_post = await crud_posts.get(db=db, id=id, is_deleted=False, schema_to_select=PostRead)
150139
if db_post is None:
151140
raise NotFoundException("Post not found")
152141

153-
db_post = cast(dict[str, Any], db_post)
154-
155142
await crud_posts.delete(db=db, id=id)
156143

157144
return {"message": "Post deleted"}
@@ -166,13 +153,9 @@ async def erase_db_post(
166153
if db_user is None:
167154
raise NotFoundException("User not found")
168155

169-
db_user = cast(dict[str, Any], db_user)
170-
171156
db_post = await crud_posts.get(db=db, id=id, is_deleted=False, schema_to_select=PostRead)
172157
if db_post is None:
173158
raise NotFoundException("Post not found")
174159

175-
db_post = cast(dict[str, Any], db_post)
176-
177160
await crud_posts.db_delete(db=db, id=id)
178161
return {"message": "Post deleted from the database"}

src/app/api/v1/rate_limits.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Annotated, Any, cast
1+
from typing import Annotated, Any
22

33
from fastapi import APIRouter, Depends, Request
44
from fastcrud import PaginatedListResponse, compute_offset, paginated_response
@@ -23,9 +23,6 @@ async def write_rate_limit(
2323
if not db_tier:
2424
raise NotFoundException("Tier not found")
2525

26-
db_tier = cast(dict[str, Any], db_tier)
27-
28-
db_tier = cast(dict[str, Any], db_tier)
2926
rate_limit_internal_dict = rate_limit.model_dump()
3027
rate_limit_internal_dict["tier_id"] = db_tier["id"]
3128

@@ -43,7 +40,7 @@ async def write_rate_limit(
4340
if rate_limit_read is None:
4441
raise NotFoundException("Created rate limit not found")
4542

46-
return cast(dict[str, Any], rate_limit_read)
43+
return rate_limit_read
4744

4845

4946
@router.get("/tier/{tier_name}/rate_limits", response_model=PaginatedListResponse[RateLimitRead])
@@ -58,8 +55,6 @@ async def read_rate_limits(
5855
if not db_tier:
5956
raise NotFoundException("Tier not found")
6057

61-
db_tier = cast(dict[str, Any], db_tier)
62-
6358
rate_limits_data = await crud_rate_limits.get_multi(
6459
db=db,
6560
offset=compute_offset(page, items_per_page),
@@ -79,13 +74,11 @@ async def read_rate_limit(
7974
if not db_tier:
8075
raise NotFoundException("Tier not found")
8176

82-
db_tier = cast(dict[str, Any], db_tier)
83-
8477
db_rate_limit = await crud_rate_limits.get(db=db, tier_id=db_tier["id"], id=id, schema_to_select=RateLimitRead)
8578
if db_rate_limit is None:
8679
raise NotFoundException("Rate Limit not found")
8780

88-
return cast(dict[str, Any], db_rate_limit)
81+
return db_rate_limit
8982

9083

9184
@router.patch("/tier/{tier_name}/rate_limit/{id}", dependencies=[Depends(get_current_superuser)])
@@ -100,8 +93,6 @@ async def patch_rate_limit(
10093
if not db_tier:
10194
raise NotFoundException("Tier not found")
10295

103-
db_tier = cast(dict[str, Any], db_tier)
104-
10596
db_rate_limit = await crud_rate_limits.get(db=db, tier_id=db_tier["id"], id=id, schema_to_select=RateLimitRead)
10697
if db_rate_limit is None:
10798
raise NotFoundException("Rate Limit not found")
@@ -118,8 +109,6 @@ async def erase_rate_limit(
118109
if not db_tier:
119110
raise NotFoundException("Tier not found")
120111

121-
db_tier = cast(dict[str, Any], db_tier)
122-
123112
db_rate_limit = await crud_rate_limits.get(db=db, tier_id=db_tier["id"], id=id, schema_to_select=RateLimitRead)
124113
if db_rate_limit is None:
125114
raise NotFoundException("Rate Limit not found")

src/app/api/v1/tiers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Annotated, Any, cast
1+
from typing import Annotated, Any
22

33
from fastapi import APIRouter, Depends, Request
44
from fastcrud import PaginatedListResponse, compute_offset, paginated_response
@@ -32,7 +32,7 @@ async def write_tier(
3232
if tier_read is None:
3333
raise NotFoundException("Created tier not found")
3434

35-
return cast(dict[str, Any], tier_read)
35+
return tier_read
3636

3737

3838
@router.get("/tiers", response_model=PaginatedListResponse[TierRead])
@@ -51,7 +51,7 @@ async def read_tier(request: Request, name: str, db: Annotated[AsyncSession, Dep
5151
if db_tier is None:
5252
raise NotFoundException("Tier not found")
5353

54-
return cast(dict[str, Any], db_tier)
54+
return db_tier
5555

5656

5757
@router.patch("/tier/{name}", dependencies=[Depends(get_current_superuser)])

src/app/api/v1/users.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Annotated, Any, cast
1+
from typing import Annotated, Any
22

33
from fastapi import APIRouter, Depends, Request
44
from fastcrud import PaginatedListResponse, compute_offset, paginated_response
@@ -43,7 +43,7 @@ async def write_user(
4343
if user_read is None:
4444
raise NotFoundException("Created user not found")
4545

46-
return cast(dict[str, Any], user_read)
46+
return user_read
4747

4848

4949
@router.get("/users", response_model=PaginatedListResponse[UserRead])
@@ -74,7 +74,7 @@ async def read_user(
7474
if db_user is None:
7575
raise NotFoundException("User not found")
7676

77-
return cast(dict[str, Any], db_user)
77+
return db_user
7878

7979

8080
@router.patch("/user/{username}")
@@ -151,7 +151,6 @@ async def read_user_rate_limits(
151151
if db_user is None:
152152
raise NotFoundException("User not found")
153153

154-
db_user = cast(dict[str, Any], db_user)
155154
user_dict = dict(db_user)
156155
if db_user["tier_id"] is None:
157156
user_dict["tier_rate_limits"] = []
@@ -161,7 +160,6 @@ async def read_user_rate_limits(
161160
if db_tier is None:
162161
raise NotFoundException("Tier not found")
163162

164-
db_tier = cast(dict[str, Any], db_tier)
165163
db_rate_limits = await crud_rate_limits.get_multi(db=db, tier_id=db_tier["id"])
166164

167165
user_dict["tier_rate_limits"] = db_rate_limits["data"]
@@ -177,15 +175,13 @@ async def read_user_tier(
177175
if db_user is None:
178176
raise NotFoundException("User not found")
179177

180-
db_user = cast(dict[str, Any], db_user)
181178
if db_user["tier_id"] is None:
182179
return None
183180

184181
db_tier = await crud_tiers.get(db=db, id=db_user["tier_id"], schema_to_select=TierRead)
185182
if not db_tier:
186183
raise NotFoundException("Tier not found")
187184

188-
db_tier = cast(dict[str, Any], db_tier)
189185
user_dict = dict(db_user)
190186
tier_dict = dict(db_tier)
191187

@@ -203,7 +199,6 @@ async def patch_user_tier(
203199
if db_user is None:
204200
raise NotFoundException("User not found")
205201

206-
db_user = cast(dict[str, Any], db_user)
207202
db_tier = await crud_tiers.get(db=db, id=values.tier_id, schema_to_select=TierRead)
208203
if db_tier is None:
209204
raise NotFoundException("Tier not found")

src/app/core/security.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import UTC, datetime, timedelta
22
from enum import Enum
3-
from typing import Any, Literal, cast
3+
from typing import Any, Literal
44

55
import bcrypt
66
from fastapi.security import OAuth2PasswordBearer
@@ -45,7 +45,6 @@ async def authenticate_user(username_or_email: str, password: str, db: AsyncSess
4545
if not db_user:
4646
return False
4747

48-
db_user = cast(dict[str, Any], db_user)
4948
if not await verify_password(password, db_user["hashed_password"]):
5049
return False
5150

uv.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)