Skip to content

Commit

Permalink
Use mypy via pre-commit instead of pytest-mypy (#319)
Browse files Browse the repository at this point in the history
* use mypy via pre-commit instead of pytest-mypy

* fix mypy?

* fix coverage

* test with python3.11

* 💄

* don't cover destructor
  • Loading branch information
dionhaefner authored Sep 26, 2023
1 parent 329d885 commit 0cf1ea4
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 30 deletions.
13 changes: 2 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:

matrix:
os: [ubuntu-latest]
python-version: ["3.8", "3.10"]
python-version: ["3.8", "3.11"]

defaults:
run:
Expand Down Expand Up @@ -83,10 +83,6 @@ jobs:
pip install -e .[test]
pip freeze
- name: Initialize mypy
run: |
mypy --install-types --non-interactive . || true
- name: Run tests
run: |
MYSQL_SRV="${{ env.DB_USER }}:${{ env.DB_PASSWORD }}@127.0.0.1:${{ env.MYSQL_PORT }}"
Expand All @@ -111,7 +107,7 @@ jobs:

matrix:
os: [macos-latest, windows-latest]
python-version: ["3.8", "3.10"]
python-version: ["3.8", "3.11"]

defaults:
run:
Expand Down Expand Up @@ -159,11 +155,6 @@ jobs:
pip install -e .[test]
conda list
- name: Initialize mypy
run: |
mypy . > /dev/null || true
mypy --install-types --non-interactive
- name: Run tests
run: |
python -m pytest . --color=yes --cov=terracotta
Expand Down
13 changes: 13 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ repos:
hooks:
- id: flake8
args: ['--config=setup.cfg']
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.1
hooks:
- id: mypy
additional_dependencies:
- types-cachetools==5.3.0.6
- types-click-spinner==0.1.13.5
- types-docutils==0.20.0.3
- types-setuptools==68.2.0.0
- types-toml==0.10.8.7

default_language_version:
python: python3.11

exclude: "
^$\
Expand Down
2 changes: 1 addition & 1 deletion readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2
build:
os: ubuntu-20.04
tools:
python: "3.10"
python: "3.11"

sphinx:
configuration: docs/conf.py
Expand Down
3 changes: 0 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ extend-ignore = E203,E501,E402

[tool:pytest]
addopts =
--mypy
--benchmark-group-by=func
--benchmark-columns=min,max,median
-v
markers =
mypy: MyPy marker
filterwarnings =
error
# ignored by default
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Framework :: Flask",
"Operating System :: Microsoft :: Windows :: Windows 10",
"Operating System :: MacOS :: MacOS X",
Expand Down Expand Up @@ -80,7 +81,6 @@
"test": [
"pytest",
"pytest-cov",
"pytest-mypy",
"pytest-benchmark",
"attrs>=17.4.0",
"codecov",
Expand Down
5 changes: 4 additions & 1 deletion terracotta/drivers/sqlite_remote_meta_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,7 @@ def _connection_callback(self) -> None:

def __del__(self) -> None:
"""Clean up temporary database upon exit"""
self.__rm(self._local_path)
try:
self.__rm(self._local_path)
except OSError: # pragma: no cover
pass
19 changes: 10 additions & 9 deletions terracotta/scripts/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@


def parse_version(verstr: str) -> Tuple[int, ...]:
"""Convert 'v<major>.<minor>.<patch>' to (major, minor, patch)"""
"""Convert 'v<major>.<minor>.<patch>' to (major, minor)"""
components = verstr.split(".")
components[0] = components[0].lstrip("v")
return tuple(int(c) for c in components[:3])
return tuple(int(c) for c in components[:2])


def join_version(vertuple: Tuple[int, ...]) -> str:
Expand All @@ -30,26 +30,27 @@ def join_version(vertuple: Tuple[int, ...]) -> str:
@click.option("-y", "--yes", is_flag=True, help="Do not ask for confirmation.")
@click.command("migrate")
def migrate(database: str, to_version: str, from_version: str, yes: bool) -> None:
from_version_tuple, to_version_tuple, tc_version_tuple = (
parse_version(v)[:2] if v is not None else None
for v in (from_version, to_version, __version__)
)

"""Migrate databases between Terracotta versions."""
driver = get_driver(database)
meta_store = driver.meta_store
assert isinstance(meta_store, RelationalMetaStore)

to_version_tuple = parse_version(to_version)
tc_version_tuple = parse_version(__version__)

if to_version_tuple > tc_version_tuple:
raise ValueError(
f"Unknown target version {join_version(to_version_tuple)} (this is {join_version(tc_version_tuple)}). Try upgrading terracotta."
)

if from_version_tuple is None:
if from_version is None:
try: # type: ignore
with meta_store.connect(verify=False):
from_version_tuple = parse_version(driver.db_version)[:2]
from_version_tuple = parse_version(driver.db_version)
except Exception as e:
raise RuntimeError("Cannot determine database version.") from e
else:
from_version_tuple = parse_version(from_version)

if from_version_tuple == to_version_tuple:
click.echo("Already at target version, nothing to do.")
Expand Down
5 changes: 1 addition & 4 deletions terracotta/server/flask_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,7 @@ def create_app(debug: bool = False, profile: bool = False) -> Flask:
if profile:
from werkzeug.contrib.profiler import ProfilerMiddleware

# use setattr to work around mypy false-positive (python/mypy#2427)
setattr(
new_app, "wsgi_app", ProfilerMiddleware(new_app.wsgi_app, restrictions=[30])
)
new_app.wsgi_app = ProfilerMiddleware(new_app.wsgi_app, restrictions=[30])

_setup_error_handlers(new_app)

Expand Down

0 comments on commit 0cf1ea4

Please sign in to comment.