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

Basic usage triggers deprecation warning #886

Closed
MasterOdin opened this issue May 22, 2022 · 3 comments · Fixed by #887
Closed

Basic usage triggers deprecation warning #886

MasterOdin opened this issue May 22, 2022 · 3 comments · Fixed by #887
Assignees
Labels
is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF

Comments

@MasterOdin
Copy link
Member

Importing anything from PyPDF2 now causes a deprecation warning to be raised as a result of #867. The root cause is that as part of importing PyPDF2.pagerange.PageRange a PageRange object is created, which calls isString. Here is a stack trace:

>>> from PyPDF2 import PdfReader
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mpeveler/code/github/PyPDF2/PyPDF2/__init__.py", line 4, in <module>
    from PyPDF2.merger import PdfFileMerger, PdfMerger
  File "/Users/mpeveler/code/github/PyPDF2/PyPDF2/merger.py", line 35, in <module>
    from PyPDF2.pagerange import PageRange
  File "/Users/mpeveler/code/github/PyPDF2/PyPDF2/pagerange.py", line 128, in <module>
    PAGE_RANGE_ALL = PageRange(":")  # The range of all pages.
  File "/Users/mpeveler/code/github/PyPDF2/PyPDF2/pagerange.py", line 74, in __init__
    m = isString(arg) and re.match(PAGE_RANGE_RE, arg)

There is a number of other places where isString (and probably some other deprecated utils functions) is called as part of regular usage (e.g. in PdfReader constructor). To avoid these warnings as part of regular usage, I would suggest splitting each of these util functions in two as follows (using isString as an example):

def _isString(s):
    return isinstance(s, _basestring)

def isString(s):
    """Test if arg is a string. Compatible with Python 2 and 3."""
    warnings.warn(DEPR_MSG_NO_REPLACEMENT.format("isString"))
    return _isString(s)

and change all internal usages of the function to _isString while leaving the user facing isString in place for downstream consumers to see the expected deprecation warning.

Environment

Which environment were you using when you encountered the problem?

$ python -m platform
macOS-12.4-arm64-arm-64bit

$ python -c "import PyPDF2;print(PyPDF2.__version__)"
/Users/mpeveler/code/github/PyPDF2/PyPDF2/_utils.py:72: UserWarning: isString is deprecated and will be removed in PyPDF2 2.0.0.
  warnings.warn(DEPR_MSG_NO_REPLACEMENT.format("isString"))
1.28.0

Code

>>> from PyPDF2 import PdfReader
/Users/mpeveler/code/github/PyPDF2/PyPDF2/_utils.py:72: UserWarning: isString is deprecated and will be removed in PyPDF2 2.0.0.
  warnings.warn(DEPR_MSG_NO_REPLACEMENT.format("isString"))
@MasterOdin MasterOdin added the is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF label May 22, 2022
@MasterOdin
Copy link
Member Author

Happy to put together a PR for this, but wanted to get confirmation on the approach before beginning.

@MasterOdin
Copy link
Member Author

Some other basic usage triggering these deprecation warnings:

>>> pageObject = a.pages[0]
UserWarning: ConvertFunctionsToVirtualList is deprecated and will be removed in PyPDF2 2.0.0. [_utils.py:171]
UserWarning: isInt is deprecated and will be removed in PyPDF2 2.0.0. [_utils.py:78]

@MartinThoma
Copy link
Member

Thank you for all of the information and testing it so quickly! Yes, splitting the function isString into isString + one that does not trigger a warning is an excellent idea 👍 (although I would prefer _is_string - but I'd accept either version in a PR 😄 ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants