Skip to content

Commit

Permalink
Merge pull request #712 from nationalarchives/fix-types
Browse files Browse the repository at this point in the history
Fix typing issues
  • Loading branch information
jacksonj04 authored Oct 8, 2024
2 parents 7b8371d + c47f0ca commit bb65754
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 99 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ repos:
- boto3-stubs[s3,sns]
- types-python-dateutil
- types-pytz
- ds-caselaw-utils
- ds-caselaw-utils~=2.0.0
args:
- --strict
files: ^src/
Expand All @@ -48,7 +48,7 @@ repos:
- boto3-stubs[s3,sns]
- types-python-dateutil
- types-pytz
- ds-caselaw-utils
- ds-caselaw-utils~=2.0.0
files: ^tests/
id: mypy
name: mypy-tests
Expand Down
147 changes: 74 additions & 73 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ requests = "^2.28.2"
requests-toolbelt = ">=0.10.1,<1.1.0"
memoization = "^0.4.0"
lxml = "^5.0.0"
ds-caselaw-utils = "^1.4.1"
ds-caselaw-utils = "^2.0.0"
boto3 = "^1.26.112"
typing-extensions = "^4.7.1"
mypy-boto3-s3 = "^1.26.104"
Expand Down
3 changes: 2 additions & 1 deletion src/caselawclient/Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import environ
import requests
from ds_caselaw_utils.types import NeutralCitationString
from requests.auth import HTTPBasicAuth
from requests.structures import CaseInsensitiveDict
from requests_toolbelt.multipart import decoder
Expand Down Expand Up @@ -1035,7 +1036,7 @@ def search_judgments_and_decode_response(
search_parameters.collections = [DOCUMENT_COLLECTION_URI_JUDGMENT]
return self.search_and_decode_response(search_parameters)

def update_document_uri(self, old_uri: DocumentURIString, new_citation: str) -> DocumentURIString:
def update_document_uri(self, old_uri: DocumentURIString, new_citation: NeutralCitationString) -> DocumentURIString:
"""
Move the document at old_uri to the correct location based on the neutral citation
The new neutral citation *must* not already exist (that is handled elsewhere)
Expand Down
3 changes: 2 additions & 1 deletion src/caselawclient/models/documents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from ds_caselaw_utils import courts
from ds_caselaw_utils.courts import CourtNotFoundException
from ds_caselaw_utils.types import NeutralCitationString
from lxml import html as html_parser
from requests_toolbelt.multipart import decoder

Expand Down Expand Up @@ -441,7 +442,7 @@ def delete(self) -> None:
else:
raise DocumentNotSafeForDeletion

def move(self, new_citation: str) -> None:
def move(self, new_citation: NeutralCitationString) -> None:
self.api_client.update_document_uri(self.uri, new_citation)

def force_reparse(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/caselawclient/models/documents/body.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Optional

import pytz
from ds_caselaw_utils.courts import CourtCode
from ds_caselaw_utils.types import CourtCode

from caselawclient.models.utilities.dates import parse_string_date_as_utc

Expand Down
18 changes: 11 additions & 7 deletions src/caselawclient/models/judgments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from functools import cached_property
from typing import TYPE_CHECKING, Any, Optional

from ds_caselaw_utils.types import NeutralCitationString

from caselawclient.errors import DocumentNotFoundError
from caselawclient.models.neutral_citation_mixin import NeutralCitationMixin

Expand All @@ -23,13 +25,15 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
super(Judgment, self).__init__(self.document_noun, *args, **kwargs)

@cached_property
def neutral_citation(self) -> str:
return self.body.get_xpath_match_string(
"/akn:akomaNtoso/akn:*/akn:meta/akn:proprietary/uk:cite/text()",
{
"uk": "https://caselaw.nationalarchives.gov.uk/akn",
"akn": "http://docs.oasis-open.org/legaldocml/ns/akn/3.0",
},
def neutral_citation(self) -> NeutralCitationString:
return NeutralCitationString(
self.body.get_xpath_match_string(
"/akn:akomaNtoso/akn:*/akn:meta/akn:proprietary/uk:cite/text()",
{
"uk": "https://caselaw.nationalarchives.gov.uk/akn",
"akn": "http://docs.oasis-open.org/legaldocml/ns/akn/3.0",
},
)
)

@property
Expand Down
8 changes: 5 additions & 3 deletions src/caselawclient/models/neutral_citation_mixin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from abc import ABC, abstractmethod
from functools import cached_property
from typing import Any

from ds_caselaw_utils import neutral_url
from ds_caselaw_utils.types import NeutralCitationString


class NeutralCitationMixin:
class NeutralCitationMixin(ABC):
"""
A mixin class that provides functionality related to neutral citation.
Expand Down Expand Up @@ -35,8 +37,8 @@ def __init__(self, document_noun: str, *args: Any, **kwargs: Any) -> None:
super(NeutralCitationMixin, self).__init__(*args, **kwargs)

@cached_property
def neutral_citation(self) -> str:
return ""
@abstractmethod
def neutral_citation(self) -> NeutralCitationString: ...

@cached_property
def has_ncn(self) -> bool:
Expand Down
16 changes: 10 additions & 6 deletions src/caselawclient/models/press_summaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from functools import cached_property
from typing import TYPE_CHECKING, Any, Optional

from ds_caselaw_utils.types import NeutralCitationString

from caselawclient.errors import DocumentNotFoundError
from caselawclient.models.neutral_citation_mixin import NeutralCitationMixin

Expand All @@ -25,12 +27,14 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
super(PressSummary, self).__init__(self.document_noun, *args, **kwargs)

@cached_property
def neutral_citation(self) -> str:
return self.body.get_xpath_match_string(
"/akn:akomaNtoso/akn:doc/akn:preface/akn:p/akn:neutralCitation/text()",
{
"akn": "http://docs.oasis-open.org/legaldocml/ns/akn/3.0",
},
def neutral_citation(self) -> NeutralCitationString:
return NeutralCitationString(
self.body.get_xpath_match_string(
"/akn:akomaNtoso/akn:doc/akn:preface/akn:p/akn:neutralCitation/text()",
{
"akn": "http://docs.oasis-open.org/legaldocml/ns/akn/3.0",
},
)
)

@property
Expand Down
7 changes: 4 additions & 3 deletions src/caselawclient/models/utilities/move.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import TYPE_CHECKING, Any, Optional

import ds_caselaw_utils as caselawutils
from ds_caselaw_utils.types import NeutralCitationString

from caselawclient.errors import MarklogicAPIError
from caselawclient.models.documents import DocumentURIString
Expand All @@ -19,7 +20,7 @@ class MoveJudgmentError(Exception):


def update_document_uri(
source_uri: DocumentURIString, target_citation: str, api_client: "MarklogicApiClient"
source_uri: DocumentURIString, target_citation: NeutralCitationString, api_client: "MarklogicApiClient"
) -> DocumentURIString:
"""
Move the document at source_uri to the correct location based on the neutral citation
Expand All @@ -30,7 +31,7 @@ def update_document_uri(
:param api_client: An instance of MarklogicApiClient used to make the search request
:return: The URL associated with the `target_citation`
"""
new_ncn_based_uri = caselawutils.neutral_url(target_citation.strip())
new_ncn_based_uri = caselawutils.neutral_url(target_citation)
new_uri: Optional[DocumentURIString] = DocumentURIString(new_ncn_based_uri) if new_ncn_based_uri else None
if new_uri is None:
raise NeutralCitationToUriError(
Expand Down Expand Up @@ -63,7 +64,7 @@ def update_document_uri(
return new_uri


def set_metadata(old_uri: str, new_uri: str, api_client: Any) -> None:
def set_metadata(old_uri: DocumentURIString, new_uri: DocumentURIString, api_client: Any) -> None:
source_organisation = api_client.get_property(old_uri, "source-organisation")
source_name = api_client.get_property(old_uri, "source-name")
source_email = api_client.get_property(old_uri, "source-email")
Expand Down
3 changes: 2 additions & 1 deletion src/caselawclient/responses/search_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

from dateutil import parser as dateparser
from dateutil.parser import ParserError
from ds_caselaw_utils.courts import Court, CourtCode, CourtNotFoundException, JurisdictionCode, courts
from ds_caselaw_utils.courts import Court, CourtNotFoundException, courts
from ds_caselaw_utils.types import CourtCode, JurisdictionCode
from lxml import etree

from caselawclient.Client import MarklogicApiClient
Expand Down

0 comments on commit bb65754

Please sign in to comment.