Skip to content

Commit 5bd11df

Browse files
authored
Fixed types for old pyhton versions and SQLAlchemy memory leak (#150)
1 parent 868ed36 commit 5bd11df

6 files changed

Lines changed: 20 additions & 25 deletions

File tree

fastapi_template/cli.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
SKIP_ENTRY,
99
)
1010
from importlib.metadata import version
11-
from typing import Callable, Optional
11+
from typing import Callable, List, Optional
1212
from click import Command, Option
1313
import re
1414

@@ -40,20 +40,20 @@ def disable_orm(ctx: BuilderContext) -> MenuEntry:
4040
return None
4141

4242

43-
def do_not_ask_features_if_quite(ctx: BuilderContext) -> Optional[list[MenuEntry]]:
43+
def do_not_ask_features_if_quite(ctx: BuilderContext) -> Optional[List[MenuEntry]]:
4444
if ctx.quite:
4545
return [SKIP_ENTRY]
4646
return None
4747

4848

49-
def check_db(allowed_values: list[str]) -> Callable[[BuilderContext], bool]:
49+
def check_db(allowed_values: List[str]) -> Callable[[BuilderContext], bool]:
5050
def checker(ctx: BuilderContext) -> bool:
5151
return ctx.db not in allowed_values
5252

5353
return checker
5454

5555

56-
def check_orm(allowed_values: list[str]) -> Callable[[BuilderContext], bool]:
56+
def check_orm(allowed_values: List[str]) -> Callable[[BuilderContext], bool]:
5757
def checker(ctx: BuilderContext) -> bool:
5858
return ctx.orm not in allowed_values
5959

@@ -499,7 +499,7 @@ def checker(ctx: BuilderContext) -> bool:
499499

500500

501501
def handle_cli(
502-
menus: list[BaseMenuModel],
502+
menus: List[BaseMenuModel],
503503
callback: Callable[[BuilderContext], None],
504504
):
505505
def inner_callback(**cli_args: Any):
@@ -532,7 +532,7 @@ def inner_callback(**cli_args: Any):
532532

533533

534534
def run_command(callback: Callable[[BuilderContext], None]) -> None:
535-
menus: "list[BaseMenuModel]" = [
535+
menus: "List[BaseMenuModel]" = [
536536
api_menu,
537537
db_menu,
538538
orm_menu,

fastapi_template/input_model.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import enum
2-
from typing import Optional, Callable, Any
2+
from typing import List, Optional, Callable, Any
33

44
from pydantic import BaseModel
55
import click
@@ -54,7 +54,7 @@ def generated_name(self) -> str:
5454

5555
class BaseMenuModel(BaseModel, abc.ABC):
5656
title: str
57-
entries: list[MenuEntry]
57+
entries: List[MenuEntry]
5858
description: str = ""
5959

6060
def _preview(self, current_value: str):
@@ -65,7 +65,7 @@ def _preview(self, current_value: str):
6565
return "Unknown value"
6666

6767
@abc.abstractmethod
68-
def get_cli_options(self) -> list[click.Option]:
68+
def get_cli_options(self) -> List[click.Option]:
6969
pass
7070

7171
@abc.abstractmethod
@@ -91,7 +91,7 @@ class SingularMenuModel(BaseMenuModel):
9191
]
9292
parser: Optional[Callable[[str], Any]]
9393

94-
def get_cli_options(self) -> list[click.Option]:
94+
def get_cli_options(self) -> List[click.Option]:
9595
cli_name = self.code
9696
if self.cli_name is not None:
9797
cli_name = self.cli_name
@@ -168,9 +168,9 @@ def after_ask(self, context: "BuilderContext") -> "BuilderContext":
168168

169169

170170
class MultiselectMenuModel(BaseMenuModel):
171-
before_ask: Optional[Callable[["BuilderContext"], Optional[list[MenuEntry]]]]
171+
before_ask: Optional[Callable[["BuilderContext"], Optional[List[MenuEntry]]]]
172172

173-
def get_cli_options(self) -> list[click.Option]:
173+
def get_cli_options(self) -> List[click.Option]:
174174
options = []
175175
for entry in self.entries:
176176
options.append(

fastapi_template/template/{{cookiecutter.project_name}}/conditional_files.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
"Self-hosted swagger": {
148148
"enabled": "{{cookiecutter.self_hosted_swagger}}",
149149
"resources": [
150-
"{{cookiecutter.project_name}}/web/static",
150+
"{{cookiecutter.project_name}}/static/docs",
151151
"{{cookiecutter.project_name}}/web/api/docs"
152152
]
153153
},
@@ -210,4 +210,4 @@
210210
"{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_sqlite.sql"
211211
]
212212
}
213-
}
213+
}

fastapi_template/template/{{cookiecutter.project_name}}/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ aiokafka = "^0.8.0"
123123
[tool.poetry.dev-dependencies]
124124
pytest = "^7.2.1"
125125
flake8 = "~4.0.1"
126-
mypy = "^0.991"
126+
mypy = "^1.1.1"
127127
isort = "^5.11.4"
128128
pre-commit = "^3.0.1"
129129
wemake-python-styleguide = "^0.17.0"

fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import enum
22
from pathlib import Path
33
from tempfile import gettempdir
4-
from typing import Optional
4+
from typing import List, Optional
55

66
from pydantic import BaseSettings
77
from yarl import URL
@@ -111,7 +111,7 @@ class Settings(BaseSettings):
111111

112112
{%- if cookiecutter.enable_kafka == "True" %}
113113

114-
kafka_bootstrap_servers: list[str] = ["{{cookiecutter.project_name}}-kafka:9092"]
114+
kafka_bootstrap_servers: List[str] = ["{{cookiecutter.project_name}}-kafka:9092"]
115115

116116
{%- endif %}
117117

fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/lifetime.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ async def _setup_db(app: FastAPI) -> None:
7777
{%- endif %}
7878

7979
{%- if cookiecutter.orm == "sqlalchemy" %}
80-
from asyncio import current_task
8180
from sqlalchemy.ext.asyncio import (
82-
async_scoped_session,
8381
create_async_engine,
8482
async_sessionmaker,
8583
)
@@ -102,12 +100,9 @@ def _setup_db(app: FastAPI) -> None: # pragma: no cover
102100
:param app: fastAPI application.
103101
"""
104102
engine = create_async_engine(str(settings.db_url), echo=settings.db_echo)
105-
session_factory = async_scoped_session(
106-
async_sessionmaker(
107-
engine,
108-
expire_on_commit=False,
109-
),
110-
scopefunc=current_task,
103+
session_factory = async_sessionmaker(
104+
engine,
105+
expire_on_commit=False,
111106
)
112107
app.state.db_engine = engine
113108
app.state.db_session_factory = session_factory

0 commit comments

Comments
 (0)