Skip to content

Commit e0b413e

Browse files
committed
Bump FastAPI to 0.95.0
This release adds support for dependencies and parameters using Annotated and recommends its usage
1 parent 07ff82c commit e0b413e

File tree

9 files changed

+55
-57
lines changed

9 files changed

+55
-57
lines changed

api/database.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
from typing import Annotated
2+
3+
from fastapi import Depends
14
from sqlalchemy import BigInteger, Boolean, Column, Enum, ForeignKey, Index, Integer, PrimaryKeyConstraint, Text, text
25
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
36
from sqlalchemy.ext.declarative import declarative_base
47
from sqlalchemy.orm import relationship, sessionmaker
58

69
from api.constants import Config
10+
from api.dependencies import get_db_session
711

812
engine = create_async_engine(Config.DATABASE_URL)
913
Base = declarative_base()
1014

1115
Session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
16+
DBSession = Annotated[AsyncSession, Depends(get_db_session)]
1217

1318

1419
class TeamUser(Base):

api/routers/codejams.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
from typing import Optional
22

3-
from fastapi import APIRouter, Depends, HTTPException
3+
from fastapi import APIRouter, HTTPException
44
from sqlalchemy import desc, update
5-
from sqlalchemy.ext.asyncio import AsyncSession
65
from sqlalchemy.future import select
76

8-
from api.database import Jam, Team, TeamUser, User
9-
from api.dependencies import get_db_session
7+
from api.database import DBSession, Jam, Team, TeamUser, User
108
from api.models import CodeJam, CodeJamResponse
119

1210
router = APIRouter(prefix="/codejams", tags=["codejams"])
1311

1412

1513
@router.get("/")
16-
async def get_codejams(session: AsyncSession = Depends(get_db_session)) -> list[CodeJamResponse]:
14+
async def get_codejams(session: DBSession) -> list[CodeJamResponse]:
1715
"""Get all the codejams stored in the database."""
1816
codejams = await session.execute(select(Jam).order_by(desc(Jam.id)))
1917
codejams.unique()
@@ -25,7 +23,7 @@ async def get_codejams(session: AsyncSession = Depends(get_db_session)) -> list[
2523
"/{codejam_id}",
2624
responses={404: {"description": "CodeJam could not be found or there is no ongoing code jam."}},
2725
)
28-
async def get_codejam(codejam_id: int, session: AsyncSession = Depends(get_db_session)) -> CodeJamResponse:
26+
async def get_codejam(codejam_id: int, session: DBSession) -> CodeJamResponse:
2927
"""
3028
Get a specific codejam stored in the database by ID.
3129
@@ -52,9 +50,9 @@ async def get_codejam(codejam_id: int, session: AsyncSession = Depends(get_db_se
5250
@router.patch("/{codejam_id}", responses={404: {"description": "Code Jam with specified ID does not exist."}})
5351
async def modify_codejam(
5452
codejam_id: int,
53+
session: DBSession,
5554
name: Optional[str] = None,
5655
ongoing: Optional[bool] = None,
57-
session: AsyncSession = Depends(get_db_session),
5856
) -> CodeJamResponse:
5957
"""Modify the specified codejam to change its name and/or whether it's the ongoing code jam."""
6058
codejam = await session.execute(select(Jam).where(Jam.id == codejam_id))
@@ -80,7 +78,7 @@ async def modify_codejam(
8078

8179

8280
@router.post("/")
83-
async def create_codejam(codejam: CodeJam, session: AsyncSession = Depends(get_db_session)) -> CodeJamResponse:
81+
async def create_codejam(codejam: CodeJam, session: DBSession) -> CodeJamResponse:
8482
"""
8583
Create a new codejam and get back the one just created.
8684

api/routers/infractions.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
from fastapi import APIRouter, Depends, HTTPException
2-
from sqlalchemy.ext.asyncio import AsyncSession
1+
from fastapi import APIRouter, HTTPException
32
from sqlalchemy.future import select
43

4+
from api.database import DBSession
55
from api.database import Infraction as DbInfraction
66
from api.database import Jam, User
7-
from api.dependencies import get_db_session
87
from api.models import Infraction, InfractionResponse
98

109
router = APIRouter(prefix="/infractions", tags=["infractions"])
1110

1211

1312
@router.get("/")
14-
async def get_infractions(session: AsyncSession = Depends(get_db_session)) -> list[InfractionResponse]:
13+
async def get_infractions(session: DBSession) -> list[InfractionResponse]:
1514
"""Get every infraction stored in the database."""
1615
infractions = await session.execute(select(DbInfraction))
1716
infractions.unique()
@@ -23,7 +22,7 @@ async def get_infractions(session: AsyncSession = Depends(get_db_session)) -> li
2322
"/{infraction_id}",
2423
responses={404: {"description": "Infraction could not be found."}},
2524
)
26-
async def get_infraction(infraction_id: int, session: AsyncSession = Depends(get_db_session)) -> InfractionResponse:
25+
async def get_infraction(infraction_id: int, session: DBSession) -> InfractionResponse:
2726
"""Get a specific infraction stored in the database by ID."""
2827
infraction_result = await session.execute(select(DbInfraction).where(DbInfraction.id == infraction_id))
2928
infraction_result.unique()
@@ -40,7 +39,7 @@ async def get_infraction(infraction_id: int, session: AsyncSession = Depends(get
4039
)
4140
async def create_infraction(
4241
infraction: Infraction,
43-
session: AsyncSession = Depends(get_db_session),
42+
session: DBSession,
4443
) -> InfractionResponse:
4544
"""Add an infraction for a user to the database."""
4645
jam_id = (await session.execute(select(Jam.id).where(Jam.id == infraction.jam_id))).scalars().one_or_none()

api/routers/teams.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from typing import Optional
22

3-
from fastapi import APIRouter, Depends, HTTPException, Response
3+
from fastapi import APIRouter, HTTPException, Response
44
from sqlalchemy import func
55
from sqlalchemy.ext.asyncio import AsyncSession
66
from sqlalchemy.future import select
77

8-
from api.database import Jam, Team, TeamUser
8+
from api.database import DBSession, Jam, Team, TeamUser
99
from api.database import User as DbUser
10-
from api.dependencies import get_db_session
1110
from api.models import TeamResponse, User
1211

1312
router = APIRouter(prefix="/teams", tags=["teams"])
@@ -36,7 +35,7 @@ async def ensure_user_exists(user_id: int, session: AsyncSession) -> DbUser:
3635

3736

3837
@router.get("/")
39-
async def get_teams(current_jam: bool = False, session: AsyncSession = Depends(get_db_session)) -> list[TeamResponse]:
38+
async def get_teams(session: DBSession, current_jam: bool = False) -> list[TeamResponse]:
4039
"""Get every code jam team in the database."""
4140
if not current_jam:
4241
teams = await session.execute(select(Team))
@@ -49,7 +48,9 @@ async def get_teams(current_jam: bool = False, session: AsyncSession = Depends(g
4948

5049
@router.get("/find", responses={404: {"description": "Team could not be found."}})
5150
async def find_team_by_name(
52-
name: str, jam_id: Optional[int] = None, session: AsyncSession = Depends(get_db_session)
51+
name: str,
52+
session: DBSession,
53+
jam_id: Optional[int] = None,
5354
) -> TeamResponse:
5455
"""Get a specific code jam team by name."""
5556
if jam_id is None:
@@ -70,13 +71,13 @@ async def find_team_by_name(
7071

7172

7273
@router.get("/{team_id}", responses={404: {"description": "Team could not be found."}})
73-
async def get_team(team_id: int, session: AsyncSession = Depends(get_db_session)) -> TeamResponse:
74+
async def get_team(team_id: int, session: DBSession) -> TeamResponse:
7475
"""Get a specific code jam team in the database by ID."""
7576
return await ensure_team_exists(team_id, session)
7677

7778

7879
@router.get("/{team_id}/users", responses={404: {"description": "Team could not be found."}})
79-
async def get_team_users(team_id: int, session: AsyncSession = Depends(get_db_session)) -> list[User]:
80+
async def get_team_users(team_id: int, session: DBSession) -> list[User]:
8081
"""Get the users of a specific code jam team in the database."""
8182
await ensure_team_exists(team_id, session)
8283

@@ -95,9 +96,7 @@ async def get_team_users(team_id: int, session: AsyncSession = Depends(get_db_se
9596
400: {"description": "This user is already on the team."},
9697
},
9798
)
98-
async def add_user_to_team(
99-
team_id: int, user_id: int, is_leader: bool = False, session: AsyncSession = Depends(get_db_session)
100-
) -> User:
99+
async def add_user_to_team(team_id: int, user_id: int, session: DBSession, is_leader: bool = False) -> User:
101100
"""Add a user to a specific code jam team in the database."""
102101
await ensure_team_exists(team_id, session)
103102
await ensure_user_exists(user_id, session)
@@ -128,7 +127,9 @@ async def add_user_to_team(
128127
},
129128
)
130129
async def remove_user_from_team(
131-
team_id: int, user_id: int, session: AsyncSession = Depends(get_db_session)
130+
team_id: int,
131+
user_id: int,
132+
session: DBSession,
132133
) -> Response:
133134
"""Remove a user from a specific code jam team in the database."""
134135
await ensure_team_exists(team_id, session)

api/routers/users.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from typing import Any
22

3-
from fastapi import APIRouter, Depends, HTTPException
3+
from fastapi import APIRouter, HTTPException
44
from sqlalchemy.ext.asyncio import AsyncSession
55
from sqlalchemy.future import select
66

7-
from api.database import Jam, TeamUser, User
8-
from api.dependencies import get_db_session
7+
from api.database import DBSession, Jam, TeamUser, User
98
from api.models import UserResponse, UserTeamResponse
109

1110
router = APIRouter(prefix="/users", tags=["users"])
@@ -48,7 +47,7 @@ async def get_user_data(session: AsyncSession, user_id: int) -> dict[str, Any]:
4847

4948

5049
@router.get("/")
51-
async def get_users(session: AsyncSession = Depends(get_db_session)) -> list[UserResponse]:
50+
async def get_users(session: DBSession) -> list[UserResponse]:
5251
"""Get information about all the users stored in the database."""
5352
users = await session.execute(select(User.id))
5453
users.unique()
@@ -57,7 +56,7 @@ async def get_users(session: AsyncSession = Depends(get_db_session)) -> list[Use
5756

5857

5958
@router.get("/{user_id}", responses={404: {"description": "User could not be found."}})
60-
async def get_user(user_id: int, session: AsyncSession = Depends(get_db_session)) -> UserResponse:
59+
async def get_user(user_id: int, session: DBSession) -> UserResponse:
6160
"""Get a specific user stored in the database by ID."""
6261
user = await session.execute(select(User).where(User.id == user_id))
6362
user.unique()
@@ -69,7 +68,7 @@ async def get_user(user_id: int, session: AsyncSession = Depends(get_db_session)
6968

7069

7170
@router.post("/{user_id}", responses={400: {"description": "User already exists."}})
72-
async def create_user(user_id: int, session: AsyncSession = Depends(get_db_session)) -> UserResponse:
71+
async def create_user(user_id: int, session: DBSession) -> UserResponse:
7372
"""Create a new user with the specified ID to the database."""
7473
user = await session.execute(select(User).where(User.id == user_id))
7574
user.unique()
@@ -94,7 +93,7 @@ async def create_user(user_id: int, session: AsyncSession = Depends(get_db_sessi
9493
}
9594
},
9695
)
97-
async def get_current_team(user_id: int, session: AsyncSession = Depends(get_db_session)) -> UserTeamResponse:
96+
async def get_current_team(user_id: int, session: DBSession) -> UserTeamResponse:
9897
"""Get a user's current team information."""
9998
user = await session.execute(select(User).where(User.id == user_id))
10099
user.unique()

api/routers/winners.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
from fastapi import APIRouter, Depends, HTTPException
1+
from fastapi import APIRouter, HTTPException
22
from sqlalchemy import func
3-
from sqlalchemy.ext.asyncio import AsyncSession
43
from sqlalchemy.future import select
54

6-
from api.database import Jam, User
5+
from api.database import DBSession, Jam, User
76
from api.database import Winner as DbWinner
8-
from api.dependencies import get_db_session
97
from api.models import Winner, WinnerResponse
108

119
router = APIRouter(prefix="/winners", tags=["winners"])
@@ -15,7 +13,7 @@
1513
"/{jam_id}",
1614
responses={404: {"description": "The specified codejam could not be found."}},
1715
)
18-
async def get_winners(jam_id: int, session: AsyncSession = Depends(get_db_session)) -> list[WinnerResponse]:
16+
async def get_winners(jam_id: int, session: DBSession) -> list[WinnerResponse]:
1917
"""Get the top ten winners from the specified codejam."""
2018
jam = await session.execute(select(Jam).where(Jam.id == jam_id))
2119
jam.unique()
@@ -38,9 +36,7 @@ async def get_winners(jam_id: int, session: AsyncSession = Depends(get_db_sessio
3836
409: {"description": "One or more users are already a winner in the specified codejam."},
3937
},
4038
)
41-
async def create_winners(
42-
jam_id: int, winners: list[Winner], session: AsyncSession = Depends(get_db_session)
43-
) -> list[WinnerResponse]:
39+
async def create_winners(jam_id: int, winners: list[Winner], session: DBSession) -> list[WinnerResponse]:
4440
"""Add the top ten winners to the specified codejam."""
4541
jam = await session.execute(select(Jam).where(Jam.id == jam_id))
4642
jam.unique()

main-requirements.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ click==8.1.3 ; python_full_version >= "3.11.0" and python_full_version < "3.12.0
4747
colorama==0.4.6 ; python_full_version >= "3.11.0" and python_full_version < "3.12.0" and sys_platform == "win32" or python_full_version >= "3.11.0" and python_full_version < "3.12.0" and platform_system == "Windows" \
4848
--hash=sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44 \
4949
--hash=sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6
50-
fastapi==0.89.0 ; python_full_version >= "3.11.0" and python_full_version < "3.12.0" \
51-
--hash=sha256:34990132751324c988db7bd15bf43a86f5c8580e58a4438e14b12d72fb0ae1d1 \
52-
--hash=sha256:d8cafbd223b5a3c010119ba61fb246d5fd3b8f4766cfa93f922a416650797976
50+
fastapi==0.95.0 ; python_full_version >= "3.11.0" and python_full_version < "3.12.0" \
51+
--hash=sha256:99d4fdb10e9dd9a24027ac1d0bd4b56702652056ca17a6c8721eec4ad2f14e18 \
52+
--hash=sha256:daf73bbe844180200be7966f68e8ec9fd8be57079dff1bacb366db32729e6eb5
5353
greenlet==2.0.2 ; python_full_version >= "3.11.0" and platform_machine == "aarch64" and python_full_version < "3.12.0" or python_full_version >= "3.11.0" and platform_machine == "ppc64le" and python_full_version < "3.12.0" or python_full_version >= "3.11.0" and platform_machine == "x86_64" and python_full_version < "3.12.0" or python_full_version >= "3.11.0" and platform_machine == "amd64" and python_full_version < "3.12.0" or python_full_version >= "3.11.0" and platform_machine == "AMD64" and python_full_version < "3.12.0" or python_full_version >= "3.11.0" and platform_machine == "win32" and python_full_version < "3.12.0" or python_full_version >= "3.11.0" and platform_machine == "WIN32" and python_full_version < "3.12.0" \
5454
--hash=sha256:03a8f4f3430c3b3ff8d10a2a86028c660355ab637cee9333d63d66b56f09d52a \
5555
--hash=sha256:0bf60faf0bc2468089bdc5edd10555bab6e85152191df713e2ab1fcc86382b5a \
@@ -390,9 +390,9 @@ sqlalchemy[asyncio,postgresql-asyncpg]==1.4.45 ; python_full_version >= "3.11.0"
390390
--hash=sha256:f61e54b8c2b389de1a8ad52394729c478c67712dbdcdadb52c2575e41dae94a5 \
391391
--hash=sha256:f7944b04e6fcf8d733964dd9ee36b6a587251a1a4049af3a9b846f6e64eb349a \
392392
--hash=sha256:fd69850860093a3f69fefe0ab56d041edfdfe18510b53d9a2eaecba2f15fa795
393-
starlette==0.22.0 ; python_full_version >= "3.11.0" and python_full_version < "3.12.0" \
394-
--hash=sha256:b092cbc365bea34dd6840b42861bdabb2f507f8671e642e8272d2442e08ea4ff \
395-
--hash=sha256:b5eda991ad5f0ee5d8ce4c4540202a573bb6691ecd0c712262d0bc85cf8f2c50
393+
starlette==0.26.1 ; python_full_version >= "3.11.0" and python_full_version < "3.12.0" \
394+
--hash=sha256:41da799057ea8620e4667a3e69a5b1923ebd32b1819c8fa75634bbe8d8bea9bd \
395+
--hash=sha256:e87fce5d7cbdde34b76f0ac69013fd9d190d581d80681493016666e6f96c6d5e
396396
typing-extensions==4.5.0 ; python_full_version >= "3.11.0" and python_full_version < "3.12.0" \
397397
--hash=sha256:5cb5f4a79139d699607b3ef622a1dedafa84e115ab0024e0d9c044a9479ca7cb \
398398
--hash=sha256:fb33085c39dd998ac16d1431ebc293a8b3eedd00fd4a32de0ff79002c19511b4

poetry.lock

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

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ license = "MIT"
99
python = "3.11.*"
1010

1111
alembic = {version = "1.10.2", extras = ["tz"]}
12-
fastapi = "0.89.0"
12+
fastapi = "0.95.0"
1313
python-decouple = "3.8"
1414
SQLAlchemy = {version = "1.4.45", extras = ["asyncio", "postgresql_asyncpg"]}
1515
uvicorn = {version = "0.21.1", extras = ["standard"]}

0 commit comments

Comments
 (0)