diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9973e7249..326339266 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,7 @@ + repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.3.0 # Use the ref you want to point at + rev: v5.0.0 # Use the ref you want to point at hooks: - id: check-merge-conflict - id: check-yaml @@ -13,13 +14,13 @@ repos: - id: yapf args: [--in-place, --parallel, --recursive, --style, .yapf-config] files: "^(trestle|tests|scripts)" - stages: [commit] + stages: [pre-commit] additional_dependencies: [toml] - repo: https://github.com/PyCQA/flake8 - rev: 5.0.4 + rev: 7.1.1 hooks: - id: flake8 - args: [--extend-ignore, "P1,C812,C813,C814,C815,C816,W503,W605,B017,B028", "--illegal-import-packages=filecmp"] + args: [--extend-ignore, "P1,C812,C813,C814,C815,C816,W503,W605,B017,B028"] additional_dependencies: [ flake8-2020, @@ -37,14 +38,13 @@ repos: flake8-quotes, flake8-string-format, flake8-use-fstring, - flake8-illegal-import, pep8-naming, ] files: "^(tests|scripts)" exclude: "(oscal/|third_party)" - stages: [commit] + stages: [pre-commit] - id: flake8 - args: [--extend-ignore, "P1,C812,C813,C814,C815,C816,W503,W605,B017,B028", "--illegal-import-packages=filecmp"] + args: [--extend-ignore, "P1,C812,C813,C814,C815,C816,W503,W605,B017,B028"] additional_dependencies: [ flake8-2020, @@ -62,16 +62,15 @@ repos: flake8-quotes, flake8-string-format, flake8-use-fstring, - flake8-illegal-import, pep8-naming, flake8-bandit, dlint ] files: "^(trestle)" exclude: "(oscal/)" - stages: [commit] + stages: [pre-commit] - repo: https://github.com/executablebooks/mdformat - rev: 0.7.16 + rev: 0.7.17 hooks: - id: mdformat exclude: "CHANGELOG.md|docs/mkdocs_code_of_conduct.md|docs/maintainers.md|docs/api_reference|tests/data/author|docs/contributing/mkdocs_contributing.md|tests/data/jinja_markdown_include|tests/data/jinja_cmd/number_captions_data.md|tests/data/jinja_cmd/number_captions_expected_output.md" @@ -80,3 +79,4 @@ repos: - mdformat-config - mdformat-frontmatter - mdformat-gfm + diff --git a/scripts/flatten_schema.py b/scripts/flatten_schema.py index 0e1df3a29..cf155cf9a 100644 --- a/scripts/flatten_schema.py +++ b/scripts/flatten_schema.py @@ -56,7 +56,7 @@ def _replace_ref(self, d, ref_set): def _replace_refs(self, obj, ref_set): """Given an object recurse into it replacing any found ref: defs with what is in def_list.""" - if type(obj) == dict: + if isinstance(obj, dict): # first check if it is a simple $ref line and replace it directly if len(obj.items()) == 1 and obj.get(self._ref_str, None) is not None: return self._replace_ref(obj, ref_set) @@ -71,9 +71,9 @@ def _replace_refs(self, obj, ref_set): if changed: dirty = True return new_dict, ref_set, dirty - elif type(obj) == str: + elif isinstance(obj, str): return obj, ref_set, False - elif type(obj) == list: + elif isinstance(obj, list): n_list = len(obj) changed = False dirty = False @@ -82,7 +82,7 @@ def _replace_refs(self, obj, ref_set): if changed: dirty = True return obj, ref_set, dirty - elif type(obj) == tuple: + elif isinstance(obj, tuple): new_val, ref_set, changed = self._replace_refs(obj[1], ref_set) return (obj[0], new_val), ref_set, changed if hasattr(obj, '__iter__'): diff --git a/scripts/oscal_normalize.py b/scripts/oscal_normalize.py index 73a37b295..5b2027936 100644 --- a/scripts/oscal_normalize.py +++ b/scripts/oscal_normalize.py @@ -235,7 +235,7 @@ def add_ref_pattern(self, p, line): new_refs = p.findall(line) if new_refs: for r in new_refs: - if type(r) == tuple: + if isinstance(r, tuple): for s in r: self.add_ref_if_good(s) else: diff --git a/scripts/schema_integrity.py b/scripts/schema_integrity.py index fc004e443..cc1ed60f6 100644 --- a/scripts/schema_integrity.py +++ b/scripts/schema_integrity.py @@ -77,11 +77,11 @@ def recursive_ref(self, ref_key: str, dict_of_interest: Dict[str, Any]) -> List[ for key, value in dict_of_interest.items(): if key == ref_key: returner.append(value) - elif type(value) == dict: + elif isinstance(value, dict): returner = returner + self.recursive_ref(ref_key, value) - elif type(value) == list: + elif isinstance(value, list): for item in value: - if type(item) == dict: + if isinstance(item, dict): returner = returner + self.recursive_ref(ref_key, item) elif key == ref_key: returner.append(value) diff --git a/tests/trestle/core/base_model_test.py b/tests/trestle/core/base_model_test.py index bc1ff567c..61581bb5c 100644 --- a/tests/trestle/core/base_model_test.py +++ b/tests/trestle/core/base_model_test.py @@ -86,7 +86,7 @@ def test_is_oscal_base() -> None: """Test that the typing information is as expected.""" catalog = simple_catalog() - assert (isinstance(catalog, ospydantic.OscalBaseModel)) + assert isinstance(catalog, ospydantic.OscalBaseModel) def test_no_timezone_exception() -> None: @@ -104,7 +104,7 @@ def test_with_timezone() -> None: popo_json = json.loads(jsoned_catalog) time = popo_json['metadata']['last-modified'] - assert (type(time) == str) + assert isinstance(time, str) assert ('Z' in time or '+' in time or '-' in time) diff --git a/tests/trestle/core/generator_test.py b/tests/trestle/core/generator_test.py index 5ebe2f4e8..e50b08540 100644 --- a/tests/trestle/core/generator_test.py +++ b/tests/trestle/core/generator_test.py @@ -154,7 +154,7 @@ def test_gen_control() -> None: def test_ensure_optional_exists() -> None: """Explicit test to ensure that optional variables are populated.""" my_catalog = gens.generate_sample_model(catalog.Catalog, include_optional=True, depth=-1) - assert type(my_catalog.controls[0]) == catalog.Control + assert isinstance(my_catalog.controls[0], catalog.Control) def test_gen_party() -> None: diff --git a/tests/trestle/core/remote/cache_test.py b/tests/trestle/core/remote/cache_test.py index 51dbfe99a..1c7fdac39 100644 --- a/tests/trestle/core/remote/cache_test.py +++ b/tests/trestle/core/remote/cache_test.py @@ -276,13 +276,13 @@ def test_fetcher_factory(tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch as_file_uri('user/oscal_file.json'), as_file_uri('../user/oscal_file.json')]: fetcher = cache.FetcherFactory.get_fetcher(tmp_trestle_dir, uri) - assert type(fetcher) == cache.LocalFetcher + assert isinstance(fetcher, cache.LocalFetcher) # paths with drive letter for uri in ['C:\\Users\\user\\this.json', 'C:/Users/user/this.json', 'C:file.json']: if file_utils.is_windows(): fetcher = cache.FetcherFactory.get_fetcher(tmp_trestle_dir, uri) - assert type(fetcher) == cache.LocalFetcher + assert isinstance(fetcher, cache.LocalFetcher) else: with pytest.raises(TrestleError): cache.FetcherFactory.get_fetcher(tmp_trestle_dir, uri) @@ -291,15 +291,15 @@ def test_fetcher_factory(tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch monkeypatch.setenv('myusername', 'user123') monkeypatch.setenv('mypassword', 'somep4ss') fetcher = cache.FetcherFactory.get_fetcher(tmp_trestle_dir, https_uri) - assert type(fetcher) == cache.HTTPSFetcher + assert isinstance(fetcher, cache.HTTPSFetcher) sftp_uri = 'sftp://user@hostname:/path/to/file.json' fetcher = cache.FetcherFactory.get_fetcher(tmp_trestle_dir, sftp_uri) - assert type(fetcher) == cache.SFTPFetcher + assert isinstance(fetcher, cache.SFTPFetcher) sftp_uri = 'sftp://user@hostname:2000/path/to/file.json' fetcher = cache.FetcherFactory.get_fetcher(tmp_trestle_dir, sftp_uri) - assert type(fetcher) == cache.SFTPFetcher + assert isinstance(fetcher, cache.SFTPFetcher) def test_fetcher_expiration(tmp_trestle_dir: pathlib.Path) -> None: diff --git a/trestle/core/markdown/markdown_validator.py b/trestle/core/markdown/markdown_validator.py index 3cd91cea5..35b8ce30c 100644 --- a/trestle/core/markdown/markdown_validator.py +++ b/trestle/core/markdown/markdown_validator.py @@ -181,8 +181,8 @@ def compare_keys( return False for key in template.keys(): if key in candidate.keys(): - if type(template[key]) == dict: - if type(candidate[key]) == dict: + if isinstance(template[key], dict): + if isinstance(candidate[key], dict): status = cls.compare_keys(template[key], candidate[key], ignore_fields) if not status: return status diff --git a/trestle/tasks/cis_xlsx_to_oscal_catalog.py b/trestle/tasks/cis_xlsx_to_oscal_catalog.py index 1fd5d52d5..3eff90df5 100644 --- a/trestle/tasks/cis_xlsx_to_oscal_catalog.py +++ b/trestle/tasks/cis_xlsx_to_oscal_catalog.py @@ -41,7 +41,7 @@ logger = logging.getLogger(__name__) -timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc).isoformat() +timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat() class XlsxHelper: diff --git a/trestle/tasks/csv_to_oscal_cd.py b/trestle/tasks/csv_to_oscal_cd.py index a7343e02d..6c70d1c25 100644 --- a/trestle/tasks/csv_to_oscal_cd.py +++ b/trestle/tasks/csv_to_oscal_cd.py @@ -241,8 +241,7 @@ def print_info(self) -> None: def configure(self) -> bool: """Configure.""" - self._timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc - ).isoformat() + self._timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat() # config verbosity self._quiet = self._config.get('quiet', False) self._verbose = not self._quiet diff --git a/trestle/tasks/ocp4_cis_profile_to_oscal_catalog.py b/trestle/tasks/ocp4_cis_profile_to_oscal_catalog.py index fc2c00596..4b0f48f18 100644 --- a/trestle/tasks/ocp4_cis_profile_to_oscal_catalog.py +++ b/trestle/tasks/ocp4_cis_profile_to_oscal_catalog.py @@ -64,8 +64,7 @@ def __init__(self, config_object: Optional[configparser.SectionProxy]) -> None: config_object: Config section associated with the task. """ super().__init__(config_object) - self._timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc - ).isoformat() + self._timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat() def print_info(self) -> None: """Print the help string.""" diff --git a/trestle/tasks/ocp4_cis_profile_to_oscal_cd.py b/trestle/tasks/ocp4_cis_profile_to_oscal_cd.py index f04d4eaa0..cfc1cf7bb 100644 --- a/trestle/tasks/ocp4_cis_profile_to_oscal_cd.py +++ b/trestle/tasks/ocp4_cis_profile_to_oscal_cd.py @@ -63,8 +63,7 @@ def __init__(self, config_object: Optional[configparser.SectionProxy]) -> None: config_object: Config section associated with the task. """ super().__init__(config_object) - self._timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc - ).isoformat() + self._timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat() def set_timestamp(self, timestamp: str) -> None: """Set the timestamp.""" diff --git a/trestle/tasks/oscal_catalog_to_csv.py b/trestle/tasks/oscal_catalog_to_csv.py index b68ed8457..73b1d5b2e 100644 --- a/trestle/tasks/oscal_catalog_to_csv.py +++ b/trestle/tasks/oscal_catalog_to_csv.py @@ -37,7 +37,7 @@ logger = logging.getLogger(__name__) -timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc).isoformat() +timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat() recurse = True diff --git a/trestle/tasks/xlsx_to_oscal_cd.py b/trestle/tasks/xlsx_to_oscal_cd.py index fb966f15e..d55acf303 100644 --- a/trestle/tasks/xlsx_to_oscal_cd.py +++ b/trestle/tasks/xlsx_to_oscal_cd.py @@ -65,8 +65,7 @@ def __init__(self, config_object: Optional[configparser.SectionProxy]) -> None: """ super().__init__(config_object) self.xlsx_helper = XlsxHelper() - self._timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc - ).isoformat() + self._timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat() def set_timestamp(self, timestamp: str) -> None: """Set the timestamp.""" diff --git a/trestle/tasks/xlsx_to_oscal_profile.py b/trestle/tasks/xlsx_to_oscal_profile.py index faaa2ce74..84d587e0c 100644 --- a/trestle/tasks/xlsx_to_oscal_profile.py +++ b/trestle/tasks/xlsx_to_oscal_profile.py @@ -57,8 +57,7 @@ def __init__(self, config_object: Optional[configparser.SectionProxy]) -> None: """ super().__init__(config_object) self.xlsx_helper = XlsxHelper() - self._timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc - ).isoformat() + self._timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat() def set_timestamp(self, timestamp: str) -> None: """Set the timestamp.""" diff --git a/trestle/transforms/transformer_factory.py b/trestle/transforms/transformer_factory.py index 99f23e6c2..ba84bb4f3 100644 --- a/trestle/transforms/transformer_factory.py +++ b/trestle/transforms/transformer_factory.py @@ -28,7 +28,7 @@ class TransformerBase(ABC): """Abstract base interface for all transformers.""" # the current time for consistent timestamping - _timestamp = datetime.datetime.utcnow().replace(microsecond=0).replace(tzinfo=datetime.timezone.utc).isoformat() + _timestamp = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0).isoformat() @staticmethod def set_timestamp(value: str) -> None: