Skip to content

Commit 101aa6f

Browse files
authored
Migrate to pyproject.toml and update Python versions to 3.9 and 3.10 (#8)
* Migrate to pyproject.toml and update Python versions - Dropped support py3.6, py3.7, and py3.8 as supporting them in 2025 is very challenging - Added support for py3.10 - Replaced setup.py, setup.cfg, and tox.ini with pyproject.toml for modern packaging and configuration. - Updated CI workflow to test on Python 3.9 and 3.10 - added .python-version - Added test command to Makefile, which uses uv instead of tox for testing multiple python versions - Adjusted Makefiel to use ruff instead of black, isort, and flake8 - Refactored code for improved type hinting and formatting consistency. * Update workflow versions and migrate to astral gh action
1 parent 8ab05b1 commit 101aa6f

File tree

13 files changed

+1232
-129
lines changed

13 files changed

+1232
-129
lines changed

.github/workflows/python-package.yml

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,37 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
python-version: [3.9]
17+
python-version: ['3.10']
1818

1919
steps:
2020
- name: Checkout the repository
21-
uses: actions/checkout@v2
21+
uses: actions/checkout@v5
2222

2323
- name: Setup Python
24-
uses: actions/setup-python@v1
25-
with:
26-
python-version: ${{ matrix.python-version }}
24+
uses: astral-sh/ruff-action@v3
2725

2826
- name: Install package
2927
run: |
3028
pip install mypy
31-
pip install black
32-
pip install isort
3329
- name: Run pre-checks
3430
run: |
3531
mypy fastapi_asyncpg/
36-
isort -c -rc fastapi_asyncpg/
37-
black -l 80 --check --verbose fastapi_asyncpg/
32+
ruff check .
33+
ruff format --check .
34+
3835
# Job to run tests
3936
tests:
4037
runs-on: ubuntu-latest
4138
strategy:
4239
matrix:
43-
python-version: [3.7, 3.8, 3.9]
40+
python-version: ['3.9','3.10']
4441
# Set environment variables
4542
steps:
4643
- name: Checkout the repository
47-
uses: actions/checkout@v2
44+
uses: actions/checkout@v5
4845

4946
- name: Setup Python
50-
uses: actions/setup-python@v1
47+
uses: actions/setup-python@v6
5148
with:
5249
python-version: ${{ matrix.python-version }}
5350

@@ -60,6 +57,7 @@ jobs:
6057
pytest -vs tests/
6158
6259
- name: Upload coverage to Codecov
63-
uses: codecov/codecov-action@v1
60+
uses: codecov/codecov-action@v5
6461
with:
65-
file: ./coverage.xml
62+
files: ./coverage.xml
63+

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ pyvenv.cfg
1919
*.profraw
2020
*.py?
2121
*.swp
22+
.venv

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.10

Makefile

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
.PHONY: isort black flake8 mypy
22

3-
lint: isort black flake8 mypy
4-
5-
isort:
6-
isort fastapi_asyncpg
7-
8-
black:
9-
black fastapi_asyncpg/ -l 80
10-
11-
flake8:
12-
flake8 fastapi_asyncpg
3+
lint:
4+
uv run --python 3.10 --extra dev ruff check . --fix
5+
uv run --python 3.10 --extra dev ruff format .
136

147
mypy:
15-
mypy fastapi_asyncpg
8+
uv run --python 3.10 --extra dev mypy fastapi_asyncpg
9+
10+
test:
11+
uv run --python 3.9 --extra test pytest tests
12+
uv run --python 3.10 --extra test pytest tests

fastapi_asyncpg/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(
1616
app: FastAPI,
1717
dsn: str,
1818
*,
19-
init_db: typing.Callable = None, # callable for running sql on init
19+
init_db: typing.Optional[typing.Callable] = None, # callable for running sql on init
2020
pool=None, # usable on testing
2121
**options,
2222
):
@@ -117,7 +117,7 @@ class SingleConnectionTestingPool:
117117
def __init__(
118118
self,
119119
conn: asyncpg.Connection,
120-
initialize: typing.Callable = None,
120+
initialize: typing.Optional[typing.Callable] = None,
121121
add_logger_postgres: bool = False,
122122
):
123123
self._conn = conn
@@ -154,16 +154,14 @@ def __getattr__(self, key):
154154
async def create_pool_test(
155155
dsn: str,
156156
*,
157-
initialize: typing.Callable = None,
157+
initialize: typing.Optional[typing.Callable] = None,
158158
add_logger_postgres: bool = False,
159159
):
160160
"""This part is only used for testing,
161161
we create a fake "pool" that just starts a connecion,
162162
that does a transaction inside it"""
163163
conn = await asyncpg.connect(dsn=dsn)
164-
pool = SingleConnectionTestingPool(
165-
conn, initialize=initialize, add_logger_postgres=add_logger_postgres
166-
)
164+
pool = SingleConnectionTestingPool(conn, initialize=initialize, add_logger_postgres=add_logger_postgres)
167165
return pool
168166

169167

fastapi_asyncpg/sql.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ async def select(conn, table, condition="1 = 1", args=None, fields="*"):
1717

1818
async def count(conn, table, where="1=1", args=None):
1919
args = args or []
20-
return await conn.fetchval(
21-
f"select count(*) from {table} WHERE {where}", *args
22-
)
20+
return await conn.fetchval(f"select count(*) from {table} WHERE {where}", *args)
2321

2422

2523
async def insert(conn, table, values):
@@ -45,9 +43,7 @@ async def update(conn, table, conditions: dict, values: dict):
4543
vals.append(f"{column}=${counter}")
4644
params.append(value)
4745
counter += 1
48-
sql = qs.format(
49-
table=table, columns=" ,".join(vals), cond=" AND ".join(cond)
50-
)
46+
sql = qs.format(table=table, columns=" ,".join(vals), cond=" AND ".join(cond))
5147
return await conn.fetchrow(sql, *params)
5248

5349

pyproject.toml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[build-system]
2+
requires = ["setuptools>=45", "setuptools-scm[toml]>=6.2"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "fastapi_asyncpg"
7+
version = "1.0.1"
8+
description = "FastAPI integration for asyncpg"
9+
readme = "README.md"
10+
requires-python = ">=3.9,<3.11"
11+
license = {text = "MIT"}
12+
authors = [
13+
{name = "Jordi collell", email = "jordic@gmail.com"}
14+
]
15+
classifiers = [
16+
"License :: OSI Approved :: MIT License",
17+
"Programming Language :: Python",
18+
"Programming Language :: Python :: 3",
19+
"Programming Language :: Python :: 3.9",
20+
]
21+
dependencies = [
22+
"fastapi",
23+
"asyncpg",
24+
]
25+
26+
[project.optional-dependencies]
27+
dev = [
28+
"ruff",
29+
"mypy"
30+
]
31+
docs = [
32+
"sphinx",
33+
"recommonmark",
34+
]
35+
test = [
36+
"pytest-docker-fixtures[pg]",
37+
"pytest",
38+
"async_asgi_testclient",
39+
"pytest-asyncio",
40+
]
41+
publish = [
42+
"twine",
43+
]
44+
45+
[project.urls]
46+
Homepage = "https://github.com/jordic/fastapi_asyncpg"
47+
48+
[tool.setuptools]
49+
packages = {find = {exclude = ["tests"]}}
50+
51+
[tool.setuptools.package-data]
52+
fastapi = ["py.typed"]
53+
54+
[tool.ruff]
55+
line-length = 120
56+
exclude = [".git", "__pycache__", "docs/source/conf.py", "old", "build", "dist"]
57+
58+
[tool.ruff.lint]
59+
ignore = ["E302", "W391", "E701", "F901", "E252", "E203"]
60+
61+
[tool.ruff.format]
62+
line-ending = "auto"
63+
64+
[tool.mypy]
65+
namespace_packages = true
66+
67+
[tool.pytest.ini_options]
68+
asyncio_mode = "auto"
69+
asyncio_default_fixture_loop_scope = "function"

setup.cfg

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

setup.py

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

tests/test_con_rollback.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616

1717
dir = Path(__file__).parent
1818

19-
images.configure(
20-
"postgresql", "postgres", "11.1", env={"POSTGRES_DB": "test_db"}
21-
)
19+
images.configure("postgresql", "postgres", "11.1", env={"POSTGRES_DB": "test_db"})
2220

2321

2422
@pytest.fixture
@@ -48,7 +46,7 @@ async def test_the_db_is_empty_again(pool):
4846

4947

5048
async def test_sql(pool):
51-
""" sql.py contains poor man sql helpers to work with sql and asyncpg """
49+
"""sql.py contains poor man sql helpers to work with sql and asyncpg"""
5250
async with pool.acquire() as db:
5351
res = await sql.insert(db, "test", {"item": "test", "val": "value"})
5452
result = await sql.get(db, "test", "id=$1", args=[res["id"]])
@@ -104,13 +102,11 @@ class Schema(pd.BaseModel):
104102

105103
item: str
106104
val: str
107-
id: typing.Optional[int]
105+
id: typing.Optional[int] = None
108106

109107
async def save(self, db):
110108
if self.id is None:
111-
result = await sql.insert(
112-
db, self.__tablename__, self.dict(exclude_unset=True)
113-
)
109+
result = await sql.insert(db, self.__tablename__, self.dict(exclude_unset=True))
114110
self.id = result["id"]
115111
else:
116112
result = await sql.update_by(

0 commit comments

Comments
 (0)