Skip to content

Commit

Permalink
style(FCL-343): fix type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-dxw committed Sep 27, 2024
1 parent a6cbbcc commit 2d06b76
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 76 deletions.
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,3 @@ 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"]
58 changes: 31 additions & 27 deletions src/ds_caselaw_utils/courts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,45 @@

import pathlib
from datetime import date
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 typing import NewType, Optional

from ruamel.yaml import YAML

from ds_caselaw_utils.courts_schema_types_autogenerated import (
RawCourt,
RawCourtRepository,
RawJurisdiction,
)

CourtCode = NewType("CourtCode", str)
CourtParam = NewType("CourtParam", str)
JurisdictionCode = NewType("JurisdictionCode", str)


class Jurisdiction:
def __init__(self, data: RawJurisdiction):
self.code: JurisdictionCode = data["code"]
self.code: JurisdictionCode = JurisdictionCode(data["code"])
self.name = data["name"]
self.prefix: str = data.get("prefix")
self.prefix: Optional[str] = data.get("prefix")


class Court:
def __init__(self, data: RawCourt) -> None:
self.canonical_param: Optional[CourtParam]
self.param_aliases: list[CourtParam]
self.code: CourtCode = CourtCode(data["code"])
self.name: str = data["name"]
self.grouped_name: str = data.get("grouped_name") or data["name"]
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", [])
]
if "param" in data:
self.canonical_param = CourtParam(data["param"])
self.param_aliases = [CourtParam(data["param"])] + [
CourtParam(extra_param) for extra_param in data.get("extra_params", [])
]
else:
self.canonical_param = None
self.param_aliases = []

self.start_year: Optional[int] = data.get("start_year")
self.end_year: int = data.get("end_year") or date.today().year
Expand Down Expand Up @@ -86,7 +89,7 @@ def ncn(self) -> Optional[str]:
return self.court.ncn

@property
def canonical_param(self) -> CourtParam:
def canonical_param(self) -> Optional[CourtParam]:
return self.court.canonical_param

@property
Expand Down Expand Up @@ -122,14 +125,15 @@ class CourtNotFoundException(Exception):


class CourtsRepository:
def __init__(self, data: RawCourtRepositoryData):
def __init__(self, data: RawCourtRepository):
self._data = data
self._byParam = {}
self._byCode = {}
for group in self._data:
for courtData in group.get("courts"):
for courtData in group.get("courts", []):
court = Court(courtData)
self._byParam[courtData.get("param")] = court
if "param" in courtData:
self._byParam[courtData.get("param")] = court
self._byCode[courtData["code"]] = court

def get_by_param(self, param: CourtParam) -> Court:
Expand Down Expand Up @@ -163,7 +167,7 @@ def get_by_code(self, code: CourtCode) -> Court:
return self.get_court_by_code(code)

def get_all(self, with_jurisdictions: bool = False) -> list[Court]:
courts = [Court(court) for category in self._data for court in category.get("courts")]
courts = [Court(court) for category in self._data for court in category.get("courts", [])]
if with_jurisdictions:
return [c for court in courts for c in court.expand_jurisdictions()]
else:
Expand All @@ -172,15 +176,15 @@ def get_all(self, with_jurisdictions: bool = False) -> list[Court]:
def get_selectable(self) -> list[Court]:
courts = []
for category in self._data:
for court in category.get("courts"):
for court in category.get("courts", []):
if court["selectable"]:
courts.append(Court(court))
return courts

def get_selectable_groups(self) -> list[CourtGroup]:
groups = []
for category in self._data:
courts = [Court(court) for court in category.get("courts") if court["selectable"]]
courts = [Court(court) for court in category.get("courts", []) if court["selectable"]]
if len(courts) > 0:
groups.append(CourtGroup(category.get("display_name"), courts))
return groups
Expand All @@ -189,7 +193,7 @@ def get_grouped_selectable_courts(self) -> list[CourtGroup]:
groups = []
for category in self._data:
if not category.get("is_tribunal"):
courts = [Court(court) for court in category.get("courts") if court["selectable"]]
courts = [Court(court) for court in category.get("courts", []) if court["selectable"]]
if len(courts) > 0:
groups.append(CourtGroup(category.get("display_name"), courts))
return groups
Expand All @@ -198,15 +202,15 @@ def get_grouped_selectable_tribunals(self) -> list[CourtGroup]:
groups = []
for category in self._data:
if category.get("is_tribunal"):
courts = [Court(court) for court in category.get("courts") if court["selectable"]]
courts = [Court(court) for court in category.get("courts", []) if court["selectable"]]
if len(courts) > 0:
groups.append(CourtGroup(category.get("display_name"), courts))
return groups

def get_listable_groups(self) -> list[CourtGroup]:
groups = []
for category in self._data:
courts = [Court(court) for court in category.get("courts") if court["listable"]]
courts = [Court(court) for court in category.get("courts", []) if court["listable"]]
if len(courts) > 0:
groups.append(CourtGroup(category.get("display_name"), courts))
return groups
Expand All @@ -233,6 +237,6 @@ def get_listable_tribunals(self) -> list[Court]:
yaml = YAML()
datafile = pathlib.Path(__file__).parent / "data/court_names.yaml"
with open(datafile) as f:
court_data: RawCourtRepositoryData = yaml.load(f)
court_data: RawCourtRepository = yaml.load(f)

courts = CourtsRepository(court_data)
34 changes: 18 additions & 16 deletions src/ds_caselaw_utils/courts_schema_types_autogenerated.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 """

1 change: 1 addition & 0 deletions src/ds_caselaw_utils/data/schema/courts.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"type": "array",
"items": {
"type": "object",
"title": "Raw Court Group",
"properties": {
"name": {
"type": "string"
Expand Down
7 changes: 5 additions & 2 deletions src/ds_caselaw_utils/factory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import typing

from .courts import Court
from .courts_schema_types_autogenerated import RawCourt, RawCourtRepository

Expand All @@ -15,7 +17,8 @@ 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:
Expand All @@ -26,4 +29,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 typing.cast(RawCourtRepository, new_court_groups)
56 changes: 28 additions & 28 deletions src/ds_caselaw_utils/test_courts.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,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 = 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])
Expand All @@ -51,8 +51,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])
Expand All @@ -76,8 +76,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])
Expand Down Expand Up @@ -107,8 +107,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])
Expand All @@ -127,8 +127,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):
Expand All @@ -142,8 +142,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):
Expand All @@ -157,8 +157,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):
Expand All @@ -174,8 +174,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):
Expand All @@ -191,8 +191,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")

Expand All @@ -207,8 +207,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):
Expand All @@ -227,8 +227,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()])
Expand All @@ -251,8 +251,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()])
Expand Down Expand Up @@ -285,8 +285,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])
Expand Down Expand Up @@ -322,8 +322,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])
Expand Down

0 comments on commit 2d06b76

Please sign in to comment.