Skip to content

Commit fbda008

Browse files
Kludexatombrellapre-commit-ci[bot]
authored
Add mypy to the pipeline (#7383)
* Add typing to Celery This is a simple bootstrap of the process, adding some types to a few selected functions, based on comment annotations. MyPy is chosen as the default static analyzer for the types. * Add mypy to the pipeline * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove unused command from tox * Install mypy only on CPython * Remove wrong annotations * Update celery/utils/saferepr.py Co-authored-by: Mads Jensen <mje@inducks.org> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 24f22a5 commit fbda008

8 files changed

Lines changed: 48 additions & 23 deletions

File tree

.github/workflows/lint_python.yml

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,22 @@ jobs:
99
- uses: pre-commit/action@v2.0.3
1010
- run: pip install --upgrade pip wheel
1111
- run: pip install bandit codespell flake8 isort pytest pyupgrade tox
12-
- run: bandit -r . || true
13-
- run: codespell --ignore-words-list="brane,gool,ist,sherif,wil" --quiet-level=2 --skip="*.key" || true
12+
13+
- name: bandit
14+
run: bandit -r . || true
15+
16+
- name: Run CodeSpell
17+
run: codespell --ignore-words-list="brane,gool,ist,sherif,wil" --quiet-level=2 --skip="*.key" || true
1418
- run: pip install -r requirements.txt || true
15-
- run: tox || true
16-
- run: pytest . || true
17-
- run: pytest --doctest-modules . || true
19+
20+
- name: Run tox
21+
run: tox || true
22+
23+
- name: Run pytest
24+
run: pytest . || true
25+
26+
- name: Test pytest with doctest
27+
run: pytest --doctest-modules . || true
28+
29+
- name: MyPy
30+
run: tox -e mypy

celery/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
__all__ = ('main',)
88

99

10-
def main():
10+
def main() -> None:
1111
"""Entrypoint to the ``celery`` umbrella command."""
1212
if 'multi' not in sys.argv:
1313
maybe_patch_concurrency()

celery/contrib/testing/worker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def _start_worker_process(app,
167167
cluster.stopwait()
168168

169169

170-
def setup_app_for_worker(app, loglevel, logfile):
170+
def setup_app_for_worker(app, loglevel, logfile) -> None:
171171
# type: (Celery, Union[str, int], str) -> None
172172
"""Setup the app to be used for starting an embedded worker."""
173173
app.finalize()

celery/events/state.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from itertools import islice
2323
from operator import itemgetter
2424
from time import time
25-
from typing import Mapping
25+
from typing import Mapping, Optional
2626
from weakref import WeakSet, ref
2727

2828
from kombu.clocks import timetuple
@@ -452,7 +452,7 @@ def clear_tasks(self, ready=True):
452452
with self._mutex:
453453
return self._clear_tasks(ready)
454454

455-
def _clear_tasks(self, ready=True):
455+
def _clear_tasks(self, ready: bool = True):
456456
if ready:
457457
in_progress = {
458458
uuid: task for uuid, task in self.itertasks()
@@ -470,7 +470,7 @@ def _clear(self, ready=True):
470470
self.event_count = 0
471471
self.task_count = 0
472472

473-
def clear(self, ready=True):
473+
def clear(self, ready: bool = True):
474474
with self._mutex:
475475
return self._clear(ready)
476476

@@ -647,13 +647,13 @@ def rebuild_taskheap(self, timetuple=timetuple):
647647
]
648648
heap.sort()
649649

650-
def itertasks(self, limit=None):
650+
def itertasks(self, limit: Optional[int] = None):
651651
for index, row in enumerate(self.tasks.items()):
652652
yield row
653653
if limit and index + 1 >= limit:
654654
break
655655

656-
def tasks_by_time(self, limit=None, reverse=True):
656+
def tasks_by_time(self, limit=None, reverse: bool = True):
657657
"""Generator yielding tasks ordered by time.
658658
659659
Yields:

celery/utils/collections.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ def __getattr__(self, k):
113113
raise AttributeError(
114114
f'{type(self).__name__!r} object has no attribute {k!r}')
115115

116-
def __setattr__(self, key, value):
117-
# type: (str, Any) -> None
116+
def __setattr__(self, key: str, value) -> None:
118117
"""`d[key] = value -> d.key = value`."""
119118
self[key] = value
120119

@@ -595,7 +594,7 @@ def purge(self, now=None):
595594
break # oldest item hasn't expired yet
596595
self.pop()
597596

598-
def pop(self, default=None):
597+
def pop(self, default=None) -> Any:
599598
# type: (Any) -> Any
600599
"""Remove and return the oldest item, or :const:`None` when empty."""
601600
while self._heap:
@@ -671,20 +670,17 @@ class Evictable:
671670

672671
Empty = Empty
673672

674-
def evict(self):
675-
# type: () -> None
673+
def evict(self) -> None:
676674
"""Force evict until maxsize is enforced."""
677675
self._evict(range=count)
678676

679-
def _evict(self, limit=100, range=range):
680-
# type: (int) -> None
677+
def _evict(self, limit: int = 100, range=range) -> None:
681678
try:
682679
[self._evict1() for _ in range(limit)]
683680
except IndexError:
684681
pass
685682

686-
def _evict1(self):
687-
# type: () -> None
683+
def _evict1(self) -> None:
688684
if self._evictcount <= self.maxsize:
689685
raise IndexError()
690686
try:
@@ -746,8 +742,7 @@ def __len__(self):
746742
# type: () -> int
747743
return self._len()
748744

749-
def __contains__(self, item):
750-
# type: () -> bool
745+
def __contains__(self, item) -> bool:
751746
return item in self.data
752747

753748
def __reversed__(self):

pyproject.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,15 @@ testpaths = "t/unit/"
44
python_classes = "test_*"
55
xdfail_strict=true
66
markers = ["sleepdeprived_patched_module", "masked_modules", "patched_environ", "patched_module"]
7+
8+
[tool.mypy]
9+
warn_unused_configs = true
10+
strict = false
11+
warn_return_any = true
12+
follow_imports = "skip"
13+
show_error_codes = true
14+
disallow_untyped_defs = true
15+
ignore_missing_imports = true
16+
files = [
17+
"celery/__main__.py",
18+
]

requirements/test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ pytest-subtests
44
pytest-timeout~=2.1.0
55
boto3>=1.9.178
66
moto>=2.2.6
7+
# typing extensions
8+
mypy; platform_python_implementation=="CPython"
79
pre-commit
810
-r extras/yaml.txt
911
-r extras/msgpack.txt

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,12 @@ basepython =
7878
3.9: python3.9
7979
3.10: python3.10
8080
pypy3: pypy3
81+
mypy: python3.8
8182
lint,apicheck,linkcheck,configcheck,bandit: python3.9
8283
usedevelop = True
8384

85+
[testenv:mypy]
86+
commands = python -m mypy --config-file pyproject.toml
8487

8588
[testenv:apicheck]
8689
setenv =

0 commit comments

Comments
 (0)