From 78a67771726117aa0705a5ed999f3b0b1b2eb328 Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Mon, 20 Nov 2023 12:13:33 +0800 Subject: [PATCH 1/5] add mypy --- .pre-commit-config.yaml | 18 +++++++++++++++++- CHANGELOG.md | 4 ++++ docs/conf.py | 4 +++- stac_validator/utilities.py | 11 ++++++----- stac_validator/validate.py | 18 +++++++++--------- 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a980964e..272651fe 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,4 +12,20 @@ repos: rev: 22.3.0 hooks: - id: black - language_version: python3.8 \ No newline at end of file + language_version: python3.8 +- repo: https://github.com/pre-commit/mirrors-mypy + rev: v0.991 + hooks: + - id: mypy + exclude: /tests/ + # --strict + args: [ + --no-strict-optional, + --ignore-missing-imports, + --implicit-reexport, + --explicit-package-bases, + ] + additional_dependencies: [ + "types-attrs", + "types-requests" + ] \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ff108ee..c75cf505 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is (loosely) based on [Keep a Changelog](http://keepachangelog.com/) ## [Unreleased] +### Added + +- Added mypy to pre-commit config ([#229](https://github.com/stac-utils/stac-validator/pull/224)) + ## [v3.3.2] - 2023-11-17 ### Added diff --git a/docs/conf.py b/docs/conf.py index 47b13472..e5da64a8 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -4,6 +4,8 @@ # list see the documentation: # https://www.sphinx-doc.org/en/master/usage/configuration.html +from typing import List + # -- Path setup -------------------------------------------------------------- # If extensions (or modules to document with autodoc) are in another directory, @@ -30,7 +32,7 @@ # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. -extensions = [] +extensions: List[str] = [] # Add any paths that contain templates here, relative to this directory. templates_path = ["_templates"] diff --git a/stac_validator/utilities.py b/stac_validator/utilities.py index c12aba06..f07b95b1 100644 --- a/stac_validator/utilities.py +++ b/stac_validator/utilities.py @@ -1,6 +1,7 @@ import functools import json import ssl +from typing import Dict from urllib.parse import urlparse from urllib.request import urlopen @@ -44,7 +45,7 @@ def is_valid_url(url: str) -> bool: return urlparse(url).scheme in ["http", "https"] -def get_stac_type(stac_content: dict) -> str: +def get_stac_type(stac_content: Dict) -> str: """Determine the type of a STAC resource. Given a dictionary representing a STAC resource, this function determines the @@ -74,7 +75,7 @@ def get_stac_type(stac_content: dict) -> str: return str(e) -def fetch_and_parse_file(input_path: str) -> dict: +def fetch_and_parse_file(input_path: str) -> Dict: """Fetches and parses a JSON file from a URL or local file. Given a URL or local file path to a JSON file, this function fetches the file, @@ -107,7 +108,7 @@ def fetch_and_parse_file(input_path: str) -> dict: @functools.lru_cache(maxsize=48) -def fetch_and_parse_schema(input_path: str) -> dict: +def fetch_and_parse_schema(input_path: str) -> Dict: """Fetches and parses a JSON schema file from a URL or local file using a cache. Given a URL or local file path to a JSON schema file, this function fetches the file @@ -147,8 +148,8 @@ def set_schema_addr(version: str, stac_type: str) -> str: def link_request( - link: dict, - initial_message: dict, + link: Dict, + initial_message: Dict, ) -> None: """Makes a request to a URL and appends it to the relevant field of the initial message. diff --git a/stac_validator/validate.py b/stac_validator/validate.py index 15896fa9..de62c957 100644 --- a/stac_validator/validate.py +++ b/stac_validator/validate.py @@ -1,7 +1,7 @@ import json import os from json.decoder import JSONDecodeError -from typing import Optional +from typing import Dict, Optional from urllib.error import HTTPError, URLError import click # type: ignore @@ -68,7 +68,7 @@ def __init__( self.max_depth = max_depth self.extensions = extensions self.core = core - self.stac_content: dict = {} + self.stac_content: Dict = {} self.version = "" self.depth: int = 0 self.skip_val = False @@ -76,7 +76,7 @@ def __init__( self.valid = False self.log = log - def create_err_msg(self, err_type: str, err_msg: str) -> dict: + def create_err_msg(self, err_type: str, err_msg: str) -> Dict: self.valid = False return { "version": self.version, @@ -99,7 +99,7 @@ def create_links_message(self): "request_invalid": request_invalid, } - def create_message(self, stac_type: str, val_type: str) -> dict: + def create_message(self, stac_type: str, val_type: str) -> Dict: return { "version": self.version, "path": self.stac_file, @@ -109,7 +109,7 @@ def create_message(self, stac_type: str, val_type: str) -> dict: "validation_method": val_type, } - def assets_validator(self) -> dict: + def assets_validator(self) -> Dict: """Validate assets. Returns: @@ -122,7 +122,7 @@ def assets_validator(self) -> dict: link_request(asset, initial_message) return initial_message - def links_validator(self) -> dict: + def links_validator(self) -> Dict: """Validate links. Returns: @@ -143,7 +143,7 @@ def links_validator(self) -> dict: return initial_message - def extensions_validator(self, stac_type: str) -> dict: + def extensions_validator(self, stac_type: str) -> Dict: """Validate the STAC extensions according to their corresponding JSON schemas. Args: @@ -253,7 +253,7 @@ def core_validator(self, stac_type: str) -> None: self.schema = set_schema_addr(self.version, stac_type) self.custom_validator() - def default_validator(self, stac_type: str) -> dict: + def default_validator(self, stac_type: str) -> Dict: """Validate the STAC catalog or item against the core schema and its extensions. Args: @@ -379,7 +379,7 @@ def validate_dict(self, stac_content) -> bool: self.stac_content = stac_content return self.run() - def validate_item_collection_dict(self, item_collection: dict) -> None: + def validate_item_collection_dict(self, item_collection: Dict) -> None: """Validate the contents of an item collection. Args: From bcb55eda22f1c2c9198320be8dce0eca992c125d Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Mon, 20 Nov 2023 12:25:28 +0800 Subject: [PATCH 2/5] update pre-commit --- .pre-commit-config.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 272651fe..fec58f10 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,20 +1,20 @@ repos: - repo: https://github.com/PyCQA/flake8 - rev: 3.9.1 + rev: 6.1.0 hooks: - id: flake8 - repo: https://github.com/timothycrosley/isort - rev: 5.11.5 + rev: 5.12.0 hooks: - id: isort args: ["--profile", "black"] - repo: https://github.com/psf/black - rev: 22.3.0 + rev: 23.11.0 hooks: - id: black language_version: python3.8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.991 + rev: v1.7.0 hooks: - id: mypy exclude: /tests/ From 9dcb0b6415887bf6ed5b0e062f67bb8135fe5644 Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Mon, 20 Nov 2023 13:12:59 +0800 Subject: [PATCH 3/5] update tox --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 0d057fcc..db9f8c70 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py37,py38,py39 +envlist = py38,py39,py310,py311 [testenv] deps = pytest From 8822dc6c7707fe13959be5f28ed2709dbc9d4304 Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Mon, 20 Nov 2023 13:27:22 +0800 Subject: [PATCH 4/5] use typing list --- stac_validator/validate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stac_validator/validate.py b/stac_validator/validate.py index de62c957..e89a6024 100644 --- a/stac_validator/validate.py +++ b/stac_validator/validate.py @@ -1,7 +1,7 @@ import json import os from json.decoder import JSONDecodeError -from typing import Dict, Optional +from typing import Dict, List, Optional from urllib.error import HTTPError, URLError import click # type: ignore @@ -60,7 +60,7 @@ def __init__( self.stac_file = stac_file self.item_collection = item_collection self.pages = pages - self.message: list = [] + self.message: List = [] self.schema = custom self.links = links self.assets = assets From 4a6f0c78e4bfed39a243463ae302f4c5425450f8 Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Mon, 20 Nov 2023 13:28:49 +0800 Subject: [PATCH 5/5] update --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 8b5af76f..b98ca394 100644 --- a/setup.py +++ b/setup.py @@ -18,7 +18,7 @@ "Intended Audience :: Information Technology", "Intended Audience :: Science/Research", "License :: OSI Approved :: Apache Software License", - "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", "Topic :: Scientific/Engineering :: GIS", ], keywords="STAC validation raster",