Skip to content

[BUG] python-telegram-bot-raw deprecation warning appears incorrectly if compiled with Nuitka #4296

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

Closed
laggykiller opened this issue Jun 10, 2024 · 4 comments · Fixed by #4288

Comments

@laggykiller
Copy link

laggykiller commented Jun 10, 2024

Steps to Reproduce

  1. pip install python-telegram-bot nuitka
  2. Create minimal python file main.py (The actual content does not matter as long as telegram is imported and used)
from telegram.ext import ApplicationBuilder

app = ApplicationBuilder()
  1. Run main.py. Note that deprecation warning is not appearing (Which is correct behaviour)
  2. Compile with nuitka by python -m nuitka --standalone main.py
  3. Run the compiled program. Note that deprecation warning is appearing incorrectly, even though python-telegram-bot is installed instead of python-telegram-bot-raw
PTBUserWarning: Hey. You seem to be using the `python-telegram-bot-raw` library. Please note that this libray has been deprecated and will no longer be updated. Please instead use the `python-telegram-bot` library. The change requires no changes in your code and requires no additional dependencies. For additional information, please see the channel post at https://t.me/pythontelegrambotchannel/145

Expected behaviour

Deprecation warning should not appear when compiled with Nuitka

Actual behaviour

Deprecation warning appears when compiled with Nuitka even if python-telegram-bot is used instead of python-telegram-bot-raw

Operating System

Any (Recommend to test on Linux)

Version of Python, python-telegram-bot & dependencies

python-telegram-bot 21.3
Bot API 7.4
Python 3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64 bit (AMD64)]

Relevant log output

No response

Additional Context

Related issue: #4261

Related PR: #4270

Related code:

if not (Path(__file__).parent.resolve().absolute() / "ext").exists():
_MESSAGE = (
"Hey. You seem to be using the `python-telegram-bot-raw` library. "
"Please note that this libray has been deprecated and will no longer be updated. "
"Please instead use the `python-telegram-bot` library. The change requires no "
"changes in your code and requires no additional dependencies. For additional "
"information, please see the channel post at "
"https://t.me/pythontelegrambotchannel/145."
)
# DeprecationWarning is ignored by default in Python 3.7 and later by default outside
# __main__ modules. We use both warning categories to increase the chance of the user
# seeing the warning.
warn(
warnings.PTBDeprecationWarning(version="21.3", message=_MESSAGE),
stacklevel=2,
)
warn(
message=_MESSAGE,
category=warnings.PTBUserWarning,
stacklevel=2,
)

This is happening because main.dist/telegram/ext is missing in the Nuitka compilation result.

Instead of checking by if not (Path(__file__).parent.resolve().absolute() / "ext").exists():, I propose using this check instead:

import importlib.utils

if importlib.util.find_spec("telegram.ext") is None:
    ...
@Poolitzer
Copy link
Member

Did you verify it fixes the issue and still gives an error when you use ptb-raw?

@Bibo-Joshi
Copy link
Member

Hi. Thanks for reaching out.

TBH I'm a bit confused by your description: In step one you say that you install ptb-raw. In step 5 you claim that you installed PTB instead of PTB-raw. Is step 1 a typo?

I'm not sure how common this use case is. I'm not familiar with nuitka and how it does the compilation. E.g. I'm wondering how nuitka can detect telegram.ext if it doesn't include that module in compilation...

Additionally, I'd like to point out that instead of doing a release that addresses the false positive, we could instead just get #4288 going and drop the warning from PTB completely:)

@laggykiller
Copy link
Author

laggykiller commented Jun 10, 2024

@Bibo-Joshi Yes that was a typo, corrected.

@Poolitzer The fix is working only if main.py (The script being compiled) uses telegram.ext. Some workarounds:

  • Creating main.dist/telegram/ext directory manually after compiling (Confirm to work)
  • Set some variable to a value that is specific to python-telegram-bot-raw and check for it, but this might be overkill.
  • Checking __compiled__ in globals(), but this is overkill for a small nuisance.

I'm not sure how common this use case is.

Might also affect pyinstaller (?)

we could instead just get 4288 going and drop the warning from PTB completely

Nice! No headache at all if that get merged.

@harshil21
Copy link
Member

Closed by #4288

@github-actions github-actions bot locked and limited conversation to collaborators Jun 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
4 participants