Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix deprecation for Ressources when using old constants #2705

Merged
merged 8 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions pypdf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,57 @@ def _human_readable_bytes(bytes: int) -> str:
return f"{bytes / 10**9:.1f} GB"


# The following class has been copied from Django:
# https://github.com/django/django/blob/adae619426b6f50046b3daaa744db52989c9d6db/django/utils/functional.py#L51-L65
#
# Original license:
#
# ---------------------------------------------------------------------------------
# Copyright (c) Django Software Foundation and individual contributors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of Django nor the names of its contributors may be used
# to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ---------------------------------------------------------------------------------
class classproperty: # noqa: N801
"""
Decorator that converts a method with a single cls argument into a property
that can be accessed directly from the class.
"""

def __init__(self, method=None): # type: ignore # noqa: ANN001
self.fget = method

def __get__(self, instance, cls=None) -> Any: # type: ignore # noqa: ANN001
return self.fget(cls)

def getter(self, method): # type: ignore # noqa: ANN001, ANN202
self.fget = method
return self


@dataclass
class File:
from .generic import IndirectObject
Expand Down
42 changes: 17 additions & 25 deletions pypdf/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from enum import IntFlag, auto
from typing import Dict, Tuple

from ._utils import deprecate_with_replacement
from ._utils import classproperty, deprecate_with_replacement


class Core:
Expand Down Expand Up @@ -161,51 +161,43 @@ class Ressources: # deprecated
.. deprecated:: 5.0.0
"""

@classmethod # type: ignore
@property
def EXT_G_STATE(cls) -> str:
@classproperty
def EXT_G_STATE(cls) -> str: # noqa: N805
deprecate_with_replacement("Ressources", "Resources", "5.0.0")
return "/ExtGState" # dictionary, optional

@classmethod # type: ignore
@property
def COLOR_SPACE(cls) -> str:
@classproperty
def COLOR_SPACE(cls) -> str: # noqa: N805
deprecate_with_replacement("Ressources", "Resources", "5.0.0")
return "/ColorSpace" # dictionary, optional

@classmethod # type: ignore
@property
def PATTERN(cls) -> str:
@classproperty
def PATTERN(cls) -> str: # noqa: N805
deprecate_with_replacement("Ressources", "Resources", "5.0.0")
return "/Pattern" # dictionary, optional

@classmethod # type: ignore
@property
def SHADING(cls) -> str:
@classproperty
def SHADING(cls) -> str: # noqa: N805
deprecate_with_replacement("Ressources", "Resources", "5.0.0")
return "/Shading" # dictionary, optional

@classmethod # type: ignore
@property
def XOBJECT(cls) -> str:
@classproperty
def XOBJECT(cls) -> str: # noqa: N805
deprecate_with_replacement("Ressources", "Resources", "5.0.0")
return "/XObject" # dictionary, optional

@classmethod # type: ignore
@property
def FONT(cls) -> str:
@classproperty
def FONT(cls) -> str: # noqa: N805
deprecate_with_replacement("Ressources", "Resources", "5.0.0")
return "/Font" # dictionary, optional

@classmethod # type: ignore
@property
def PROC_SET(cls) -> str:
@classproperty
def PROC_SET(cls) -> str: # noqa: N805
deprecate_with_replacement("Ressources", "Resources", "5.0.0")
return "/ProcSet" # array, optional

@classmethod # type: ignore
@property
def PROPERTIES(cls) -> str:
@classproperty
def PROPERTIES(cls) -> str: # noqa: N805
deprecate_with_replacement("Ressources", "Resources", "5.0.0")
return "/Properties" # dictionary, optional

Expand Down
27 changes: 27 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
_get_max_pdf_version_header,
_human_readable_bytes,
check_if_whitespace_only,
classproperty,
deprecate_with_replacement,
deprecation_no_replacement,
mark_location,
Expand Down Expand Up @@ -424,3 +425,29 @@ def test_version_compare_lt_str():

def test_bad_version():
assert Version("a").components == [(0, "a")]


def test_classproperty():
class Container:
@classproperty
def value1(cls) -> int: # noqa: N805
return 42

@classproperty
def value2(cls) -> int: # noqa: N805
return 1337

@classproperty
def value3(cls) -> int: # noqa: N805
return 1

@value3.getter
def value3(cls) -> int: # noqa: N805
return 2

assert Container.value1 == 42
assert Container.value2 == 1337
assert Container.value3 == 2
assert Container().value1 == 42
assert Container().value2 == 1337
assert Container().value3 == 2
Loading