Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run mypy during tests #1104

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
- {name: '3.8', python: '3.8', tox: py38}
- {name: '3.7', python: '3.7', tox: py37}
- {name: 'PyPy 3.9', python: 'pypy3.9', tox: pypy39}
- {name: 'Typing', python: '3.10', os: ubuntu-latest, tox: typing}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
Expand Down
1 change: 1 addition & 0 deletions requirements/dev.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-r docs.in
-r tests.in
-r typing.in
pip-compile-multi
pre-commit
tox
3 changes: 2 additions & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SHA1:54196885a2acdc154945dacc9470e2a9900fd8c1
# SHA1:54b5b77ec8c7a0064ffa93b2fd16cb0130ba177c
#
# This file is autogenerated by pip-compile-multi
# To update, run:
Expand All @@ -7,6 +7,7 @@
#
-r docs.txt
-r tests.txt
-r typing.txt
build==0.8.0
# via pip-tools
cfgv==3.3.1
Expand Down
3 changes: 3 additions & 0 deletions requirements/typing.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mypy
pytest
sqlalchemy[mypy]
41 changes: 41 additions & 0 deletions requirements/typing.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# SHA1:8a98a56578d312cffa4514127f5a91091f2e38d8
#
# This file is autogenerated by pip-compile-multi
# To update, run:
#
# pip-compile-multi
#
attrs==22.1.0
# via pytest
greenlet==1.1.3
# via sqlalchemy
iniconfig==1.1.1
# via pytest
mypy==0.971
# via
# -r requirements/typing.in
# sqlalchemy
mypy-extensions==0.4.3
# via mypy
packaging==21.3
# via pytest
pluggy==1.0.0
# via pytest
py==1.11.0
# via pytest
pyparsing==3.0.9
# via packaging
pytest==7.1.3
# via -r requirements/typing.in
sqlalchemy[mypy]==1.4.41
# via -r requirements/typing.in
sqlalchemy2-stubs==0.0.2a27
# via sqlalchemy
tomli==2.0.1
# via
# mypy
# pytest
typing-extensions==4.3.0
# via
# mypy
# sqlalchemy2-stubs
14 changes: 14 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,17 @@ ignore =
W503
# up to 88 allowed by bugbear B950
max-line-length = 80

[mypy]
python_version = 3.7
files = src/flask_sqlalchemy, tests
show_error_codes = true
pretty = true
strict = true
# db.Model attribute doesn't recognize subclassing
disable_error_code = name-defined
# db.Model is Any
disallow_subclassing_any = false

[mypy-importlib_metadata.*]
ignore_missing_imports = True
4 changes: 2 additions & 2 deletions src/flask_sqlalchemy/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ def _apply_driver_defaults(self, options: dict[str, t.Any], app: Flask) -> None:
url = sa.engine.make_url(options["url"])

if url.drivername in {"sqlite", "sqlite+pysqlite"}:
if url.database in {None, "", ":memory:"}:
if url.database is None or url.database in {"", ":memory:"}:
options["poolclass"] = sa.pool.StaticPool

if "connect_args" not in options:
Expand All @@ -558,7 +558,7 @@ def _apply_driver_defaults(self, options: dict[str, t.Any], app: Flask) -> None:
else:
db_str = url.database

if not os.path.isabs(db_str): # type: ignore[arg-type]
if not os.path.isabs(db_str):
os.makedirs(app.instance_path, exist_ok=True)
db_str = os.path.join(app.instance_path, db_str)

Expand Down
9 changes: 6 additions & 3 deletions src/flask_sqlalchemy/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ def _query_count(self) -> int:
select = self._query_args["select"]
sub = select.options(sa.orm.lazyload("*")).order_by(None).subquery()
session = self._query_args["session"]
return session.execute(sa.select(sa.func.count()).select_from(sub)).scalar()
out = session.execute(sa.select(sa.func.count()).select_from(sub)).scalar()
return out # type: ignore[no-any-return]


class QueryPagination(Pagination):
Expand All @@ -350,8 +351,10 @@ class QueryPagination(Pagination):

def _query_items(self) -> list[t.Any]:
query = self._query_args["query"]
return query.limit(self.per_page).offset(self._query_offset).all()
out = query.limit(self.per_page).offset(self._query_offset).all()
return out # type: ignore[no-any-return]

def _query_count(self) -> int:
# Query.count automatically disables eager loads
return self._query_args["query"].order_by(None).count()
out = self._query_args["query"].order_by(None).count()
return out # type: ignore[no-any-return]
5 changes: 3 additions & 2 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ def test_sqlite_driver_level_uri(app: Flask) -> None:
db = SQLAlchemy(app)
db.create_all()
db_path = db.engine.url.database
assert db_path.startswith(f"file:{app.instance_path}") # type: ignore[union-attr]
assert os.path.exists(db_path[5:]) # type: ignore[arg-type]
assert db_path is not None
assert db_path.startswith(f"file:{app.instance_path}")
assert os.path.exists(db_path[5:])


@unittest.mock.patch.object(SQLAlchemy, "_make_engine", autospec=True)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_track_modifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ def after_commit(sender: Flask, changes: list[tuple[t.Any, str]]) -> None:
assert before == after

db.session.remove()
item = db.session.get(Example, 1)
item = db.session.get(Example, 1) # type: ignore[assignment]
item.data = "test" # type: ignore[assignment]
db.session.commit()
assert len(before) == 1
assert before[0] == (item, "update")
assert before == after

db.session.remove()
item = db.session.get(Example, 1)
item = db.session.get(Example, 1) # type: ignore[assignment]
db.session.delete(item)
db.session.commit()
assert len(before) == 1
Expand Down
4 changes: 4 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ deps = pre-commit
skip_install = true
commands = pre-commit run --all-files --show-diff-on-failure

[testenv:typing]
deps = -r requirements/typing.txt
commands = mypy

[testenv:docs]
deps = -r requirements/docs.txt
commands = sphinx-build -W -b html -d {envtmpdir}/doctrees docs {envtmpdir}/html