10000 Type Hinting (#1920) · Konano/python-telegram-bot@5fd7606 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5fd7606

Browse files
committed
1 parent 103b115 commit 5fd7606

File tree

151 files changed

+3696
-2702
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+3696
-2702
lines changed

.github/CONTRIBUTING.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ Here's how to make a one-off code change.
6868
- You can refer to relevant issues in the commit message by writing, e.g., "#105".
6969

7070
- Your code should adhere to the `PEP 8 Style Guide`_, with the exception that we have a maximum line length of 99.
71-
71+
72+
- Provide static typing with signature annotations. The documentation of `MyPy`_ will be a good start, the cheat sheet is `here`_. We also have some custom type aliases in ``telegram.utils.helpers.typing``.
73+
7274
- Document your code. This project uses `sphinx`_ to generate static HTML docs. To build them, first make sure you have the required dependencies:
7375

7476
.. code-block:: bash
@@ -251,3 +253,5 @@ break the API classes. For example:
251253
.. _`Google Python Style Guide`: http://google.github.io/styleguide/pyguide.html
252254
.. _`Google Python Style Docstrings`: https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
253255
.. _AUTHORS.rst: ../AUTHORS.rst
256+
.. _`MyPy`: https://mypy.readthedocs.io/en/stable/index.html
257+
.. _`here`: https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ htmlcov/
4646
.coverage.*
4747
.cache
4848
.pytest_cache
49+
.mypy_cache
4950
nosetests.xml
5051
coverage.xml
5152
*,cover

.pre-commit-config.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ repos:
77
args:
88
- --diff
99
- repo: https://gitlab.com/pycqa/flake8
10-
rev: 3.7.1
10+
rev: 3.8.1
1111
hooks:
1212
- id: flake8
1313
- repo: git://github.com/pre-commit/mirrors-pylint
@@ -18,3 +18,8 @@ repos:
1818
args:
1919
- --errors-only
2020
- --disable=import-error
21+
- repo: https://github.com/pre-commit/mirrors-mypy
22+
rev: 'v0.770'
23+
hooks:
24+
- id: mypy
25+
files: ^telegram/.*\.py$

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ PYTEST := pytest
66
PEP257 := pep257
77
PEP8 := flake8
88
YAPF := yapf
9+
MYPY := mypy
910
PIP := pip
1011

1112
clean:
@@ -28,6 +29,9 @@ yapf:
2829
lint:
2930
$(PYLINT) -E telegram --disable=no-name-in-module,import-error
3031

32+
mypy:
33+
$(MYPY) -p telegram
34+
3135
test:
3236
$(PYTEST) -v
3337

@@ -41,6 +45,7 @@ help:
4145
@echo "- pep8 Check style with flake8"
4246
@echo "- lint Check style with pylint"
4347
@echo "- yapf Check style with yapf"
48+
@echo "- mypy Check type hinting with mypy"
4449
@echo "- test Run tests using pytest"
4550
@echo
4651
@echo "Available variables:"
@@ -49,4 +54,5 @@ help:
4954
@echo "- PEP257 default: $(PEP257)"
5055
@echo "- PEP8 default: $(PEP8)"
5156
@echo "- YAPF default: $(YAPF)"
57+
@echo "- MYPY default: $(MYPY)"
5258
@echo "- PIP default: $(PIP)"

docs/source/telegram.utils.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ telegram.utils package
66
telegram.utils.helpers
77
telegram.utils.promise
88
telegram.utils.request
9+
telegram.utils.types

docs/source/telegram.utils.types.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
telegram.utils.types Module
2+
===========================
3+
4+
.. automodule:: telegram.utils.types
5+
:members:
6+
:show-inheritance:

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pep257
33
pylint
44
flaky
55
yapf
6+
mypy==0.770
67
pre-commit
78
beautifulsoup4
89
pytest==4.2.0

setup.cfg

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,22 @@ omit =
4040
telegram/__main__.py
4141
telegram/vendor/*
4242

43+
[coverage:report]
44+
exclude_lines =
45+
if TYPE_CHECKING:
46+
47+
[mypy]
48+
warn_unused_ignores = True
49+
warn_unused_configs = True
50+
disallow_untyped_defs = True
51+
disallow_incomplete_defs = True
52+
disallow_untyped_decorators = True
53+
show_error_codes = True
54+
55+
[mypy-telegram.vendor.*]
56+
ignore_errors = True
57+
58+
# Disable strict optional for telegram objects with class methods
59+
# We don't want to clutter the code with 'if self.bot is None: raise RuntimeError()'
60+
[mypy-telegram.callbackquery,telegram.chat,telegram.message,telegram.user,telegram.files.*,telegram.inline.inlinequery,telegram.payment.precheckoutquery,telegram.payment.shippingquery,telegram.passport.passportdata,telegram.passport.credentials,telegram.passport.passportfile,telegram.ext.filters]
61+
strict_optional = False

telegram/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@
104104
from .update import Update
105105
from .files.inputmedia import (InputMedia, InputMediaVideo, InputMediaPhoto, InputMediaAnimation,
106106
InputMediaAudio, InputMediaDocument)
107-
from .bot import Bot
108107
from .constants import (MAX_MESSAGE_LENGTH, MAX_CAPTION_LENGTH, SUPPORTED_WEBHOOK_PORTS,
109108
MAX_FILESIZE_DOWNLOAD, MAX_FILESIZE_UPLOAD,
110109
MAX_MESSAGES_PER_SECOND_PER_CHAT, MAX_MESSAGES_PER_SECOND,
@@ -124,6 +123,7 @@
124123
SecureData,
125124
FileCredentials,
126125
TelegramDecryptionError)
126+
from .bot import Bot
127127
from .version import __version__ # noqa: F401
128128

129129
__author__ = 'devs@python-telegram-bot.org'

telegram/__main__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121

2222
import certifi
2323

24+
from typing import Optional
2425

2526
from . import __version__ as telegram_ver
2627

2728

28-
def _git_revision():
29+
def _git_revision() -> Optional[str]:
2930
try:
3031
output = subprocess.check_output(["git", "describe", "--long", "--tags"],
3132
stderr=subprocess.STDOUT)
@@ -34,15 +35,15 @@ def _git_revision():
3435
return output.decode().strip()
3536

3637

37-
def print_ver_info():
38+
def print_ver_info() -> None:
3839
git_revision = _git_revision()
3940
print('python-telegram-bot {}'.format(telegram_ver) + (' ({})'.format(git_revision)
4041
if git_revision else ''))
41-
print('certifi {}'.format(certifi.__version__))
42+
print('certifi {}'.format(certifi.__version__)) # type: ignore[attr-defined]
4243
print('Python {}'.format(sys.version.replace('\n', ' ')))
4344

4445

45-
def main():
46+
def main() -> None:
4647
print_ver_info()
4748

4849

0 commit comments

Comments
 (0)
0