Skip to content

Commit 547f30f

Browse files
committed
Refactoring. Adding the None annotation to the return value
1 parent 3233f40 commit 547f30f

6 files changed

Lines changed: 26 additions & 26 deletions

File tree

‎fastapi__examples/blog_from_stepic/src/blog/repositories.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class MemoryUsersRepository(UsersRepository):
2727
Реализация пользовательского хранилища в оперативной памяти.
2828
Пользователи инициализируются во время инициализации репозитория
2929
"""
30-
def __init__(self):
30+
def __init__(self) -> None:
3131
self.users = [
3232
Admin(
3333
id="29ae7ebf-4445-42f2-9548-a3a54f095220", # это uuid4 – уникальный идентификатор пользователя
@@ -70,13 +70,13 @@ def create_article(self, article: Article):
7070

7171

7272
class ShelveArticlesRepository(ArticlesRepository):
73-
def __init__(self):
73+
def __init__(self) -> None:
7474
self.db_name = "articles"
7575

7676
def get_articles(self) -> list[Article]:
7777
with shelve.open(self.db_name) as db:
7878
return list(db.values())
7979

80-
def create_article(self, article: Article):
80+
def create_article(self, article: Article) -> None:
8181
with shelve.open(self.db_name) as db:
8282
db[article.id] = article

‎fastapi__examples/blog_from_stepic/src/blog/resources.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
@router.get("/", response_class=HTMLResponse)
26-
def index():
26+
def index() -> str:
2727
return """
2828
<div><a href="https://stepik.org/lesson/1186984/step/7?unit=1222202">Урок</a></div>
2929
<br/>

‎fastapi__examples/market_from_stepic/src/market/db.py‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class InvalidException(DbException):
3131

3232

3333
class InvalidOrderStatusException(InvalidException):
34-
def __init__(self, prev_status: models.StatusOrderEnum, new_status: models.StatusOrderEnum):
34+
def __init__(self, prev_status: models.StatusOrderEnum, new_status: models.StatusOrderEnum) -> None:
3535
super().__init__(f"Unable to change order status {prev_status.value!r} to {new_status.value!r}")
3636

3737

@@ -80,10 +80,10 @@ def get_value(self, name: str, default: Any = None) -> Any:
8080
return self.db.get(name)
8181

8282
@session()
83-
def set_value(self, name: str, value: Any):
83+
def set_value(self, name: str, value: Any) -> None:
8484
self.db[name] = value
8585

86-
def __init__(self, file_name: Path | str = DB_FILE_NAME):
86+
def __init__(self, file_name: Path | str = DB_FILE_NAME) -> None:
8787
self.file_name: str = str(file_name)
8888
self.db: shelve.Shelf | None = None
8989

@@ -93,7 +93,7 @@ def _generate_id(self) -> str:
9393
return str(uuid4())
9494

9595
@lock()
96-
def rebuild_indexes(self, clear: bool = True):
96+
def rebuild_indexes(self, clear: bool = True) -> None:
9797
indexes: dict[str, dict[str, str]] = self.get_value(self.KEY_INDEXES, default=dict())
9898

9999
if self.KEY_USERS not in indexes:
@@ -111,14 +111,14 @@ def rebuild_indexes(self, clear: bool = True):
111111
self.set_value(self.KEY_INDEXES, indexes)
112112

113113
@lock()
114-
def add_index(self, table: str, key: str, id: str):
114+
def add_index(self, table: str, key: str, id: str) -> None:
115115
indexes: dict[str, dict[str, str]] = self.get_value(self.KEY_INDEXES)
116116
indexes[table][key] = id
117117

118118
self.set_value(self.KEY_INDEXES, indexes)
119119

120120
@lock()
121-
def remove_index(self, table: str, key: str):
121+
def remove_index(self, table: str, key: str) -> None:
122122
indexes: dict[str, dict[str, str]] = self.get_value(self.KEY_INDEXES)
123123
indexes[table].pop(key)
124124

@@ -130,7 +130,7 @@ def get_id_from_index(self, table: str, key: str) -> str | None:
130130
return indexes[table].get(key)
131131

132132
@lock()
133-
def _do_init_db_objects(self):
133+
def _do_init_db_objects(self) -> None:
134134
self.rebuild_indexes(clear=False)
135135

136136
if self.KEY_USERS not in self.get_value(""):
@@ -271,7 +271,7 @@ def update_product(
271271
name: str | None = None,
272272
price_minor: int | None = None,
273273
description: str | None = None,
274-
):
274+
) -> None:
275275
product = self.get_product(id, check_exists=True)
276276

277277
if name is not None:
@@ -311,7 +311,7 @@ def create_shopping_cart(self, product_ids: list[str]) -> models.ShoppingCart:
311311
return obj
312312

313313
@lock()
314-
def delete_shopping_cart(self, shopping_cart_id: str):
314+
def delete_shopping_cart(self, shopping_cart_id: str) -> None:
315315
# Проверка наличия
316316
self.get_shopping_cart(shopping_cart_id, check_exists=True)
317317

@@ -325,7 +325,7 @@ def update_shopping_cart(
325325
self,
326326
shopping_cart_id: str,
327327
product_ids: list[str],
328-
):
328+
) -> None:
329329
shopping_cart: models.ShoppingCart = self.get_shopping_cart(
330330
shopping_cart_id, check_exists=True
331331
)
@@ -356,7 +356,7 @@ def add_product_in_shopping_cart(
356356
self,
357357
shopping_cart_id: str,
358358
product_id: str,
359-
):
359+
) -> None:
360360
shopping_cart: models.ShoppingCart = self.get_shopping_cart(
361361
shopping_cart_id, check_exists=True
362362
)
@@ -372,7 +372,7 @@ def remove_product_from_shopping_cart(
372372
self,
373373
shopping_cart_id: str,
374374
product_id: str,
375-
):
375+
) -> None:
376376
shopping_cart: models.ShoppingCart = self.get_shopping_cart(
377377
shopping_cart_id, check_exists=True
378378
)

‎fastapi__examples/market_from_stepic/src/market/main.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async def unicorn_exception_handler(_: Request, exc: DbException):
2828

2929

3030
@app.get("/", response_class=HTMLResponse)
31-
def index():
31+
def index() -> str:
3232
return """\
3333
<!DOCTYPE html>
3434
<html>

‎fastapi__examples/market_from_stepic/src/market/resources.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def create_shopping_cart(
178178

179179

180180
@router.delete("/shopping-cart/{id}", status_code=status.HTTP_204_NO_CONTENT)
181-
def delete_shopping_cart(id: str):
181+
def delete_shopping_cart(id: str) -> None:
182182
services.delete_shopping_cart(id)
183183

184184

‎fastapi__examples/market_from_stepic/src/market/services.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def update_product(
6666
name: str | None = None,
6767
price_minor: int | None = None, # Копейки
6868
description: str | None = None,
69-
):
69+
) -> None:
7070
db.update_product(
7171
id=id,
7272
name=name,
@@ -85,15 +85,15 @@ def create_shopping_cart(product_ids: list[str] = None) -> models.IdBasedObj:
8585
return models.IdBasedObj(id=shopping_cart.id)
8686

8787

88-
def delete_shopping_cart(shopping_cart_id: str):
88+
def delete_shopping_cart(shopping_cart_id: str) -> None:
8989
db.delete_shopping_cart(shopping_cart_id)
9090

9191

9292
def add_product_in_shopping_cart(
9393
shopping_cart_id: str,
9494
product_id: str,
95-
):
96-
return db.add_product_in_shopping_cart(
95+
) -> None:
96+
db.add_product_in_shopping_cart(
9797
shopping_cart_id=shopping_cart_id,
9898
product_id=product_id,
9999
)
@@ -102,8 +102,8 @@ def add_product_in_shopping_cart(
102102
def remove_product_from_shopping_cart(
103103
shopping_cart_id: str,
104104
product_id: str,
105-
):
106-
return db.remove_product_from_shopping_cart(
105+
) -> None:
106+
db.remove_product_from_shopping_cart(
107107
shopping_cart_id=shopping_cart_id,
108108
product_id=product_id,
109109
)
@@ -143,7 +143,7 @@ def update_order(
143143
status: models.StatusOrderEnum | None = None,
144144
cancel_reason: str | None = None,
145145
context_user: models.User | None = None,
146-
):
146+
) -> None:
147147
# Клиент не может сам запускать выполнение заказа или завершать его
148148
if context_user is None and status in (models.StatusOrderEnum.IN_PROCESSED, models.StatusOrderEnum.FINISHED):
149149
raise fastapi.HTTPException(
@@ -177,7 +177,7 @@ def submit_order(
177177
id: str,
178178
status: models.StatusOrderEnum,
179179
context_user: models.User | None = None,
180-
):
180+
) -> None:
181181
update_order(
182182
id=id,
183183
status=status,

0 commit comments

Comments
 (0)