Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ Here's how to make a one-off code change.
- You can refer to relevant issues in the commit message by writing, e.g., "#105".

- Your code should adhere to the `PEP 8 Style Guide`_, with the exception that we have a maximum line length of 99.


- Provide static typing with signature annotations. The documentation of `MyPy`_ will be a good start, the cheat sheet is `here`_.

- Document your code. This project uses `sphinx`_ to generate static HTML docs. To build them, first make sure you have the required dependencies:

.. code-block:: bash
Expand Down Expand Up @@ -251,3 +253,5 @@ break the API classes. For example:
.. _`Google Python Style Guide`: http://google.github.io/styleguide/pyguide.html
.. _`Google Python Style Docstrings`: https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
.. _AUTHORS.rst: ../AUTHORS.rst
.. _`MyPy`: https://mypy.readthedocs.io/en/stable/index.html
.. _`here`: https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html
39 changes: 39 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: typing coverage
on:
pull_request:
branches:
- master
- type_hinting_master
paths:
- 'telegram/**'
- '**.py'
jobs:
mypy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Set up Python 3.7
uses: actions/setup-python@v1
with:
python-version: 3.7
- name: Install dependencies and mypy
run: |
pip install -r requirements.txt
pip install -r requirements-dev.txt
pip install diff-cover lxml
- name: Verify mypy types
run: |
mypy --version
- name: Running mypy on changed files and save report
run: |
python -m mypy -p telegram --cobertura-xml-report . || true
shell: bash
- name: Computing typing coverage
run: |
diff-cover --fail-under=100 cobertura.xml --html-report typing_coverage.html --compare-branch=origin/$GITHUB_BASE_REF
shell: bash
- name: Uploading coverage report as html
uses: actions/upload-artifact@v1
with:
name: typing_coverage
path: typing_coverage.html
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
pull_request:
branches:
- master
- type_hinting_master
schedule:
- cron: 7 3 * * *
push:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ htmlcov/
.coverage.*
.cache
.pytest_cache
.mypy_cache
nosetests.xml
coverage.xml
*,cover
Expand Down
12 changes: 8 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
repos:
- repo: git://github.com/python-telegram-bot/mirrors-yapf
sha: 5769e088ef6e0a0d1eb63bd6d0c1fe9f3606d6c8
rev: 5769e088ef6e0a0d1eb63bd6d0c1fe9f3606d6c8
hooks:
- id: yapf
files: ^(telegram|tests)/.*\.py$
args:
- --diff
- repo: git://github.com/pre-commit/pre-commit-hooks
sha: 0b70e285e369bcb24b57b74929490ea7be9c4b19
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.9
hooks:
- id: flake8
- repo: git://github.com/pre-commit/mirrors-pylint
sha: 9d8dcbc2b86c796275680f239c1e90dcd50bd398
rev: v2.4.4
hooks:
- id: pylint
files: ^telegram/.*\.py$
args:
- --errors-only
- --disable=import-error
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.761'
hooks:
- id: mypy
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ PYTEST := pytest
PEP257 := pep257
PEP8 := flake8
YAPF := yapf
MYPY := mypy
PIP := pip

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

mypy:
$(MYPY) -p telegram

test:
$(PYTEST) -v

Expand All @@ -41,6 +45,7 @@ help:
@echo "- pep8 Check style with flake8"
@echo "- lint Check style with pylint"
@echo "- yapf Check style with yapf"
@echo "- mypy Check type hinting with mypy"
@echo "- test Run tests using pytest"
@echo
@echo "Available variables:"
Expand All @@ -49,4 +54,5 @@ help:
@echo "- PEP257 default: $(PEP257)"
@echo "- PEP8 default: $(PEP8)"
@echo "- YAPF default: $(YAPF)"
@echo "- MYPY default: $(MYPY)"
@echo "- PIP default: $(PIP)"
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pep257
pylint
flaky
yapf
mypy==0.761
pre-commit
beautifulsoup4
pytest==4.2.0
Expand Down
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ omit =
telegram/__main__.py
telegram/vendor/*

[mypy]
warn_unused_configs = True

[mypy-telegram.vendor.*]
ignore_errors = True
2 changes: 1 addition & 1 deletion telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram Bot."""
"""This module contains an object that represents a Telegram Bot and is being used as a workflow test now."""

import functools
import inspect
Expand Down
16 changes: 8 additions & 8 deletions telegram/ext/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import re

from future.utils import string_types

from typing import Optional
from telegram import Chat, Update, MessageEntity

__all__ = ['Filters', 'BaseFilter', 'InvertedFilter', 'MergedFilter']
Expand Down Expand Up @@ -78,11 +78,11 @@ class variable.
(depends on the handler).
"""

name = None
update_filter = False
data_filter = False
name: Optional[str] = None
update_filter: bool = False
data_filter: bool = False

def __call__(self, update):
def __call__(self, update: Update) -> Optional[bool]:
if self.update_filter:
return self.filter(update)
else:
Expand All @@ -97,13 +97,13 @@ def __or__(self, other):
def __invert__(self):
return InvertedFilter(self)

def __repr__(self):
def __repr__(self) -> str:
# We do this here instead of in a __init__ so filter don't have to call __init__ or super()
if self.name is None:
self.name = self.__class__.__name__
return self.name

def filter(self, update):
def filter(self, update: Update) -> Optional[bool]:
"""This method must be overwritten.

Note:
Expand Down Expand Up @@ -136,7 +136,7 @@ def __init__(self, f):
def filter(self, update):
return not bool(self.f(update))

def __repr__(self):
def __repr__(self) -> str:
return "<inverted {}>".format(self.f)


Expand Down