From 0cf1ea4a6994c8e04f3200f4413bbe8ecf206889 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dion=20H=C3=A4fner?= Date: Tue, 26 Sep 2023 12:58:10 +0200 Subject: [PATCH] Use mypy via pre-commit instead of pytest-mypy (#319) * use mypy via pre-commit instead of pytest-mypy * fix mypy? * fix coverage * test with python3.11 * :lipstick: * don't cover destructor --- .github/workflows/test.yml | 13 ++----------- .pre-commit-config.yaml | 13 +++++++++++++ readthedocs.yml | 2 +- setup.cfg | 3 --- setup.py | 2 +- .../drivers/sqlite_remote_meta_store.py | 5 ++++- terracotta/scripts/migrate.py | 19 ++++++++++--------- terracotta/server/flask_api.py | 5 +---- 8 files changed, 32 insertions(+), 30 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index de1e484d..45deacd4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -24,7 +24,7 @@ jobs: matrix: os: [ubuntu-latest] - python-version: ["3.8", "3.10"] + python-version: ["3.8", "3.11"] defaults: run: @@ -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 }}" @@ -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: @@ -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 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 78f5290d..c19e78ed 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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: " ^$\ diff --git a/readthedocs.yml b/readthedocs.yml index c3bc6f70..2a507c5b 100644 --- a/readthedocs.yml +++ b/readthedocs.yml @@ -3,7 +3,7 @@ version: 2 build: os: ubuntu-20.04 tools: - python: "3.10" + python: "3.11" sphinx: configuration: docs/conf.py diff --git a/setup.cfg b/setup.cfg index 9249be36..73526ff8 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 diff --git a/setup.py b/setup.py index ff8b3dc9..79e1c112 100644 --- a/setup.py +++ b/setup.py @@ -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", @@ -80,7 +81,6 @@ "test": [ "pytest", "pytest-cov", - "pytest-mypy", "pytest-benchmark", "attrs>=17.4.0", "codecov", diff --git a/terracotta/drivers/sqlite_remote_meta_store.py b/terracotta/drivers/sqlite_remote_meta_store.py index 30bb8f13..de9ab441 100644 --- a/terracotta/drivers/sqlite_remote_meta_store.py +++ b/terracotta/drivers/sqlite_remote_meta_store.py @@ -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 diff --git a/terracotta/scripts/migrate.py b/terracotta/scripts/migrate.py index 16afa2f6..3bbfb10a 100644 --- a/terracotta/scripts/migrate.py +++ b/terracotta/scripts/migrate.py @@ -14,10 +14,10 @@ def parse_version(verstr: str) -> Tuple[int, ...]: - """Convert 'v..' to (major, minor, patch)""" + """Convert 'v..' 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: @@ -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.") diff --git a/terracotta/server/flask_api.py b/terracotta/server/flask_api.py index 5661bbc5..0c3ee4ba 100644 --- a/terracotta/server/flask_api.py +++ b/terracotta/server/flask_api.py @@ -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)