From fb09e130781b33bbd66f3e0065ab953962819427 Mon Sep 17 00:00:00 2001 From: Samuel GIFFARD Date: Tue, 14 Feb 2023 13:33:37 +0100 Subject: [PATCH] Add type hints (#189) The type hints are only checked for Python 3.7+ * New step in CI: mypy. Co-authored-by: Carlo Bertini --- .github/workflows/build.yml | 4 ++ .github/workflows/resolve_versions.py | 7 ++- README.md | 66 ++++++++++++++------------- celery_pubsub/__init__.pyi | 4 ++ celery_pubsub/pubsub.pyi | 21 +++++++++ mypy.ini | 4 ++ requirements_test.txt | 6 ++- setup.py | 4 +- tests/conftest.py | 1 - 9 files changed, 80 insertions(+), 37 deletions(-) create mode 100644 celery_pubsub/__init__.pyi create mode 100644 celery_pubsub/pubsub.pyi create mode 100644 mypy.ini diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 37d1244..a0d2118 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -109,6 +109,10 @@ jobs: pip install "celery${{ matrix.celery }}" pip install pytest-github-actions-annotate-failures + - name: Check with mypy + if: ${{ !startsWith(matrix.python-version, 'pypy') && matrix.python-version >= '3.7' }} + run: mypy + - name: Lint with flake8 run: | pip install flake8 diff --git a/.github/workflows/resolve_versions.py b/.github/workflows/resolve_versions.py index 72df3e3..3543de8 100644 --- a/.github/workflows/resolve_versions.py +++ b/.github/workflows/resolve_versions.py @@ -81,7 +81,12 @@ def parse_args() -> Namespace: ) parser_matrix.add_argument("-n", "--package-name", type=str, help="Package name") parser_matrix.add_argument("-s", "--specifiers", type=str, help="Specifiers") - parser_matrix.add_argument("--invert", action="store_true", default=False, help="Invert matrix (only show the skipped ones)") + parser_matrix.add_argument( + "--invert", + action="store_true", + default=False, + help="Invert matrix (only show the skipped ones)", + ) return parser.parse_args() diff --git a/README.md b/README.md index 63c8254..6c49008 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -# celery-pubsub 1.0.1 +# celery-pubsub 1.0.2 -[![image](https://travis-ci.org/Mulugruntz/celery-pubsub.svg?branch=master)](https://travis-ci.org/Mulugruntz/celery-pubsub) +[![Build and Test](https://github.com/Mulugruntz/celery-pubsub/actions/workflows/build.yml/badge.svg)](https://github.com/Mulugruntz/celery-pubsub/actions/workflows/build.yml) [![Code Climate](https://codeclimate.com/github/Mulugruntz/celery-pubsub/badges/gpa.svg)](https://codeclimate.com/github/Mulugruntz/celery-pubsub) [![Test Coverage](https://codeclimate.com/github/Mulugruntz/celery-pubsub/badges/coverage.svg)](https://codeclimate.com/github/Mulugruntz/celery-pubsub/coverage) [![Issue Count](https://codeclimate.com/github/Mulugruntz/celery-pubsub/badges/issue_count.svg)](https://codeclimate.com/github/Mulugruntz/celery-pubsub) @@ -102,6 +102,8 @@ celery_pubsub.publish('some.very.good.test', 42) # task 3 only ## Changelog +* 1.0.2 + * Add stubs file for type hinting. * 1.0.1 * Changed `README.rst` to `README.md`. * Added better badges to show the supported status for each Celery & Python version. @@ -197,42 +199,42 @@ celery_pubsub.publish('some.very.good.test', 42) # task 3 only [badge-m_linux_pypy3.6_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/m_linux_pypy-3.6_celery5/shields [//]: # (Status in tagged version) -[badge-t_linux_2.7_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_2.7_celery3/shields -[badge-t_linux_2.7_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_2.7_celery4/shields -[badge-t_linux_2.7_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_2.7_celery5/shields +[badge-t_linux_2.7_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_2.7_celery3/shields +[badge-t_linux_2.7_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_2.7_celery4/shields +[badge-t_linux_2.7_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_2.7_celery5/shields -[badge-t_linux_3.4_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.4_celery3/shields -[badge-t_linux_3.4_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.4_celery4/shields -[badge-t_linux_3.4_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.4_celery5/shields +[badge-t_linux_3.4_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.4_celery3/shields +[badge-t_linux_3.4_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.4_celery4/shields +[badge-t_linux_3.4_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.4_celery5/shields -[badge-t_linux_3.5_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.5_celery3/shields -[badge-t_linux_3.5_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.5_celery4/shields -[badge-t_linux_3.5_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.5_celery5/shields +[badge-t_linux_3.5_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.5_celery3/shields +[badge-t_linux_3.5_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.5_celery4/shields +[badge-t_linux_3.5_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.5_celery5/shields -[badge-t_linux_3.6_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.6_celery3/shields -[badge-t_linux_3.6_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.6_celery4/shields -[badge-t_linux_3.6_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.6_celery5/shields +[badge-t_linux_3.6_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.6_celery3/shields +[badge-t_linux_3.6_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.6_celery4/shields +[badge-t_linux_3.6_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.6_celery5/shields -[badge-t_linux_3.7_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.7_celery3/shields -[badge-t_linux_3.7_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.7_celery4/shields -[badge-t_linux_3.7_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.7_celery5/shields +[badge-t_linux_3.7_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.7_celery3/shields +[badge-t_linux_3.7_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.7_celery4/shields +[badge-t_linux_3.7_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.7_celery5/shields -[badge-t_linux_3.8_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.8_celery3/shields -[badge-t_linux_3.8_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.8_celery4/shields -[badge-t_linux_3.8_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.8_celery5/shields +[badge-t_linux_3.8_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.8_celery3/shields +[badge-t_linux_3.8_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.8_celery4/shields +[badge-t_linux_3.8_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.8_celery5/shields -[badge-t_linux_3.9_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.9_celery3/shields -[badge-t_linux_3.9_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.9_celery4/shields -[badge-t_linux_3.9_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.9_celery5/shields +[badge-t_linux_3.9_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.9_celery3/shields +[badge-t_linux_3.9_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.9_celery4/shields +[badge-t_linux_3.9_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.9_celery5/shields -[badge-t_linux_3.10_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.10_celery3/shields -[badge-t_linux_3.10_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.10_celery4/shields -[badge-t_linux_3.10_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_3.10_celery5/shields +[badge-t_linux_3.10_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.10_celery3/shields +[badge-t_linux_3.10_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.10_celery4/shields +[badge-t_linux_3.10_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_3.10_celery5/shields -[badge-t_linux_pypy2.7_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_pypy-2.7_celery3/shields -[badge-t_linux_pypy2.7_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_pypy-2.7_celery4/shields -[badge-t_linux_pypy2.7_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_pypy-2.7_celery5/shields +[badge-t_linux_pypy2.7_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_pypy-2.7_celery3/shields +[badge-t_linux_pypy2.7_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_pypy-2.7_celery4/shields +[badge-t_linux_pypy2.7_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_pypy-2.7_celery5/shields -[badge-t_linux_pypy3.6_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_pypy-3.6_celery3/shields -[badge-t_linux_pypy3.6_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_pypy-3.6_celery4/shields -[badge-t_linux_pypy3.6_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.1_linux_pypy-3.6_celery5/shields +[badge-t_linux_pypy3.6_celery3]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_pypy-3.6_celery3/shields +[badge-t_linux_pypy3.6_celery4]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_pypy-3.6_celery4/shields +[badge-t_linux_pypy3.6_celery5]: https://byob.yarr.is/Mulugruntz/celery-pubsub/1.0.2_linux_pypy-3.6_celery5/shields diff --git a/celery_pubsub/__init__.pyi b/celery_pubsub/__init__.pyi new file mode 100644 index 0000000..a2a56d0 --- /dev/null +++ b/celery_pubsub/__init__.pyi @@ -0,0 +1,4 @@ +from .pubsub import publish as publish +from .pubsub import publish_now as publish_now +from .pubsub import subscribe as subscribe +from .pubsub import unsubscribe as unsubscribe diff --git a/celery_pubsub/pubsub.pyi b/celery_pubsub/pubsub.pyi new file mode 100644 index 0000000..7ee4a9a --- /dev/null +++ b/celery_pubsub/pubsub.pyi @@ -0,0 +1,21 @@ +import re +from typing import Any + +from celery import Task, group +from celery.result import AsyncResult + +class PubSubManager: + subscribed: set[tuple[str, re.Pattern[str], Task[Any, Any]]] + jobs: dict[str, group] + def publish(self, topic: str, *args: Any, **kwargs: Any) -> AsyncResult[Any]: ... + def publish_now( + self, topic: str, *args: Any, **kwargs: Any + ) -> AsyncResult[Any]: ... + def subscribe(self, topic: str, task: Task[Any, Any]) -> None: ... + def unsubscribe(self, topic: str, task: Task[Any, Any]) -> None: ... + def get_jobs(self, topic: str) -> group: ... + +def publish(topic: str, *args: Any, **kwargs: Any) -> AsyncResult[Any]: ... +def publish_now(topic: str, *args: Any, **kwargs: Any) -> AsyncResult[Any]: ... +def subscribe(topic: str, task: Task[Any, Any]) -> None: ... +def unsubscribe(topic: str, task: Task[Any, Any]) -> None: ... diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000..5598a63 --- /dev/null +++ b/mypy.ini @@ -0,0 +1,4 @@ +[mypy] +strict = True +# Only check files in the `celery_pubsub` package +files = celery_pubsub diff --git a/requirements_test.txt b/requirements_test.txt index 3ce92d8..6a31a09 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -1,2 +1,6 @@ pytest -coverage==5.5 \ No newline at end of file +coverage==5.5 + +# Only for Python 3.7+ (also exclude pypy). +celery-types==0.14.0; python_version >= '3.7' and implementation_name != 'pypy' +mypy==1.0.0; python_version >= '3.7' and implementation_name != 'pypy' diff --git a/setup.py b/setup.py index 4f58247..91cc940 100644 --- a/setup.py +++ b/setup.py @@ -34,7 +34,7 @@ def tests_require(): setuptools.setup( name="celery-pubsub", packages=["celery_pubsub"], - version="1.0.1", + version="1.0.2", description="A Publish and Subscribe library for Celery", long_description=long_description(), long_description_content_type="text/markdown", @@ -42,7 +42,7 @@ def tests_require(): author_email="mulugruntz@gmail.com", license="MIT", url="https://github.com/Mulugruntz/celery-pubsub", - download_url="https://github.com/Mulugruntz/celery-pubsub/tarball/1.0.1", + download_url="https://github.com/Mulugruntz/celery-pubsub/tarball/1.0.2", keywords=["celery", "publish", "subscribe", "pubsub"], classifiers=[ "Development Status :: 5 - Production/Stable", diff --git a/tests/conftest.py b/tests/conftest.py index 6dd5d45..ebe7327 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,7 +14,6 @@ def celery_worker(): pass - else: # pragma: no cover task = celery.shared_task