MAINT: Deduplicate CatalogAttributes and CatalogDictionary#3868
Conversation
Currently these classes represent the same thing. To simplify, CatalogDictionary is deprecated with replacement CatalogAttributes.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3868 +/- ##
==========================================
- Coverage 97.85% 97.85% -0.01%
==========================================
Files 57 57
Lines 10702 10701 -1
Branches 2001 2001
==========================================
- Hits 10472 10471 -1
Misses 127 127
Partials 103 103 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Closes #3866. |
|
Why did you decide to use the name of the functionally less complex class? |
In constants.py, So went with that. Later noticed that |
| match=r"^CatalogDictionary is deprecated and will be removed in pypdf 7\.0\.0\. " | ||
| r"Use CatalogAttributes instead\.$", | ||
| ): | ||
| CatalogDictionary() |
There was a problem hiding this comment.
I hope nobody will be instantiating the constants classes. CatalogDictionary.NAMES will not trigger the deprecation at the moment and thus this needs further fixing.
There was a problem hiding this comment.
I will do this, but unsure how.
There was a problem hiding this comment.
We previously did this for single keys by implementing corresponding class properties, but this is most likely not feasible with this amount of keys.
I guess we could overwrite __getattr__ to issue warnings accordingly.
There was a problem hiding this comment.
@stefan6419846 with the deprecation CatalogDictionary does not exist, so cannot be added to:
CatalogDictionary = __create_old_class_instance
def _external_getattr(self, name):
deprecate_with_replacement("CatalogDictionary", "CatalogAttributes", "7.0.0")
return getattr(CatalogAttributes, name)
CatalogDictionary .__getattr__ = _external_getattr
What am I misunerstanding?
There was a problem hiding this comment.
You have to keep it a class and overwrite all relevant attributes.
There was a problem hiding this comment.
class CatalogDictionary:
"""§7.7.2 of the 1.7 and 2.0 references."""
def __init__(self) -> None:
deprecate_with_replacement("CatalogDictionary", "CatalogAttributes", "7.0.0")
def __getattribute__(self, item: str) -> str:
deprecate_with_replacement("CatalogDictionary", "CatalogAttributes", "7.0.0")
return getattr(CatalogAttributes, item)
with pytest.warns(
DeprecationWarning,
match=r"^CatalogDictionary is deprecated and will be removed in pypdf 7\.0\.0\. "
r"Use CatalogAttributes instead\.$",
):
CatalogDictionary.TYPE
FAILED tests/test_constants.py::test_catalog_dictionary - Failed: DID NOT WARN. No warnings of type (<class 'DeprecationWarning'>,) were emitted.
What is the flaw in the class that is not giving a warning in the test?
__getattr__ and __getattribute__ gives the same error, and I will change back to __getattr__.
There was a problem hiding this comment.
__getattr__ might only trigger on class instances, not on direct attribute access. It seems this either needs a ton of classproperty definitions or some more magic.
Co-authored-by: Stefan <96178532+stefan6419846@users.noreply.github.com>
Currently these classes represent the same thing. To simplify,
CatalogDictionary is deprecated with replacement CatalogAttributes.