Skip to content

Commit 9201430

Browse files
committed
Fixed mypy complaints
1 parent 71978b9 commit 9201430

File tree

8 files changed

+52
-106
lines changed

8 files changed

+52
-106
lines changed

.idea/runConfigurations/db_revision.xml

Lines changed: 0 additions & 24 deletions
This file was deleted.

.idea/runConfigurations/db_upgrade__head_.xml

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/mrmat_python_api_fastapi/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323

2424
from .config import Config
2525
app_config = Config.from_json_file()
26+
from sqlalchemy.orm import DeclarativeBase
2627

27-
from sqlalchemy.orm import declarative_base
28-
Base = declarative_base()
28+
class ORMBase(DeclarativeBase):
29+
pass
2930

3031

31-
class BaseSchema(BaseModel):
32+
class SchemaBase(BaseModel):
3233
model_config = {'from_attributes': True}

src/mrmat_python_api_fastapi/apis/platform/v1/api.py

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222

23-
from typing import Union
2423
from fastapi import APIRouter, Depends, status, HTTPException
25-
from fastapi.responses import JSONResponse, Response
24+
from fastapi.responses import Response
2625
from sqlalchemy.exc import SQLAlchemyError
2726
from sqlalchemy.orm import Session
2827

@@ -49,7 +48,10 @@
4948
async def get_resources(session: Session = Depends(get_db)) -> ResourceListSchema:
5049
try:
5150
resources = session.query(Resource).all()
52-
return ResourceListSchema(resources=resources)
51+
return ResourceListSchema(resources=[ResourceSchema(uid=r.uid,
52+
name=r.name,
53+
owner_uid=r.owner_uid)
54+
for r in resources])
5355
except SQLAlchemyError as e:
5456
# Handle the error appropriately, maybe raise an HTTPException
5557
raise HTTPException(status_code=500, detail="A database error occurred") from e
@@ -86,12 +88,7 @@ async def get_resource(uid: str,
8688
name='create_resource',
8789
summary='Create a resource',
8890
description='Create a resource owned by the authenticated user',
89-
responses={
90-
status.HTTP_201_CREATED: {
91-
'description': 'The resource was created',
92-
'model': ResourceSchema
93-
},
94-
})
91+
response_model=ResourceSchema)
9592
async def create_resource(data: ResourceInputSchema,
9693
response: Response,
9794
session: Session = Depends(get_db)):
@@ -100,9 +97,7 @@ async def create_resource(data: ResourceInputSchema,
10097
session.add(resource)
10198
session.commit()
10299
response.status_code = 201
103-
return ResourceSchema(uid=resource.uid,
104-
owner_uid=resource.owner_uid,
105-
name=resource.name)
100+
return resource
106101
except SQLAlchemyError as e:
107102
raise HTTPException(status_code=500, detail="A database error occurred") from e
108103

@@ -120,7 +115,8 @@ async def create_resource(data: ResourceInputSchema,
120115
'description': 'The resource was not found',
121116
'model': StatusSchema
122117
}
123-
})
118+
},
119+
response_model=ResourceSchema)
124120
async def modify_resource(uid: str,
125121
data: ResourceInputSchema,
126122
response: Response,
@@ -133,9 +129,7 @@ async def modify_resource(uid: str,
133129
resource.name = data.name
134130
session.add(resource)
135131
session.commit()
136-
return ResourceSchema(uid=resource.uid,
137-
owner_uid=resource.owner_uid,
138-
name=resource.name)
132+
return resource
139133
except SQLAlchemyError as e:
140134
raise HTTPException(status_code=500, detail="A database error occurred") from e
141135

@@ -176,10 +170,10 @@ async def remove_resource(uid: str,
176170
summary='List all known owners',
177171
description='Returns all currently known owners and their metadata',
178172
response_model=OwnerListSchema)
179-
async def get_owners(db: Session = Depends(get_db)):
173+
async def get_owners(session: Session = Depends(get_db)):
180174
try:
181-
owners = db.query(Owner).all()
182-
return OwnerListSchema(owners=owners)
175+
owners = session.query(Owner).all()
176+
return OwnerListSchema(owners=[OwnerSchema(uid=o.uid, name=o.name) for o in owners])
183177
except SQLAlchemyError as e:
184178
# Handle the error appropriately, maybe raise an HTTPException
185179
raise HTTPException(status_code=500, detail="A database error occurred") from e
@@ -196,9 +190,10 @@ async def get_owners(db: Session = Depends(get_db)):
196190
},
197191
status.HTTP_200_OK: {
198192
'description': 'The requested owner',
199-
'model': ResourceSchema
193+
'model': OwnerSchema
200194
}
201-
})
195+
},
196+
response_model=OwnerSchema)
202197
async def get_owner(uid: str,
203198
response: Response,
204199
session: Session = Depends(get_db)):
@@ -216,9 +211,7 @@ async def get_owner(uid: str,
216211
name='create_owner',
217212
summary='Create a owner',
218213
description='Create a owner',
219-
responses={
220-
status.HTTP_201_CREATED: {'description': 'The owner was created', 'model': OwnerSchema}
221-
})
214+
response_model=OwnerSchema)
222215
async def create_owner(data: OwnerInputSchema,
223216
response: Response,
224217
session: Session = Depends(get_db)):
@@ -227,7 +220,7 @@ async def create_owner(data: OwnerInputSchema,
227220
session.add(owner)
228221
session.commit()
229222
response.status_code = 201
230-
return OwnerSchema(uid=owner.uid, name=owner.name)
223+
return owner
231224
except SQLAlchemyError as e:
232225
# Handle the error appropriately, maybe raise an HTTPException
233226
raise HTTPException(status_code=500, detail="A database error occurred") from e
@@ -246,7 +239,8 @@ async def create_owner(data: OwnerInputSchema,
246239
'description': 'The owner was not found',
247240
'model': StatusSchema
248241
}
249-
})
242+
},
243+
response_model=OwnerSchema)
250244
async def modify_owner(uid: str,
251245
data: OwnerInputSchema,
252246
response: Response,
@@ -259,7 +253,7 @@ async def modify_owner(uid: str,
259253
owner.name = data.name
260254
session.add(owner)
261255
session.commit()
262-
return OwnerSchema(uid=owner.uid, name=owner.name)
256+
return owner
263257
except SQLAlchemyError as e:
264258
raise HTTPException(status_code=500, detail="A database error occurred") from e
265259

src/mrmat_python_api_fastapi/apis/platform/v1/db.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,28 @@
2121
# SOFTWARE.
2222

2323
import uuid
24-
from sqlalchemy import ForeignKey, Column, String, UniqueConstraint, UUID
25-
from sqlalchemy.orm import relationship
24+
from sqlalchemy import ForeignKey, String, UniqueConstraint, UUID
25+
from sqlalchemy.orm import relationship, Mapped, mapped_column
2626

27-
from mrmat_python_api_fastapi import Base
27+
from mrmat_python_api_fastapi import ORMBase
2828

2929

30-
class Owner(Base):
30+
class Owner(ORMBase):
3131
__tablename__ = 'owners'
3232
__schema__ = 'mrmat-python-api-fastapi'
33-
#uid = Column(BigInteger().with_variant(Integer, 'sqlite'), primary_key=True)
34-
uid = Column(String, primary_key=True, default=str(uuid.uuid4()))
35-
client_id = Column(String(255), nullable=False, unique=True)
36-
name = Column(String(255), nullable=False)
37-
resources = relationship('Resource', back_populates='owner')
33+
uid: Mapped[str] = mapped_column(String, primary_key=True, default=str(uuid.uuid4()))
3834

35+
client_id: Mapped[str] = mapped_column(String(255), nullable=False, unique=True)
36+
name: Mapped[str] = mapped_column(String(255), nullable=False)
37+
resources: Mapped[list["Resource"]] = relationship('Resource', back_populates='owner')
3938

40-
class Resource(Base):
39+
40+
class Resource(ORMBase):
4141
__tablename__ = 'resources'
4242
__schema__ = 'mrmat-python-api-fastapi'
43-
uid = Column(String, primary_key=True, default=str(uuid.uuid4()))
44-
owner_uid = Column(String, ForeignKey('owners.uid'), nullable=False)
45-
name = Column(String(255), nullable=False)
43+
uid: Mapped[str] = mapped_column(String, primary_key=True, default=str(uuid.uuid4()))
44+
owner_uid: Mapped[str] = mapped_column(String, ForeignKey('owners.uid'), nullable=False)
45+
name: Mapped[str] = mapped_column(String(255), nullable=False)
4646

47-
owner = relationship('Owner', back_populates='resources')
48-
UniqueConstraint('owner_uid', 'name', name='no_duplicate_names_per_owner')
47+
owner: Mapped["Owner"] = relationship('Owner', back_populates='resources')
48+
__table_args__ = (UniqueConstraint('owner_uid', 'name', name='no_duplicate_names_per_owner'),)

src/mrmat_python_api_fastapi/apis/platform/v1/schema.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,24 @@
2222

2323
import typing
2424

25-
from mrmat_python_api_fastapi import BaseSchema
25+
from mrmat_python_api_fastapi import SchemaBase
2626

2727

28-
class OwnerInputSchema(BaseSchema):
28+
class OwnerInputSchema(SchemaBase):
2929
name: str
3030

3131
class OwnerSchema(OwnerInputSchema):
3232
uid: str
3333

34-
class OwnerListSchema(BaseSchema):
35-
owners: typing.List[OwnerSchema]
34+
class OwnerListSchema(SchemaBase):
35+
owners: typing.Sequence[OwnerSchema]
3636

37-
class ResourceInputSchema(BaseSchema):
37+
class ResourceInputSchema(SchemaBase):
3838
name: str
3939
owner_uid: str
4040

4141
class ResourceSchema(ResourceInputSchema):
4242
uid: str
43-
name: str
4443

45-
class ResourceListSchema(BaseSchema):
46-
resources: typing.List[ResourceSchema]
44+
class ResourceListSchema(SchemaBase):
45+
resources: typing.Sequence[ResourceSchema]

src/mrmat_python_api_fastapi/db.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from sqlalchemy import create_engine
2626
from sqlalchemy.orm import Session, sessionmaker
2727

28-
from mrmat_python_api_fastapi import app_config, Base
28+
from mrmat_python_api_fastapi import app_config, ORMBase
2929

3030

3131
@lru_cache
@@ -34,6 +34,6 @@ def get_db() -> Session:
3434
engine = create_engine(url=app_config.db_url, connect_args={'check_same_thread': False})
3535
else:
3636
engine = create_engine(url=app_config.db_url)
37-
session_local = sessionmaker(bind=engine)
38-
Base.metadata.create_all(bind=engine)
37+
session_local = sessionmaker(autocommit=False, autoflush=False, bind=engine)
38+
ORMBase.metadata.create_all(bind=engine)
3939
return session_local()

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import pytest
2525
import fastapi.testclient
2626

27-
from mrmat_python_api_fastapi import app_config, Base
27+
from mrmat_python_api_fastapi import app_config, ORMBase
2828
from mrmat_python_api_fastapi.app import app
2929
from mrmat_python_api_fastapi.db import get_db
3030

@@ -46,5 +46,5 @@ def client(test_db_path) -> fastapi.testclient.TestClient:
4646
app_config.db_url = f'sqlite:///{test_db_path}'
4747
session = get_db()
4848
with session.begin():
49-
Base.metadata.create_all(session.bind)
49+
ORMBase.metadata.create_all(session.bind)
5050
return fastapi.testclient.TestClient(app)

0 commit comments

Comments
 (0)