Make cryptography Dependency Optional & Refactor Some Tests (#2386) · matsuoki/python-telegram-bot@a34f0b9 · GitHub
Skip to content

Commit a34f0b9

Browse files
authored
Make cryptography Dependency Optional & Refactor Some Tests (python-telegram-bot#2386)
* Make cryptography optional * Try fixing CI * Try some more * Update pytest, mypy & pyupgrade, refactor test_meta, hope that things start to work * Fix filterwarnings * Mama mia! Here we go again! * Add stupid debug prints * A new hope
1 parent eee8921 commit a34f0b9

17 files changed

Lines changed: 196 additions & 51 deletions

.github/workflows/test.yml

Lines changed: 13 additions & 14 deletions

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ repos:
2121
args:
2222
- --rcfile=setup.cfg
2323
- repo: https://github.com/pre-commit/mirrors-mypy
24-
rev: v0.790
24+
rev: v0.800
2525
hooks:
2626
- id: mypy
2727
files: ^(telegram|examples)/.*\.py$
2828
- repo: https://github.com/asottile/pyupgrade
29-
rev: v2.7.4
29+
rev: v2.10.0
3030
hooks:
3131
- id: pyupgrade
3232
files: ^(telegram|examples|tests)/.*\.py$

README.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ In case you have a previously cloned local repository already, you should initia
137137
138138
$ git submodule update --init --recursive
139139
140+
---------------------
141+
Optional Dependencies
142+
---------------------
143+
144+
PTB can be installed with optional dependencies:
145+
146+
* ``pip install python-telegram-bot[passport]`` installs the `cryptography <https://cryptography.io>`_ library. Use this, if you want to use Telegram Passport related functionality.
147+
* ``pip install python-telegram-bot[ujson]`` installs the `ujson <https://pypi.org/project/ujson/>`_ library. It will then be used for JSON de- & encoding, which can bring speed up compared to the standard `json <https://docs.python.org/3/library/json.html>`_ library.
148+
* ``pip install python-telegram-bot[socks]`` installs the `PySocks <https://pypi.org/project/PySocks/>`_ library. Use this, if you want to work behind a Socks5 server.
149+
140150
===============
141151
Getting started
142152
===============

README_RAW.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ Note
137137

138138
Installing the `.tar.gz` archive available on PyPi directly via `pip` will *not* work as expected, as `pip` does not recognize that it should use `setup-raw.py` instead of `setup.py`.
139139

140+
---------------------
141+
Optional Dependencies
142+
---------------------
143+
144+
PTB can be installed with optional dependencies:
145+
146+
* ``pip install python-telegram-bot-raw[passport]`` installs the `cryptography <https://cryptography.io>`_ library. Use this, if you want to use Telegram Passport related functionality.
147+
* ``pip install python-telegram-bot-raw[ujson]`` installs the `ujson <https://pypi.org/project/ujson/>`_ library. It will then be used for JSON de- & encoding, which can bring speed up compared to the standard `json <https://docs.python.org/3/library/json.html>`_ library.
148+
140149
===============
141150
Getting started
142151
===============

requirements-dev.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
# cryptography is an optional dependency, but running the tests properly requires it
2+
cryptography!=3.4,!=3.4.1,!=3.4.2,!=3.4.3
3+
14
pre-commit
25
# Make sure that the versions specified here match the pre-commit settings
36
black==20.8b1
47
flake8==3.8.4
58
pylint==2.6.0
6-
mypy==0.790
7-
pyupgrade==2.7.4
9+
mypy==0.800
10+
pyupgrade==2.10.0
811

9-
pytest==4.2.0
10-
# Need older attrs version for pytest 4.2.0
11-
attrs==19.1.0
12+
pytest==6.2.2
1213

1314
flaky
1415
beautifulsoup4

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
certifi
2-
cryptography!=3.4
32
# only telegram.ext: # Keep this line here; used in setup(-raw).py
43
tornado>=5.1
54
APScheduler==3.6.3

setup.cfg

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ addopts = --no-success-flaky-report -rsxX
2727
filterwarnings =
2828
error
2929
ignore::DeprecationWarning
30-
ignore::telegram.utils.deprecate.TelegramDeprecationWarning
30+
; Unfortunately due to https://github.com/pytest-dev/pytest/issues/8343 we can't have this here
31+
; and instead do a trick directly in tests/conftest.py
32+
; ignore::telegram.utils.deprecate.TelegramDeprecationWarning
3133

3234
[coverage:run]
3335
branch = True

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ def get_setup_kwargs(raw=False):
8484
install_requires=requirements,
8585
extras_require={
8686
'json': 'ujson',
87-
'socks': 'PySocks'
87+
'socks': 'PySocks',
88+
# 3.4-3.4.3 contained some cyclical import bugs
89+
'passport': 'cryptography!=3.4,!=3.4.1,!=3.4.2,!=3.4.3',
8890
},
8991
include_package_data=True,
9092
classifiers=[

telegram/bot.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@
4141
except ImportError:
4242
import json # type: ignore[no-redef] # noqa: F723
4343

44-
from cryptography.hazmat.backends import default_backend
45-
from cryptography.hazmat.primitives import serialization
44+
try:
45+
from cryptography.hazmat.backends import default_backend
46+
from cryptography.hazmat.primitives import serialization
47+
48+
CRYPTO_INSTALLED = True
49+
except ImportError:
50+
default_backend = None # type: ignore[assignment]
51+
serialization = None # type: ignore[assignment]
52+
CRYPTO_INSTALLED = False
4653

4754
from telegram import (
4855
Animation,
@@ -212,6 +219,11 @@ def __init__(
212219
self.logger = logging.getLogger(__name__)
213220

214221
if private_key:
222+
if not CRYPTO_INSTALLED:
223+
raise RuntimeError(
224+
'To use Telegram Passports, PTB must be installed via `pip install '
225+
'python-telegram-bot[passport]`.'
226+
)
215227
self.private_key = serialization.load_pem_private_key(
216228
private_key, password=private_key_password, backend=default_backend()
217229
)

telegram/passport/credentials.py

Lines changed: 25 additions & 6 deletions

0 commit comments

Comments
 (0)