Skip to content

Commit 81f3235

Browse files
authored
Merge pull request #57 from mutating/develop
0.0.41
2 parents 4a175bd + fd44410 commit 81f3235

6 files changed

Lines changed: 40 additions & 26 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ html
1919
.claude
2020
CLAUDE.md
2121
AGENTS.md
22+
site

cantok/tokens/abstract/abstract_token.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from time import sleep
44
from typing import Any, Dict, List, Optional, Union
55

6+
from printo import describe_call, not_none
7+
68
from cantok.errors import CancellationError
79
from cantok.tokens.abstract.cancel_cause import CancelCause
810
from cantok.tokens.abstract.report import CancellationReport
@@ -64,32 +66,27 @@ def __init__(self, *tokens: 'AbstractToken', cancelled: bool = False, doc: Optio
6466
self._lock: RLock = RLock()
6567

6668
def __repr__(self) -> str:
67-
chunks = []
6869
superpower = self._text_representation_of_superpower()
69-
if superpower:
70-
chunks.append(superpower)
71-
other_tokens = ', '.join([repr(x) for x in self._tokens])
72-
if other_tokens:
73-
chunks.append(other_tokens)
7470
report = self._get_report(direct=False)
75-
extra_kwargs: Dict[str, Any]
76-
if report.cause == CancelCause.NOT_CANCELLED:
77-
extra_kwargs = {}
78-
elif report.from_token is self and report.cause == CancelCause.CANCELLED:
79-
extra_kwargs = {'cancelled': True}
80-
else:
81-
extra_kwargs = {}
82-
extra_kwargs.update(**(self._get_extra_kwargs()))
83-
if self.doc is not None:
84-
extra_kwargs['doc'] = self.doc
85-
text_representation_of_extra_kwargs = self._text_representation_of_kwargs(
86-
**extra_kwargs,
71+
cancelled = report.from_token is self and report.cause == CancelCause.CANCELLED
72+
73+
return describe_call(
74+
type(self).__name__,
75+
[superpower, *self._tokens],
76+
{
77+
'cancelled': cancelled,
78+
**self._get_extra_kwargs(),
79+
'doc': self.doc,
80+
},
81+
filters={
82+
0: lambda argument: bool(argument),
83+
'cancelled': lambda argument: bool(argument),
84+
'doc': not_none,
85+
},
86+
placeholders={
87+
0: superpower,
88+
},
8789
)
88-
if text_representation_of_extra_kwargs:
89-
chunks.append(text_representation_of_extra_kwargs)
90-
91-
glued_chunks = ', '.join(chunks)
92-
return f'{type(self).__name__}({glued_chunks})'
9390

9491
def __str__(self) -> str:
9592
cancelled_flag = (

cantok/tokens/counter_token.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
import sys
12
from typing import Any, Dict, Optional
23

4+
if sys.version_info < (3, 13): # pragma: no cover
5+
from typing_extensions import deprecated
6+
else: # pragma: no cover
7+
from warnings import deprecated
8+
39
from cantok import AbstractToken, ConditionToken
410
from cantok.errors import CounterCancellationError
511

612

13+
@deprecated('CounterToken is deprecated and will be removed in a future release.')
714
class CounterToken(ConditionToken):
815
"""
916
A token that cancels automatically after a fixed number of iterations.

docs/types_of_tokens/CounterToken.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
> ⚠️ `CounterToken` is deprecated. A new mechanism for limiting attempts and iterations will be introduced in future releases.
2+
13
`CounterToken` is the least intuitive of the tokens provided by this library. Do not use it if you are not sure that you understand how it works. However, it can be very useful in situations where you want to limit the number of attempts to perform an operation.
24

35
`CounterToken` is initialized with an integer greater than or equal to zero. Each time cancellation is checked, this number is decremented by one. When this number becomes zero, the token is considered cancelled:

pyproject.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "cantok"
7-
version = "0.0.40"
7+
version = "0.0.41"
88
authors = [{ name = "Evgeniy Blinov", email = "zheni-b@yandex.ru" }]
99
description = 'Implementation of the "Cancellation Token" pattern'
1010
readme = "README.md"
1111
requires-python = ">=3.8"
1212
dependencies = [
13+
'printo>=0.0.28',
1314
'transfunctions>=0.0.12',
14-
'typing_extensions ; python_version <= "3.9"',
15+
'typing_extensions>=4.5.0 ; python_version < "3.13"',
1516
]
1617
classifiers = [
1718
"Operating System :: OS Independent",
@@ -33,8 +34,9 @@ classifiers = [
3334
'License :: OSI Approved :: MIT License',
3435
'Intended Audience :: Developers',
3536
'Topic :: Software Development :: Libraries',
37+
'Typing :: Typed',
3638
]
37-
keywords = ['cancellation tokens', 'parallel programming', 'concurrency']
39+
keywords = ['cancellation tokens', 'parallel programming', 'concurrency', 'context', 'go context']
3840

3941
[tool.setuptools.package-data]
4042
"cantok" = ["py.typed"]

tests/units/tokens/test_counter_token.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
from cantok.tokens.abstract.abstract_token import CancelCause, CancellationReport
77

88

9+
def test_counter_token_is_deprecated():
10+
with pytest.warns(DeprecationWarning, match='CounterToken is deprecated'):
11+
CounterToken(1)
12+
13+
914
@pytest.mark.parametrize(
1015
'iterations',
1116
[

0 commit comments

Comments
 (0)