From 7c7eb846eb18cc698cabe175fcca911350d9ea48 Mon Sep 17 00:00:00 2001 From: David McKee Date: Fri, 27 Sep 2024 11:06:28 +0100 Subject: [PATCH] lint --- .pre-commit-config.yaml | 2 ++ pyproject.toml | 3 +++ src/ds_caselaw_utils/courts.py | 19 ++++++++++++++----- src/ds_caselaw_utils/test_courts.py | 20 +++++++++++--------- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index dac23d8..00dcc1a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -42,6 +42,7 @@ repos: name: mypy-src additional_dependencies: - types-PyYAML + - ruamel.yaml args: - --strict files: ^(src/ds_caselaw_utils|scripts) @@ -53,6 +54,7 @@ repos: - id: mypy name: mypy-tests files: ^src/ds_caselaw_utils/test_ + exclude: courts_schema_types_autogenerated\.py - repo: https://github.com/python-jsonschema/check-jsonschema rev: 0.29.2 diff --git a/pyproject.toml b/pyproject.toml index 01f24ec..c712d61 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,3 +38,6 @@ update_changelog_on_bump = true [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" + +[tool.mypy] +exclude = ["ds_caselaw_utils/courts_schema_types_autogenerated.py"] diff --git a/src/ds_caselaw_utils/courts.py b/src/ds_caselaw_utils/courts.py index 3c7d748..e4f6e08 100644 --- a/src/ds_caselaw_utils/courts.py +++ b/src/ds_caselaw_utils/courts.py @@ -6,9 +6,17 @@ import pathlib from datetime import date -from typing import NewType, Optional +from typing import TYPE_CHECKING, NewType, Optional + +if TYPE_CHECKING: + from courts_schema_types_autogenerated import ( # type: ignore[import-not-found] + RawCourt, + RawCourtRepositoryData, + RawJurisdiction, + ) +else: + RawCourt = RawCourtRepositoryData = RawJurisdiction = None -from courts_schema_types_autogenerated import RawCourt, RawCourtRepositoryData, RawJurisdiction from ruamel.yaml import YAML CourtCode = NewType("CourtCode", str) @@ -31,9 +39,10 @@ def __init__(self, data: RawCourt) -> None: self.link: str = data["link"] self.ncn: Optional[str] = data.get("ncn") self.canonical_param: CourtParam = CourtParam(data.get("param")) - self.param_aliases: list[CourtParam] = [CourtParam(data.get("param"))] + ( - [CourtParam(extra_param) for extra_param in data.get("extra_params")] or [] - ) + self.param_aliases: list[CourtParam] = [CourtParam(data.get("param"))] + [ + CourtParam(extra_param) for extra_param in data.get("extra_params", []) + ] + self.start_year: Optional[int] = data.get("start_year") self.end_year: int = data.get("end_year") or date.today().year self.jurisdictions: list[Jurisdiction] = [ diff --git a/src/ds_caselaw_utils/test_courts.py b/src/ds_caselaw_utils/test_courts.py index b0ba4c8..adceb71 100644 --- a/src/ds_caselaw_utils/test_courts.py +++ b/src/ds_caselaw_utils/test_courts.py @@ -15,6 +15,7 @@ CourtWithJurisdiction, courts, ) +from .factory import CourtFactory, make_court_repo_valid def mock_with_properties(properties={}): @@ -48,6 +49,7 @@ def test_loads_all_courts_with_jurisdictions(self): "courts": [{"name": "court1", "jurisdictions": [{"name": "jurisdiction1"}]}], } ] + data = make_court_repo_valid(data) repo = CourtsRepository(data) courts = repo.get_all(with_jurisdictions=True) self.assertIn("court1", [c.name for c in courts]) @@ -318,43 +320,43 @@ def test_returns_grouped_selectable_tribunals(self): class TestCourt(unittest.TestCase): def test_repr_string(self): - court = Court({"name": "court_name"}) + court = CourtFactory({"name": "court_name"}) self.assertEqual("court_name", str(court)) self.assertEqual("court_name", repr(court)) def test_grouped_name_explicit(self): - court = Court({"grouped_name": "court_name"}) + court = CourtFactory({"grouped_name": "court_name"}) self.assertEqual("court_name", court.grouped_name) def test_grouped_name_default(self): - court = Court({"name": "court_name"}) + court = CourtFactory({"name": "court_name"}) self.assertEqual("court_name", court.grouped_name) def test_param_aliases(self): - court = Court({"param": "param_1", "extra_params": ["param_2"]}) + court = CourtFactory({"param": "param_1", "extra_params": ["param_2"]}) self.assertEqual(["param_1", "param_2"], court.param_aliases) def test_end_year_explicit(self): - court = Court({"end_year": 1983}) + court = CourtFactory({"end_year": 1983}) self.assertEqual(1983, court.end_year) def test_end_year_default(self): - court = Court({}) + court = CourtFactory({}) self.assertEqual(date.today().year, court.end_year) def test_get_jurisdiction(self): - court = Court({"jurisdictions": [{"code": "jurisdiction1", "name": "Jurisdiction 1"}]}) + court = CourtFactory({"jurisdictions": [{"code": "jurisdiction1", "name": "Jurisdiction 1"}]}) jurisdiction = court.get_jurisdiction("jurisdiction1") assert jurisdiction self.assertEqual("Jurisdiction 1", jurisdiction.name) def test_get_nonexistent_jurisdiction(self): - court = Court({"jurisdictions": [{"code": "jurisdiction1", "name": "Jurisdiction 1"}]}) + court = CourtFactory({"jurisdictions": [{"code": "jurisdiction1", "name": "Jurisdiction 1"}]}) jurisdiction = court.get_jurisdiction("jurisdiction2") self.assertIsNone(jurisdiction) def test_expand_jurisdictions(self): - court = Court( + court = CourtFactory( { "name": "Court 1", "jurisdictions": [{"code": "jurisdiction1", "name": "Jurisdiction 1"}],