From ed283c22ebf54b5c18e05d2b49e5c021dc734cdb Mon Sep 17 00:00:00 2001 From: David McKee Date: Fri, 27 Sep 2024 15:19:34 +0100 Subject: [PATCH] Fix types --- pyproject.toml | 4 +- .../courts_schema_types_autogenerated.py | 34 +++++------ .../data/schema/courts.schema.json | 1 + src/ds_caselaw_utils/factory.py | 6 +- src/ds_caselaw_utils/test_courts.py | 57 ++++++++++--------- 5 files changed, 54 insertions(+), 48 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 26c7133..332960b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,5 +40,5 @@ update_changelog_on_bump = true requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" -[tool.mypy] -exclude = ["ds_caselaw_utils/courts_schema_types_autogenerated.py"] +# [tool.mypy] +# exclude = ["ds_caselaw_utils/courts_schema_types_autogenerated.py"] diff --git a/src/ds_caselaw_utils/courts_schema_types_autogenerated.py b/src/ds_caselaw_utils/courts_schema_types_autogenerated.py index 762b91f..ee1b204 100644 --- a/src/ds_caselaw_utils/courts_schema_types_autogenerated.py +++ b/src/ds_caselaw_utils/courts_schema_types_autogenerated.py @@ -44,7 +44,24 @@ class RawCourt(TypedDict, total=False): jurisdictions: List["RawJurisdiction"] -RawCourtRepository = List["_RawCourtListItem"] +class RawCourtGroup(TypedDict, total=False): + """ Raw Court Group. """ + + name: Required[str] + """ Required property """ + + display_name: Required[Union[str, None]] + """ Required property """ + + is_tribunal: Required[bool] + """ Required property """ + + courts: Required[List["RawCourt"]] + """ Required property """ + + + +RawCourtRepository = List["RawCourtGroup"] """ Raw Court List. @@ -70,18 +87,3 @@ class RawJurisdiction(TypedDict, total=False): _RawCourtExtraParamsItem = str """ pattern: ^[a-z]{2,}(/[a-z]+)?$ """ - - -class _RawCourtListItem(TypedDict, total=False): - name: Required[str] - """ Required property """ - - display_name: Required[Union[str, None]] - """ Required property """ - - is_tribunal: Required[bool] - """ Required property """ - - courts: Required[List["RawCourt"]] - """ Required property """ - diff --git a/src/ds_caselaw_utils/data/schema/courts.schema.json b/src/ds_caselaw_utils/data/schema/courts.schema.json index 06936f9..c518a87 100644 --- a/src/ds_caselaw_utils/data/schema/courts.schema.json +++ b/src/ds_caselaw_utils/data/schema/courts.schema.json @@ -6,6 +6,7 @@ "type": "array", "items": { "type": "object", + "title": "Raw Court Group", "properties": { "name": { "type": "string" diff --git a/src/ds_caselaw_utils/factory.py b/src/ds_caselaw_utils/factory.py index ada9998..a22b308 100644 --- a/src/ds_caselaw_utils/factory.py +++ b/src/ds_caselaw_utils/factory.py @@ -1,3 +1,5 @@ +import typing + from .courts import Court from .courts_schema_types_autogenerated import RawCourt, RawCourtRepository @@ -15,7 +17,7 @@ def make_court_valid(data) -> RawCourt: for keyword in ["selectable", "listable"]: if keyword not in data: data[keyword] = True - return data + return typing.cast(RawCourt, data) def make_court_repo_valid(data) -> RawCourtRepository: @@ -26,4 +28,4 @@ def make_court_repo_valid(data) -> RawCourtRepository: new_courts.append(make_court_valid(court)) court_group["court"] = new_courts new_court_groups.append(court_group) - return new_court_groups + return RawCourtRepository(new_court_groups) diff --git a/src/ds_caselaw_utils/test_courts.py b/src/ds_caselaw_utils/test_courts.py index b4d04fd..3cabbea 100644 --- a/src/ds_caselaw_utils/test_courts.py +++ b/src/ds_caselaw_utils/test_courts.py @@ -5,6 +5,7 @@ from ruamel.yaml import YAML +from ds_caselaw_utils.courts_schema_types_autogenerated import RawCourtRepository from ds_caselaw_utils.factory import CourtFactory, make_court_repo_valid from .courts import ( @@ -37,8 +38,8 @@ def test_loads_all_courts_without_jurisdictions(self): "courts": [{"name": "court1", "jurisdictions": [{"name": "jurisdiction1", "code": "code"}]}], } ] - data = make_court_repo_valid(data) - repo = CourtsRepository(data) + valid_data: RawCourtRepository = make_court_repo_valid(data) + repo = CourtsRepository(valid_data) courts = repo.get_all() self.assertIn("court1", [c.name for c in courts]) self.assertNotIn("court1 – jurisdiction1", [c.name for c in courts]) @@ -51,8 +52,8 @@ def test_loads_all_courts_with_jurisdictions(self): "courts": [{"name": "court1", "jurisdictions": [{"name": "jurisdiction1", "code": "code"}]}], } ] - data = make_court_repo_valid(data) - repo = CourtsRepository(data) + valid_data = make_court_repo_valid(data) + repo = CourtsRepository(valid_data) courts = repo.get_all(with_jurisdictions=True) self.assertIn("court1", [c.name for c in courts]) self.assertIn("court1 – jurisdiction1", [c.name for c in courts]) @@ -76,8 +77,8 @@ def test_loads_selectable_courts(self): "courts": [{"name": "court3", "selectable": False}], }, ] - data = make_court_repo_valid(data) - repo = CourtsRepository(data) + valid_data = make_court_repo_valid(data) + repo = CourtsRepository(valid_data) selectable = repo.get_selectable() self.assertIn("court1", [c.name for c in selectable]) self.assertNotIn("court2", [c.name for c in selectable]) @@ -107,8 +108,8 @@ def test_loads_listable_courts(self): "courts": [{"name": "court3", "listable": False}], }, ] - data = make_court_repo_valid(data) - repo = CourtsRepository(data) + valid_data = make_court_repo_valid(data) + repo = CourtsRepository(valid_data) groups = repo.get_listable_groups() self.assertIn("court group 1", [g.name for g in groups]) self.assertNotIn("court group 2", [g.name for g in groups]) @@ -127,8 +128,8 @@ def test_loads_court_by_param(self): "courts": [{"param": "court2", "name": "Court 2"}], }, ] - data = make_court_repo_valid(data) - repo = CourtsRepository(data) + valid_data = make_court_repo_valid(data) + repo = CourtsRepository(valid_data) self.assertEqual("Court 2", repo.get_by_param(CourtParam("court2")).name) def test_raises_on_unknown_court_param(self): @@ -142,8 +143,8 @@ def test_raises_on_unknown_court_param(self): "courts": [{"param": "court2", "name": "Court 2"}], }, ] - data = make_court_repo_valid(data) - repo = CourtsRepository(data) + valid_data = make_court_repo_valid(data) + repo = CourtsRepository(valid_data) self.assertRaises(CourtNotFoundException, repo.get_by_param, "court3") def test_loads_court_by_code(self): @@ -157,8 +158,8 @@ def test_loads_court_by_code(self): "courts": [{"code": "court2", "name": "Court 2"}], }, ] - data = make_court_repo_valid(data) - repo = CourtsRepository(data) + valid_data = make_court_repo_valid(data) + repo = CourtsRepository(valid_data) self.assertEqual("Court 2", repo.get_by_code(CourtCode("court2")).name) def test_loads_court_with_jurisdiction_by_code(self): @@ -174,8 +175,8 @@ def test_loads_court_with_jurisdiction_by_code(self): ], } ] - data = make_court_repo_valid(data) - repo = CourtsRepository(data) + valid_data = make_court_repo_valid(data) + repo = CourtsRepository(valid_data) self.assertEqual("Court 1 – Jurisdiction 1", repo.get_by_code(CourtCode("court1/jurisdiction1")).name) def test_raises_error_for_nonexistent_jurisdictions(self): @@ -191,8 +192,8 @@ def test_raises_error_for_nonexistent_jurisdictions(self): ], } ] - data = make_court_repo_valid(data) - repo = CourtsRepository(data) + valid_data = make_court_repo_valid(data) + repo = CourtsRepository(valid_data) self.assertRaises(CourtNotFoundException, repo.get_by_code, "court1/jurisdiction2") self.assertRaises(CourtNotFoundException, repo.get_by_code, "court2/jurisdiction1") @@ -207,8 +208,8 @@ def test_raises_on_unknown_court_code(self): "courts": [{"code": "court2", "name": "Court 2"}], }, ] - data = make_court_repo_valid(data) - repo = CourtsRepository(data) + valid_data = make_court_repo_valid(data) + repo = CourtsRepository(valid_data) self.assertRaises(CourtNotFoundException, repo.get_by_code, "court3") def test_returns_listable_courts(self): @@ -227,8 +228,8 @@ def test_returns_listable_courts(self): "courts": [{"param": "court3", "listable": True, "name": "Court 3"}], }, ] - data = make_court_repo_valid(data) - repo = CourtsRepository(data) + valid_data = make_court_repo_valid(data) + repo = CourtsRepository(valid_data) self.assertIn("court1", [c.canonical_param for c in repo.get_listable_courts()]) self.assertNotIn("court2", [c.canonical_param for c in repo.get_listable_courts()]) self.assertNotIn("court3", [c.canonical_param for c in repo.get_listable_courts()]) @@ -251,8 +252,8 @@ def test_returns_listable_tribunals(self): ], }, ] - data = make_court_repo_valid(data) - repo = CourtsRepository(data) + valid_data = make_court_repo_valid(data) + repo = CourtsRepository(valid_data) self.assertNotIn("court1", [c.canonical_param for c in repo.get_listable_tribunals()]) self.assertNotIn("court2", [c.canonical_param for c in repo.get_listable_tribunals()]) self.assertIn("court3", [c.canonical_param for c in repo.get_listable_tribunals()]) @@ -285,8 +286,8 @@ def test_returns_grouped_selectable_courts(self): ], }, ] - data = make_court_repo_valid(data) - repo = CourtsRepository(data) + valid_data = make_court_repo_valid(data) + repo = CourtsRepository(valid_data) groups = repo.get_grouped_selectable_courts() self.assertIn("Court group", [g.name for g in groups]) self.assertNotIn("Tribunal group", [g.name for g in groups]) @@ -322,8 +323,8 @@ def test_returns_grouped_selectable_tribunals(self): ], }, ] - data = make_court_repo_valid(data) - repo = CourtsRepository(data) + valid_data = make_court_repo_valid(data) + repo = CourtsRepository(valid_data) groups = repo.get_grouped_selectable_tribunals() self.assertIn("Tribunal group", [g.name for g in groups]) self.assertNotIn("Court group", [g.name for g in groups])