From cd17d520ea137209eabea460d698fb326545217f Mon Sep 17 00:00:00 2001 From: David McKee Date: Wed, 23 Oct 2024 11:41:08 +0100 Subject: [PATCH] Erroneous doc.body attribute calls on body work with a warning --- CHANGELOG.md | 4 ++++ .../models/documents/__init__.py | 7 ++++++ tests/models/documents/test_documents.py | 23 +++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0e5889d..bc7fc505 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog 1.0.0]. +## v27.0.2 (unreleased) + +- Allow things on doc.body to be called from doc with a warning + ## v27.0.1 (2024-10-17) - Fix `.content_as_html` on Document Factory diff --git a/src/caselawclient/models/documents/__init__.py b/src/caselawclient/models/documents/__init__.py index 0daa52b4..d30d6b24 100644 --- a/src/caselawclient/models/documents/__init__.py +++ b/src/caselawclient/models/documents/__init__.py @@ -495,3 +495,10 @@ def can_reparse(self) -> bool: Is it sensible to reparse this document? """ return self.docx_exists() + + def __getattr__(self, name: str) -> Any: + warnings.warn(f"{name} no longer exists on Document, using Document.body instead", DeprecationWarning) + try: + return getattr(self.body, name) + except Exception: + raise AttributeError(f"Neither 'Document' nor 'DocumentBody' objects have an attribute '{name}'") diff --git a/tests/models/documents/test_documents.py b/tests/models/documents/test_documents.py index bebf3723..b2e254c7 100644 --- a/tests/models/documents/test_documents.py +++ b/tests/models/documents/test_documents.py @@ -1,4 +1,5 @@ import datetime +import warnings from unittest.mock import PropertyMock, patch import pytest @@ -8,6 +9,7 @@ NotSupportedOnVersion, OnlySupportedOnVersion, ) +from caselawclient.factories import DocumentBodyFactory, DocumentFactory from caselawclient.models.documents import ( DOCUMENT_STATUS_HOLD, DOCUMENT_STATUS_IN_PROGRESS, @@ -290,3 +292,24 @@ def test_returns_true_when_enriched_recently_is_true_and_validates_against_schem mock_validates_against_schema.return_value = validates_against_schema assert document.can_enrich is can_enrich + + class TestMethodMissing: + def test_attribute_on_body(self, mock_api_client): + doc = DocumentFactory.build(uri="test/1234", body=DocumentBodyFactory.build(name="docname")) + with pytest.deprecated_call(): + name = doc.name + + assert name == "docname" + + def test_real_attribute(self, mock_api_client): + doc = DocumentFactory.build(uri="test/1234", body=DocumentBodyFactory.build(name="docname")) + with warnings.catch_warnings(): + identifier = doc.best_human_identifier + assert identifier is None + + def test_absent_item(self, mock_api_client): + doc = DocumentFactory.build(uri="test/1234", body=DocumentBodyFactory.build(name="docname")) + with pytest.raises( + AttributeError, match="Neither 'Document' nor 'DocumentBody' objects have an attribute 'x'" + ): + doc.x