From c64b5bb0fcdcfa3a302aff71661a5feca50694dc Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Sun, 24 Dec 2023 12:57:16 +0100 Subject: [PATCH] Remove deprecated parameters --- pypdf/_merger.py | 75 ++----------------- pypdf/_page.py | 27 ------- pypdf/_writer.py | 168 ++----------------------------------------- tests/test_writer.py | 39 +++------- 4 files changed, 20 insertions(+), 289 deletions(-) diff --git a/pypdf/_merger.py b/pypdf/_merger.py index 8937cde33..d3db966ab 100644 --- a/pypdf/_merger.py +++ b/pypdf/_merger.py @@ -25,7 +25,6 @@ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. -import warnings from io import BytesIO, FileIO, IOBase from pathlib import Path from types import TracebackType @@ -124,12 +123,11 @@ def __exit__( @deprecation_bookmark(bookmark="outline_item", import_bookmarks="import_outline") def merge( self, - page_number: Optional[int] = None, - fileobj: Union[None, Path, StrByteType, PdfReader] = None, + page_number: int, + fileobj: Union[Path, StrByteType, PdfReader], outline_item: Optional[str] = None, pages: Optional[PageRangeSpec] = None, import_outline: bool = True, - position: Optional[int] = None, # deprecated ) -> None: """ Merge the pages from the given file into the output file at the @@ -154,33 +152,6 @@ def merge( outline (collection of outline items, previously referred to as 'bookmarks') from being imported by specifying this as ``False``. """ - if position is not None: # deprecated - if page_number is None: - page_number = position - old_term = "position" - new_term = "page_number" - warnings.warn( - ( - f"{old_term} is deprecated as an argument and will be " - f"removed in pypdf=4.0.0. Use {new_term} instead" - ), - DeprecationWarning, - ) - else: - raise ValueError( - "The argument position of merge is deprecated. " - "Use page_number only." - ) - - if page_number is None: # deprecated - # The parameter is only marked as Optional as long as - # position is not fully deprecated - raise ValueError("page_number may not be None") - if fileobj is None: # deprecated - # The argument is only Optional due to the deprecated position - # argument - raise ValueError("fileobj may not be None") - stream, encryption_obj = self._create_stream(fileobj) # Create a new PdfReader instance using the stream @@ -641,13 +612,12 @@ def find_bookmark( def add_outline_item( self, title: str, - page_number: Optional[int] = None, + page_number: int, parent: Union[None, TreeObject, IndirectObject] = None, color: Optional[Tuple[float, float, float]] = None, bold: bool = False, italic: bool = False, fit: Fit = PAGE_FIT, - pagenum: Optional[int] = None, # deprecated ) -> IndirectObject: """ Add an outline item (commonly referred to as a "Bookmark") to this PDF file. @@ -663,24 +633,6 @@ def add_outline_item( italic: Outline item font is italic fit: The fit of the destination page. """ - if page_number is not None and pagenum is not None: - raise ValueError( - "The argument pagenum of add_outline_item is deprecated. " - "Use page_number only." - ) - if pagenum is not None: # deprecated - old_term = "pagenum" - new_term = "page_number" - warnings.warn( - ( - f"{old_term} is deprecated as an argument and will be " - f"removed in pypdf==4.0.0. Use {new_term} instead" - ), - DeprecationWarning, - ) - page_number = pagenum - if page_number is None: # deprecated - raise ValueError("page_number may not be None") writer = self.output if writer is None: raise RuntimeError(ERR_CLOSED_WRITER) @@ -698,8 +650,7 @@ def add_outline_item( def add_named_destination( self, title: str, - page_number: Optional[int] = None, - pagenum: Optional[int] = None, + page_number: int, ) -> None: """ Add a destination to the output. @@ -708,24 +659,6 @@ def add_named_destination( title: Title to use page_number: Page number this destination points at. """ - if page_number is not None and pagenum is not None: - raise ValueError( - "The argument pagenum of add_named_destination is deprecated. " - "Use page_number only." - ) - if pagenum is not None: # deprecated - old_term = "pagenum" - new_term = "page_number" - warnings.warn( - ( - f"{old_term} is deprecated as an argument and will be " - f"removed in pypdf==4.0.0. Use {new_term} instead" - ), - DeprecationWarning, - ) - page_number = pagenum - if page_number is None: # deprecated - raise ValueError("page_number may not be None") dest = Destination( TextStringObject(title), NumberObject(page_number), diff --git a/pypdf/_page.py b/pypdf/_page.py index b0a3ab71c..fe319b779 100644 --- a/pypdf/_page.py +++ b/pypdf/_page.py @@ -322,41 +322,14 @@ def __init__( self, pdf: Union[None, PdfReaderProtocol, PdfWriterProtocol] = None, indirect_reference: Optional[IndirectObject] = None, - indirect_ref: Optional[IndirectObject] = None, # deprecated ) -> None: DictionaryObject.__init__(self) self.pdf: Union[None, PdfReaderProtocol, PdfWriterProtocol] = pdf self.inline_images: Optional[Dict[str, ImageFile]] = None # below Union for mypy but actually Optional[List[str]] self.inline_images_keys: Optional[List[Union[str, List[str]]]] = None - if indirect_ref is not None: # deprecated - warnings.warn( - ( - "indirect_ref is deprecated and will be removed in " - "pypdf 4.0.0. Use indirect_reference instead of indirect_ref." - ), - DeprecationWarning, - ) - if indirect_reference is not None: - raise ValueError("Use indirect_reference instead of indirect_ref.") - indirect_reference = indirect_ref self.indirect_reference = indirect_reference - @property - def indirect_ref(self) -> Optional[IndirectObject]: # deprecated - warnings.warn( - ( - "indirect_ref is deprecated and will be removed in pypdf 4.0.0" - "Use indirect_reference instead of indirect_ref." - ), - DeprecationWarning, - ) - return self.indirect_reference - - @indirect_ref.setter - def indirect_ref(self, value: Optional[IndirectObject]) -> None: # deprecated - self.indirect_reference = value - def hash_value_data(self) -> bytes: data = super().hash_value_data() data += b"%d" % id(self) diff --git a/pypdf/_writer.py b/pypdf/_writer.py index 263cbfabd..bc1b47363 100644 --- a/pypdf/_writer.py +++ b/pypdf/_writer.py @@ -34,7 +34,6 @@ import hashlib import re import uuid -import warnings from io import BytesIO, FileIO, IOBase from pathlib import Path from types import TracebackType @@ -261,25 +260,8 @@ def _add_object(self, obj: PdfObject) -> IndirectObject: def get_object( self, - indirect_reference: Union[None, int, IndirectObject] = None, - ido: Optional[IndirectObject] = None, + indirect_reference: Union[int, IndirectObject], ) -> PdfObject: - if ido is not None: # deprecated - if indirect_reference is not None: - raise ValueError( - "Please only set 'indirect_reference'. The 'ido' argument " - "is deprecated." - ) - else: - indirect_reference = ido - warnings.warn( - "The parameter 'ido' is depreciated and will be removed in " - "pypdf 4.0.0.", - DeprecationWarning, - ) - assert ( - indirect_reference is not None - ) # the None value is only there to keep the deprecated name if isinstance(indirect_reference, int): return self._objects[indirect_reference - 1] if indirect_reference.pdf != self: @@ -429,9 +411,7 @@ def insert_page( """ return self._add_page(page, lambda kids, p: kids.insert(index, p)) - def get_page( - self, page_number: Optional[int] = None, pageNumber: Optional[int] = None - ) -> PageObject: + def get_page(self, page_number: int) -> PageObject: """ Retrieve a page by number from this PDF file. @@ -442,15 +422,6 @@ def get_page( Returns: The page at the index given by *page_number* """ - if pageNumber is not None: # deprecated - if page_number is not None: - raise ValueError("Please only use the page_number parameter") - deprecate_with_replacement( - "get_page(pageNumber)", "get_page(page_number)", "4.0.0" - ) - page_number = pageNumber - if page_number is None and pageNumber is None: # deprecated - raise ValueError("Please specify the page_number") pages = cast(Dict[str, Any], self.get_object(self._pages)) # TODO: crude hack return cast(PageObject, pages[PA.KIDS][page_number].get_object()) @@ -1097,12 +1068,10 @@ def generate_file_identifiers(self) -> None: def encrypt( self, - user_password: Optional[str] = None, + user_password: str, owner_password: Optional[str] = None, use_128bit: bool = True, permissions_flag: UserAccessPermissions = ALL_DOCUMENT_PERMISSIONS, - user_pwd: Optional[str] = None, # deprecated - owner_pwd: Optional[str] = None, # deprecated *, algorithm: Optional[str] = None, ) -> None: @@ -1129,41 +1098,6 @@ def encrypt( "AES-128", "AES-256-R5", "AES-256". If it's valid, `use_128bit` will be ignored. """ - if user_pwd is not None: - if user_password is not None: - raise ValueError( - "Please only set 'user_password'. " - "The 'user_pwd' argument is deprecated." - ) - else: - warnings.warn( - "Please use 'user_password' instead of 'user_pwd'. " - "The 'user_pwd' argument is deprecated and " - "will be removed in pypdf 4.0.0." - ) - user_password = user_pwd - if user_password is None: # deprecated - # user_password is only Optional for due to the deprecated user_pwd - raise ValueError("user_password may not be None") - - if owner_pwd is not None: # deprecated - if owner_password is not None: - raise ValueError( - "The argument owner_pwd of encrypt is deprecated. " - "Use owner_password only." - ) - else: - old_term = "owner_pwd" - new_term = "owner_password" - warnings.warn( - message=( - f"{old_term} is deprecated as an argument and will be " - f"removed in pypdf 4.0.0. Use {new_term} instead" - ), - category=DeprecationWarning, - ) - owner_password = owner_pwd - if owner_password is None: owner_password = user_password @@ -1546,32 +1480,11 @@ def get_named_dest_root(self) -> ArrayObject: def add_outline_item_destination( self, - page_destination: Union[None, IndirectObject, PageObject, TreeObject] = None, + page_destination: Union[IndirectObject, PageObject, TreeObject], parent: Union[None, TreeObject, IndirectObject] = None, before: Union[None, TreeObject, IndirectObject] = None, is_open: bool = True, - dest: Union[None, PageObject, TreeObject] = None, # deprecated ) -> IndirectObject: - if page_destination is not None and dest is not None: # deprecated - raise ValueError( - "The argument dest of add_outline_item_destination is " - "deprecated. Use page_destination only." - ) - if dest is not None: # deprecated - old_term = "dest" - new_term = "page_destination" - warnings.warn( - message=( - f"{old_term} is deprecated as an argument and will be " - f"removed in pypdf 4.0.0. Use {new_term} instead" - ), - category=DeprecationWarning, - ) - page_destination = dest - if page_destination is None: # deprecated - # argument is only Optional due to deprecated argument. - raise ValueError("page_destination may not be None") - page_destination = cast(PageObject, page_destination.get_object()) if isinstance(page_destination, PageObject): return self.add_outline_item_destination( @@ -1637,7 +1550,6 @@ def add_outline_item( italic: bool = False, fit: Fit = PAGE_FIT, is_open: bool = True, - pagenum: Optional[int] = None, # deprecated ) -> IndirectObject: """ Add an outline item (commonly referred to as a "Bookmark") to the PDF file. @@ -1664,11 +1576,6 @@ def add_outline_item( return self.add_outline_item( title, page_number, parent, None, before, color, bold, italic, is_open=is_open # type: ignore ) - if page_number is not None and pagenum is not None: - raise ValueError( - "The argument pagenum of add_outline_item is deprecated. " - "Use page_number only." - ) if page_number is None: action_ref = None else: @@ -1731,28 +1638,8 @@ def add_named_destination_array( def add_named_destination_object( self, - page_destination: Optional[PdfObject] = None, - dest: Optional[PdfObject] = None, + page_destination: PdfObject, ) -> IndirectObject: - if page_destination is not None and dest is not None: - raise ValueError( - "The argument dest of add_named_destination_object is " - "deprecated. Use page_destination only." - ) - if dest is not None: # deprecated - old_term = "dest" - new_term = "page_destination" - warnings.warn( - message=( - f"{old_term} is deprecated as an argument and will be " - f"removed in pypdf 4.0.0. Use {new_term} instead" - ), - category=DeprecationWarning, - ) - page_destination = dest - if page_destination is None: # deprecated - raise ValueError("page_destination may not be None") - page_destination_ref = self._add_object(page_destination.dest_array) # type: ignore self.add_named_destination_array( cast("TextStringObject", page_destination["/Title"]), page_destination_ref # type: ignore @@ -1763,27 +1650,8 @@ def add_named_destination_object( def add_named_destination( self, title: str, - page_number: Optional[int] = None, - pagenum: Optional[int] = None, # deprecated + page_number: int, ) -> IndirectObject: - if page_number is not None and pagenum is not None: - raise ValueError( - "The argument pagenum of add_outline_item is deprecated. " - "Use page_number only." - ) - if pagenum is not None: - old_term = "pagenum" - new_term = "page_number" - warnings.warn( - message=( - f"{old_term} is deprecated as an argument and will be " - f"removed in pypdf 4.0.0. Use {new_term} instead" - ), - category=DeprecationWarning, - ) - page_number = pagenum - if page_number is None: - raise ValueError("page_number may not be None") page_ref = self.get_object(self._pages)[PA.KIDS][page_number] # type: ignore dest = DictionaryObject() dest.update( @@ -1976,7 +1844,6 @@ def clean_forms( def remove_images( self, to_delete: ImageType = ImageType.ALL, - ignore_byte_string_object: Optional[bool] = None, ) -> None: """ Remove images from this output. @@ -1987,14 +1854,7 @@ def remove_images( ignore_byte_string_object: deprecated """ if isinstance(to_delete, bool): - ignore_byte_string_object = to_delete to_delete = ImageType.ALL - if ignore_byte_string_object is not None: - warnings.warn( - "The 'ignore_byte_string_object' argument of remove_images is " - "deprecated and will be removed in pypdf 4.0.0.", - category=DeprecationWarning, - ) i = ( ( ObjectDeletionFlag.XOBJECT_IMAGES @@ -2015,19 +1875,13 @@ def remove_images( for page in self.pages: self.remove_objects_from_page(page, i) - def remove_text(self, ignore_byte_string_object: Optional[bool] = None) -> None: + def remove_text(self) -> None: """ Remove text from this output. Args: ignore_byte_string_object: deprecated """ - if ignore_byte_string_object is not None: - warnings.warn( - "The 'ignore_byte_string_object' argument of remove_images is " - "deprecated and will be removed in pypdf 4.0.0.", - category=DeprecationWarning, - ) for page in self.pages: self.remove_objects_from_page(page, ObjectDeletionFlag.TEXT) @@ -2037,7 +1891,6 @@ def add_uri( uri: str, rect: RectangleObject, border: Optional[ArrayObject] = None, - pagenum: Optional[int] = None, ) -> None: """ Add an URI from a rectangular area to the specified page. @@ -2055,13 +1908,6 @@ def add_uri( properties. See the PDF spec for details. No border will be drawn if this argument is omitted. """ - if pagenum is not None: - warnings.warn( - "The 'pagenum' argument of add_uri is deprecated and will be " - "removed in pypdf 4.0.0. Use 'page_number' instead.", - category=DeprecationWarning, - ) - page_number = pagenum page_link = self.get_object(self._pages)[PA.KIDS][page_number] # type: ignore page_ref = cast(Dict[str, Any], self.get_object(page_link)) diff --git a/tests/test_writer.py b/tests/test_writer.py index 281232c4b..a74d7825b 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -179,12 +179,7 @@ def writer_operate(writer: PdfWriter) -> None: ) writer.add_blank_page() writer.add_uri(2, "https://example.com", RectangleObject([0, 0, 100, 100])) - with pytest.warns( - DeprecationWarning, match="'pagenum' argument of add_uri is deprecated" - ): - writer.add_uri( - 2, "https://example.com", RectangleObject([0, 0, 100, 100]), pagenum=2 - ) + writer.add_uri(2, "https://example.com", RectangleObject([0, 0, 100, 100])) with pytest.raises(DeprecationError): writer.add_link(2, 1, RectangleObject([0, 0, 100, 100])) assert writer._get_page_layout() is None @@ -522,20 +517,11 @@ def test_encrypt(use_128bit, user_password, owner_password, pdf_file_path): writer.add_page(page) - with pytest.raises(ValueError, match="owner_pwd of encrypt is deprecated."): - writer.encrypt( - owner_pwd=user_password, - owner_password=owner_password, - user_password=user_password, - use_128bit=use_128bit, - ) - with pytest.raises(ValueError, match="'user_pwd' argument is deprecated"): - writer.encrypt( - owner_password=owner_password, - user_password=user_password, - user_pwd=user_password, - use_128bit=use_128bit, - ) + writer.encrypt( + owner_password=owner_password, + user_password=user_password, + use_128bit=use_128bit, + ) writer.encrypt( user_password=user_password, owner_password=owner_password, @@ -641,14 +627,8 @@ def test_add_named_destination(pdf_file_path): writer.add_named_destination(TextStringObject("A named dest"), 2) writer.add_named_destination(TextStringObject("A named dest2"), 2) - - with pytest.warns(DeprecationWarning, match="pagenum is deprecated as an argument"): - writer.add_named_destination(TextStringObject("A named dest3"), pagenum=2) - - with pytest.raises(ValueError): - writer.add_named_destination( - TextStringObject("A named dest3"), pagenum=2, page_number=2 - ) + writer.add_named_destination(TextStringObject("A named dest3"), page_number=2) + writer.add_named_destination(TextStringObject("A named dest3"), page_number=2) root = writer.get_named_dest_root() assert root[0] == "A named dest" @@ -1872,8 +1852,7 @@ def test_remove_image_per_type(): for x in (b"BI", b"ID", b"EI") ) - with pytest.raises(DeprecationWarning): - writer.remove_images(True) + writer.remove_images() writer = PdfWriter(clone_from=RESOURCE_ROOT / "GeoBase_NHNC1_Data_Model_UML_EN.pdf") writer.remove_images(ImageType.DRAWING_IMAGES)