From d186d351003b74ec6df521e3799eb03b5b97f9f2 Mon Sep 17 00:00:00 2001 From: Nick Jackson Date: Thu, 26 Sep 2024 11:13:38 +0100 Subject: [PATCH 1/4] test(ruff): add SIM tests to ruff configuration These tests are for code which can be simplified, making it easier to reason about behaviour. --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 192d00d1..77b80bf3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,9 +64,9 @@ line-length = 120 [tool.ruff.lint] ignore = ["E501", "G004", "PLR2004", "RUF005", "RUF012", "UP040"] # longlines, fstrings in logs, magic values, consider not concat, mutable classbits, type instead of TypeAlias -extend-select = ["W", "I", "SLF"] +extend-select = ["W", "I", "SLF", "SIM"] # extend-select = [ "B", "Q", "C90", "I", "UP", "YTT", "ASYNC", "S", "BLE", "A", "COM", "C4", "DTZ", "T10", "DJ", "EM", "EXE", "FA", -# "ISC", "ICN", "G", "INP", "PIE", "T20", "PYI", "PT", "Q", "RSE", "RET", "SLOT", "SIM", "TID", "TCH", "INT", "PTH", +# "ISC", "ICN", "G", "INP", "PIE", "T20", "PYI", "PT", "Q", "RSE", "RET", "SLOT", "TID", "TCH", "INT", "PTH", # "FIX", "PGH", "PL", "TRY", "FLY", "PERF", "RUF"] unfixable = ["ERA"] From 9ce6a34bb1daf58c56a844833eac3ee550d74854 Mon Sep 17 00:00:00 2001 From: Nick Jackson Date: Thu, 26 Sep 2024 11:20:49 +0100 Subject: [PATCH 2/4] style(FCL-331): combine nested with statements --- tests/client/test_advanced_search.py | 154 ++++++++-------- .../client/test_checkout_checkin_judgment.py | 35 ++-- tests/client/test_eval_xslt.py | 167 +++++++++--------- .../client/test_save_copy_delete_judgment.py | 63 +++---- tests/client/test_verify_show_unpublished.py | 11 +- tests/models/documents/test_documents.py | 19 +- 6 files changed, 219 insertions(+), 230 deletions(-) diff --git a/tests/client/test_advanced_search.py b/tests/client/test_advanced_search.py index 4dfbff1e..37687859 100644 --- a/tests/client/test_advanced_search.py +++ b/tests/client/test_advanced_search.py @@ -128,46 +128,45 @@ def test_user_can_view_unpublished_but_show_unpublished_is_false( When the advanced_search method is called with the show_unpublished parameter set to False Then it should call the MarkLogic module with the expected query parameters """ - with patch.object(self.client, "invoke") as mock_invoke: - with patch.object( - self.client, - "user_can_view_unpublished_judgments", - return_value=True, - ): - self.client.advanced_search( - SearchParameters( - query="my-query", - court="ewhc", - judge="a. judge", - party="a party", - page=2, - page_size=20, - show_unpublished=False, - ), - ) - - expected_vars = { - "court": ["ewhc"], - "judge": "a. judge", - "page": 2, - "page-size": 20, - "q": "my-query", - "party": "a party", - "neutral_citation": "", - "specific_keyword": "", - "order": "", - "from": "", - "to": "", - "show_unpublished": "false", - "only_unpublished": "false", - "collections": "", - "quoted_phrases": [], - } - - mock_invoke.assert_called_with( - "/judgments/search/search-v2.xqy", - json.dumps(expected_vars), - ) + with patch.object(self.client, "invoke") as mock_invoke, patch.object( + self.client, + "user_can_view_unpublished_judgments", + return_value=True, + ): + self.client.advanced_search( + SearchParameters( + query="my-query", + court="ewhc", + judge="a. judge", + party="a party", + page=2, + page_size=20, + show_unpublished=False, + ), + ) + + expected_vars = { + "court": ["ewhc"], + "judge": "a. judge", + "page": 2, + "page-size": 20, + "q": "my-query", + "party": "a party", + "neutral_citation": "", + "specific_keyword": "", + "order": "", + "from": "", + "to": "", + "show_unpublished": "false", + "only_unpublished": "false", + "collections": "", + "quoted_phrases": [], + } + + mock_invoke.assert_called_with( + "/judgments/search/search-v2.xqy", + json.dumps(expected_vars), + ) def test_user_can_view_unpublished_and_show_unpublished_is_true( self, @@ -178,24 +177,23 @@ def test_user_can_view_unpublished_and_show_unpublished_is_true( When the advanced_search method is called with the show_unpublished parameter set to True Then it should call the MarkLogic module with the expected query parameters """ - with patch.object(self.client, "invoke"): - with patch.object( - self.client, - "user_can_view_unpublished_judgments", - return_value=True, - ): - self.client.advanced_search( - SearchParameters( - query="my-query", - court="ewhc", - judge="a. judge", - party="a party", - page=2, - page_size=20, - show_unpublished=True, - ), - ) - assert '"show_unpublished": "true"' in self.client.invoke.call_args.args[1] + with patch.object(self.client, "invoke"), patch.object( + self.client, + "user_can_view_unpublished_judgments", + return_value=True, + ): + self.client.advanced_search( + SearchParameters( + query="my-query", + court="ewhc", + judge="a. judge", + party="a party", + page=2, + page_size=20, + show_unpublished=True, + ), + ) + assert '"show_unpublished": "true"' in self.client.invoke.call_args.args[1] def test_user_cannot_view_unpublished_but_show_unpublished_is_true( self, @@ -206,27 +204,25 @@ def test_user_cannot_view_unpublished_but_show_unpublished_is_true( When the advanced_search method is called with the show_unpublished parameter set to True Then it should call the MarkLogic module with the show_unpublished parameter set to False and log a warning """ - with patch.object(self.client, "invoke"): - with patch.object( - self.client, - "user_can_view_unpublished_judgments", - return_value=False, - ): - with patch.object(logging, "warning") as mock_logging: - self.client.advanced_search( - SearchParameters( - query="my-query", - court="ewhc", - judge="a. judge", - party="a party", - page=2, - page_size=20, - show_unpublished=True, - ), - ) - - assert '"show_unpublished": "false"' in self.client.invoke.call_args.args[1] - mock_logging.assert_called() + with patch.object(self.client, "invoke"), patch.object( + self.client, + "user_can_view_unpublished_judgments", + return_value=False, + ), patch.object(logging, "warning") as mock_logging: + self.client.advanced_search( + SearchParameters( + query="my-query", + court="ewhc", + judge="a. judge", + party="a party", + page=2, + page_size=20, + show_unpublished=True, + ), + ) + + assert '"show_unpublished": "false"' in self.client.invoke.call_args.args[1] + mock_logging.assert_called() def test_no_page_0(self): """ diff --git a/tests/client/test_checkout_checkin_judgment.py b/tests/client/test_checkout_checkin_judgment.py index a6742bdd..f42be04f 100644 --- a/tests/client/test_checkout_checkin_judgment.py +++ b/tests/client/test_checkout_checkin_judgment.py @@ -29,24 +29,23 @@ def test_checkout_judgment(self): ) def test_checkout_judgment_with_timeout(self): - with patch.object(self.client, "eval") as mock_eval: - with patch.object( - self.client, - "calculate_seconds_until_midnight", - return_value=3600, - ): - uri = "/ewca/civ/2004/632" - annotation = "locked by A KITTEN" - expires_at_midnight = True - expected_vars = { - "uri": "/ewca/civ/2004/632.xml", - "annotation": "locked by A KITTEN", - "timeout": 3600, - } - self.client.checkout_judgment(uri, annotation, expires_at_midnight) - - assert mock_eval.call_args.args[0] == (os.path.join(ROOT_DIR, "xquery", "checkout_judgment.xqy")) - assert mock_eval.call_args.kwargs["vars"] == json.dumps(expected_vars) + with patch.object(self.client, "eval") as mock_eval, patch.object( + self.client, + "calculate_seconds_until_midnight", + return_value=3600, + ): + uri = "/ewca/civ/2004/632" + annotation = "locked by A KITTEN" + expires_at_midnight = True + expected_vars = { + "uri": "/ewca/civ/2004/632.xml", + "annotation": "locked by A KITTEN", + "timeout": 3600, + } + self.client.checkout_judgment(uri, annotation, expires_at_midnight) + + assert mock_eval.call_args.args[0] == (os.path.join(ROOT_DIR, "xquery", "checkout_judgment.xqy")) + assert mock_eval.call_args.kwargs["vars"] == json.dumps(expected_vars) def test_checkin_judgment(self): with patch.object(self.client, "eval") as mock_eval: diff --git a/tests/client/test_eval_xslt.py b/tests/client/test_eval_xslt.py index 8faf499f..8d6b27ac 100644 --- a/tests/client/test_eval_xslt.py +++ b/tests/client/test_eval_xslt.py @@ -14,105 +14,100 @@ def setUp(self): @patch.dict(os.environ, {"XSLT_IMAGE_LOCATION": "imagepath"}, clear=True) def test_eval_xslt_user_can_view_unpublished(self): - with patch.object(self.client, "eval") as mock_eval: - with patch.object( - self.client, - "user_can_view_unpublished_judgments", - return_value=True, - ): - uri = "/judgment/uri" - expected_vars: XsltTransformDict = { - "uri": "/judgment/uri.xml", - "version_uri": None, - "show_unpublished": True, - "img_location": "imagepath", - "xsl_filename": "accessible-html.xsl", - "query": None, - } - self.client.eval_xslt(uri, show_unpublished=True) + with patch.object(self.client, "eval") as mock_eval, patch.object( + self.client, + "user_can_view_unpublished_judgments", + return_value=True, + ): + uri = "/judgment/uri" + expected_vars: XsltTransformDict = { + "uri": "/judgment/uri.xml", + "version_uri": None, + "show_unpublished": True, + "img_location": "imagepath", + "xsl_filename": "accessible-html.xsl", + "query": None, + } + self.client.eval_xslt(uri, show_unpublished=True) - assert mock_eval.call_args.args[0] == (os.path.join(ROOT_DIR, "xquery", "xslt_transform.xqy")) - assert mock_eval.call_args.kwargs["vars"] == json.dumps(expected_vars) + assert mock_eval.call_args.args[0] == (os.path.join(ROOT_DIR, "xquery", "xslt_transform.xqy")) + assert mock_eval.call_args.kwargs["vars"] == json.dumps(expected_vars) @patch.dict(os.environ, {"XSLT_IMAGE_LOCATION": "imagepath"}, clear=True) def test_eval_xslt_user_cannot_view_unpublished(self): """The user is not permitted to see unpublished judgments but is attempting to view them Set `show_unpublished` to false and log a warning""" - with patch.object(self.client, "eval") as mock_eval: - with patch.object( - self.client, - "user_can_view_unpublished_judgments", - return_value=False, - ): - with patch.object(logging, "warning") as mock_logging: - uri = "/judgment/uri" - expected_vars: XsltTransformDict = { - "uri": "/judgment/uri.xml", - "version_uri": None, - "show_unpublished": False, - "img_location": "imagepath", - "xsl_filename": "accessible-html.xsl", - "query": None, - } - self.client.eval_xslt(uri, show_unpublished=True) + with patch.object(self.client, "eval") as mock_eval, patch.object( + self.client, + "user_can_view_unpublished_judgments", + return_value=False, + ), patch.object(logging, "warning") as mock_logging: + uri = "/judgment/uri" + expected_vars: XsltTransformDict = { + "uri": "/judgment/uri.xml", + "version_uri": None, + "show_unpublished": False, + "img_location": "imagepath", + "xsl_filename": "accessible-html.xsl", + "query": None, + } + self.client.eval_xslt(uri, show_unpublished=True) - assert mock_eval.call_args.args[0] == (os.path.join(ROOT_DIR, "xquery", "xslt_transform.xqy")) - assert mock_eval.call_args.kwargs["vars"] == json.dumps( - expected_vars, - ) - mock_logging.assert_called() + assert mock_eval.call_args.args[0] == (os.path.join(ROOT_DIR, "xquery", "xslt_transform.xqy")) + assert mock_eval.call_args.kwargs["vars"] == json.dumps( + expected_vars, + ) + mock_logging.assert_called() @patch.dict(os.environ, {"XSLT_IMAGE_LOCATION": "imagepath"}, clear=True) def test_eval_xslt_with_filename(self): - with patch.object(self.client, "eval") as mock_eval: - with patch.object( - self.client, - "user_can_view_unpublished_judgments", - return_value=True, - ): - uri = "/judgment/uri" - expected_vars: XsltTransformDict = { - "uri": "/judgment/uri.xml", - "version_uri": None, - "show_unpublished": True, - "img_location": "imagepath", - "xsl_filename": "as-handed-down.xsl", - "query": None, - } - self.client.eval_xslt( - uri, - show_unpublished=True, - xsl_filename="as-handed-down.xsl", - ) + with patch.object(self.client, "eval") as mock_eval, patch.object( + self.client, + "user_can_view_unpublished_judgments", + return_value=True, + ): + uri = "/judgment/uri" + expected_vars: XsltTransformDict = { + "uri": "/judgment/uri.xml", + "version_uri": None, + "show_unpublished": True, + "img_location": "imagepath", + "xsl_filename": "as-handed-down.xsl", + "query": None, + } + self.client.eval_xslt( + uri, + show_unpublished=True, + xsl_filename="as-handed-down.xsl", + ) - assert mock_eval.call_args.args[0] == (os.path.join(ROOT_DIR, "xquery", "xslt_transform.xqy")) - assert mock_eval.call_args.kwargs["vars"] == json.dumps(expected_vars) + assert mock_eval.call_args.args[0] == (os.path.join(ROOT_DIR, "xquery", "xslt_transform.xqy")) + assert mock_eval.call_args.kwargs["vars"] == json.dumps(expected_vars) @patch.dict(os.environ, {"XSLT_IMAGE_LOCATION": "imagepath"}, clear=True) def test_eval_xslt_with_query(self): - with patch.object(self.client, "eval") as mock_eval: - with patch.object( - self.client, - "user_can_view_unpublished_judgments", - return_value=True, - ): - uri = "/judgment/uri" - query = "the query string" - expected_vars: XsltTransformDict = { - "uri": "/judgment/uri.xml", - "version_uri": None, - "show_unpublished": True, - "img_location": "imagepath", - "xsl_filename": "as-handed-down.xsl", - "query": query, - } - self.client.eval_xslt( - uri, - show_unpublished=True, - xsl_filename="as-handed-down.xsl", - query=query, - ) + with patch.object(self.client, "eval") as mock_eval, patch.object( + self.client, + "user_can_view_unpublished_judgments", + return_value=True, + ): + uri = "/judgment/uri" + query = "the query string" + expected_vars: XsltTransformDict = { + "uri": "/judgment/uri.xml", + "version_uri": None, + "show_unpublished": True, + "img_location": "imagepath", + "xsl_filename": "as-handed-down.xsl", + "query": query, + } + self.client.eval_xslt( + uri, + show_unpublished=True, + xsl_filename="as-handed-down.xsl", + query=query, + ) - assert mock_eval.call_args.args[0] == (os.path.join(ROOT_DIR, "xquery", "xslt_transform.xqy")) + assert mock_eval.call_args.args[0] == (os.path.join(ROOT_DIR, "xquery", "xslt_transform.xqy")) - assert mock_eval.call_args.kwargs["vars"] == json.dumps(expected_vars) + assert mock_eval.call_args.kwargs["vars"] == json.dumps(expected_vars) diff --git a/tests/client/test_save_copy_delete_judgment.py b/tests/client/test_save_copy_delete_judgment.py index 4e126900..36465ef1 100644 --- a/tests/client/test_save_copy_delete_judgment.py +++ b/tests/client/test_save_copy_delete_judgment.py @@ -61,38 +61,39 @@ def test_save_locked_judgment_xml(self): When `Client.save_locked_judgment_xml` is called with these as arguments Then the xquery in `update_locked_judgment.xqy` is called on the Marklogic db with those arguments """ - with patch.object(caselawclient.Client, "validate_content_hash"): - with patch.object(self.client, "eval") as mock_eval: - uri = "/ewca/civ/2004/632" - judgment_str = "My updated judgment" - judgment_xml = judgment_str.encode("utf-8") - expected_vars = { - "uri": "/ewca/civ/2004/632.xml", - "judgment": judgment_str, - "annotation": json.dumps( - { - "type": "enrichment", - "calling_function": "save_locked_judgment_xml", - "calling_agent": "marklogic-api-client-test", - "automated": True, - "message": "test_save_locked_judgment_xml", - "payload": {"test_payload": True}, - }, - ), - } - self.client.save_locked_judgment_xml( - uri, - judgment_xml, - VersionAnnotation( - VersionType.ENRICHMENT, - message="test_save_locked_judgment_xml", - automated=True, - payload={"test_payload": True}, - ), - ) + with patch.object(caselawclient.Client, "validate_content_hash"), patch.object( + self.client, "eval" + ) as mock_eval: + uri = "/ewca/civ/2004/632" + judgment_str = "My updated judgment" + judgment_xml = judgment_str.encode("utf-8") + expected_vars = { + "uri": "/ewca/civ/2004/632.xml", + "judgment": judgment_str, + "annotation": json.dumps( + { + "type": "enrichment", + "calling_function": "save_locked_judgment_xml", + "calling_agent": "marklogic-api-client-test", + "automated": True, + "message": "test_save_locked_judgment_xml", + "payload": {"test_payload": True}, + }, + ), + } + self.client.save_locked_judgment_xml( + uri, + judgment_xml, + VersionAnnotation( + VersionType.ENRICHMENT, + message="test_save_locked_judgment_xml", + automated=True, + payload={"test_payload": True}, + ), + ) - assert mock_eval.call_args.args[0] == (os.path.join(ROOT_DIR, "xquery", "update_locked_judgment.xqy")) - assert mock_eval.call_args.kwargs["vars"] == json.dumps(expected_vars) + assert mock_eval.call_args.args[0] == (os.path.join(ROOT_DIR, "xquery", "update_locked_judgment.xqy")) + assert mock_eval.call_args.kwargs["vars"] == json.dumps(expected_vars) def test_save_locked_judgment_xml_checks_content_hash(self): """ diff --git a/tests/client/test_verify_show_unpublished.py b/tests/client/test_verify_show_unpublished.py index ebcac4c4..50eb7fc3 100644 --- a/tests/client/test_verify_show_unpublished.py +++ b/tests/client/test_verify_show_unpublished.py @@ -17,12 +17,11 @@ def test_hide_published_if_unauthorised_and_user_asks_for_unpublished(self): self.client, "user_can_view_unpublished_judgments", return_value=False, - ): - with patch.object(logging, "warning") as mock_logger: - result = self.client.verify_show_unpublished(True) - assert result is False - # Check the logger was called - mock_logger.assert_called() + ), patch.object(logging, "warning") as mock_logger: + result = self.client.verify_show_unpublished(True) + assert result is False + # Check the logger was called + mock_logger.assert_called() def test_show_unpublished_if_authorised_and_asks_for_unpublished(self): # User can view unpublished and is asking to view unpublished judgments diff --git a/tests/models/documents/test_documents.py b/tests/models/documents/test_documents.py index 3b5d7731..bebf3723 100644 --- a/tests/models/documents/test_documents.py +++ b/tests/models/documents/test_documents.py @@ -281,13 +281,12 @@ def test_returns_true_when_enriched_recently_is_true_and_validates_against_schem Document, "enriched_recently", new_callable=PropertyMock, - ) as mock_enriched_recently: - with patch.object( - Document, - "validates_against_schema", - new_callable=PropertyMock, - ) as mock_validates_against_schema: - mock_enriched_recently.return_value = enriched_recently - mock_validates_against_schema.return_value = validates_against_schema - - assert document.can_enrich is can_enrich + ) as mock_enriched_recently, patch.object( + Document, + "validates_against_schema", + new_callable=PropertyMock, + ) as mock_validates_against_schema: + mock_enriched_recently.return_value = enriched_recently + mock_validates_against_schema.return_value = validates_against_schema + + assert document.can_enrich is can_enrich From a6bfa681d99ef64067327338ddff27c3263ff2a9 Mon Sep 17 00:00:00 2001 From: Nick Jackson Date: Thu, 26 Sep 2024 11:48:09 +0100 Subject: [PATCH 3/4] style(FCL-340): replace if/else blocks with ternary operations --- src/caselawclient/Client.py | 5 +---- src/caselawclient/models/utilities/dates.py | 6 +----- tests/factories.py | 10 ++-------- 3 files changed, 4 insertions(+), 17 deletions(-) diff --git a/src/caselawclient/Client.py b/src/caselawclient/Client.py index 8251c104..b2d31a87 100644 --- a/src/caselawclient/Client.py +++ b/src/caselawclient/Client.py @@ -800,10 +800,7 @@ def eval_xslt( else None ) - if os.getenv("XSLT_IMAGE_LOCATION"): - image_location = os.getenv("XSLT_IMAGE_LOCATION") - else: - image_location = "" + image_location = os.getenv("XSLT_IMAGE_LOCATION", "") show_unpublished = self.verify_show_unpublished(show_unpublished) diff --git a/src/caselawclient/models/utilities/dates.py b/src/caselawclient/models/utilities/dates.py index e94b518b..2b5f6453 100644 --- a/src/caselawclient/models/utilities/dates.py +++ b/src/caselawclient/models/utilities/dates.py @@ -9,11 +9,7 @@ def parse_string_date_as_utc(iso_string: str, timezone: tzinfo.BaseTzInfo) -> da ensure that it is converted to a UTC-aware datetime""" mixed_date = isoparse(iso_string) - if not mixed_date.tzinfo: - # it is an unaware time - aware_date = timezone.localize(mixed_date) - else: - aware_date = mixed_date + aware_date = mixed_date if mixed_date.tzinfo else timezone.localize(mixed_date) # make UTC utc_date = aware_date.astimezone(UTC) diff --git a/tests/factories.py b/tests/factories.py index 644cfaec..9943cb53 100644 --- a/tests/factories.py +++ b/tests/factories.py @@ -32,10 +32,7 @@ def build(cls, **kwargs) -> DocumentBody: ) for map_to, map_from in cls.PARAMS_MAP.items(): - if map_from[0] in kwargs: - value = kwargs[map_from[0]] - else: - value = map_from[1] + value = kwargs[map_from[0]] if map_from[0] in kwargs else map_from[1] setattr(document_mock.return_value, map_to, value) return document_mock() @@ -74,10 +71,7 @@ def build(cls, **kwargs) -> Judgment: judgment_mock.return_value.body = DocumentBodyFactory().build() for map_to, map_from in cls.PARAMS_MAP.items(): - if map_from[0] in kwargs: - value = kwargs[map_from[0]] - else: - value = map_from[1] + value = kwargs[map_from[0]] if map_from[0] in kwargs else map_from[1] setattr(judgment_mock.return_value, map_to, value) return judgment_mock() From bf910f9a4c0ebf78dd26b867a7a15150e04c254e Mon Sep 17 00:00:00 2001 From: Nick Jackson Date: Thu, 26 Sep 2024 11:51:43 +0100 Subject: [PATCH 4/4] style(FCL-331): where possible, return conditional statements directly --- .../models/documents/__init__.py | 26 +++++-------------- src/caselawclient/models/documents/body.py | 4 +-- .../models/neutral_citation_mixin.py | 10 ++----- 3 files changed, 10 insertions(+), 30 deletions(-) diff --git a/src/caselawclient/models/documents/__init__.py b/src/caselawclient/models/documents/__init__.py index 276d2b6e..077663d0 100644 --- a/src/caselawclient/models/documents/__init__.py +++ b/src/caselawclient/models/documents/__init__.py @@ -281,22 +281,15 @@ def is_failure(self) -> bool: :return: `True` if this document is in a 'failure' state, otherwise `False` """ - if self.body.failed_to_parse: - return True - return False + return self.body.failed_to_parse @cached_property def is_parked(self) -> bool: - if "parked" in self.uri: - return True - return False + return "parked" in self.uri @cached_property def has_name(self) -> bool: - if not self.body.name: - return False - - return True + return bool(self.body.name) @cached_property def has_valid_court(self) -> bool: @@ -373,9 +366,7 @@ def can_enrich(self) -> bool: """ Is it sensible to enrich this document? """ - if (self.enriched_recently is False) and self.validates_against_schema: - return True - return False + return (self.enriched_recently is False) and self.validates_against_schema @cached_property def enriched_recently(self) -> bool: @@ -388,9 +379,8 @@ def enriched_recently(self) -> bool: return False now = datetime.datetime.now(tz=datetime.timezone.utc) - if now - last_enrichment < MINIMUM_ENRICHMENT_TIME: - return True - return False + + return now - last_enrichment < MINIMUM_ENRICHMENT_TIME @cached_property def validates_against_schema(self) -> bool: @@ -506,6 +496,4 @@ def can_reparse(self) -> bool: """ Is it sensible to reparse this document? """ - if self.docx_exists(): - return True - return False + return self.docx_exists() diff --git a/src/caselawclient/models/documents/body.py b/src/caselawclient/models/documents/body.py index 8aa73a4c..9c7fb7bf 100644 --- a/src/caselawclient/models/documents/body.py +++ b/src/caselawclient/models/documents/body.py @@ -137,6 +137,4 @@ def failed_to_parse(self) -> bool: :return: `True` if there was a complete parser failure, otherwise `False` """ - if "error" in self._xml.root_element: - return True - return False + return "error" in self._xml.root_element diff --git a/src/caselawclient/models/neutral_citation_mixin.py b/src/caselawclient/models/neutral_citation_mixin.py index 4dd93035..bea5e851 100644 --- a/src/caselawclient/models/neutral_citation_mixin.py +++ b/src/caselawclient/models/neutral_citation_mixin.py @@ -40,14 +40,8 @@ def neutral_citation(self) -> str: @cached_property def has_ncn(self) -> bool: - if not self.neutral_citation: - return False - - return True + return bool(self.neutral_citation) @cached_property def has_valid_ncn(self) -> bool: - if not self.has_ncn or not neutral_url(self.neutral_citation): - return False - - return True + return self.has_ncn and neutral_url(self.neutral_citation) is not None