Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 34d7a53

Browse files
4.0.0 refactoring - mypy fixes
1 parent 6b4b2ae commit 34d7a53

23 files changed

Lines changed: 248 additions & 202 deletions
Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1-
__all__ = ("CalculationServiceAware",)
1+
from __future__ import annotations
2+
3+
__all__ = ("AbstractCalculationService",)
24

35
import abc
46
import typing
57

6-
from .. import iterators
8+
if typing.TYPE_CHECKING:
9+
from multibar import iterators
10+
711

12+
class AbstractCalculationService(abc.ABC):
13+
__slots__ = ("_start_value", "_end_value", "_length")
814

9-
class CalculationServiceAware(abc.ABC):
10-
__slots__ = ()
15+
def __init__(
16+
self,
17+
start_value: typing.Union[int, float],
18+
end_value: typing.Union[int, float],
19+
length: int,
20+
) -> None:
21+
self._start_value = start_value
22+
self._end_value = end_value
23+
self._length = length
1124

1225
@abc.abstractmethod
1326
def calculate_filled_indexes(self) -> iterators.AbstractIterator[int]:
@@ -27,17 +40,19 @@ def progressbar_length(self) -> int:
2740
def progress_percents(self) -> float:
2841
...
2942

30-
@property
43+
@staticmethod
3144
@abc.abstractmethod
32-
def start_value(self) -> typing.Union[int, float]:
45+
def get_progress_percentage(start: typing.Union[int, float], end: typing.Union[int, float], /) -> float:
3346
...
3447

3548
@property
36-
@abc.abstractmethod
49+
def start_value(self) -> typing.Union[int, float]:
50+
return self._start_value
51+
52+
@property
3753
def end_value(self) -> typing.Union[int, float]:
38-
...
54+
return self._end_value
3955

40-
@staticmethod
41-
@abc.abstractmethod
42-
def get_progress_percentage(start: int, end: int, /) -> float:
43-
...
56+
@property
57+
def length_value(self) -> int:
58+
return self._length

multibar/api/clients.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
from . import hooks as hooks_
1010

1111
if typing.TYPE_CHECKING:
12-
from multibar.api import progressbars
13-
from multibar.api import sectors
12+
from multibar.api import progressbars, sectors
1413

1514

1615
class ProgressbarClientAware(abc.ABC):
@@ -21,7 +20,7 @@ def get_progress(
2120
start_value: int,
2221
end_value: int,
2322
/,
24-
) -> typing.Optional[progressbars.ProgressbarAware[sectors.SectorAware]]:
23+
) -> typing.Optional[progressbars.ProgressbarAware[sectors.AbstractSector]]:
2524
...
2625

2726
@abc.abstractmethod
@@ -33,7 +32,7 @@ def get_progress(
3332
/,
3433
*,
3534
length: int,
36-
) -> typing.Optional[progressbars.ProgressbarAware[sectors.SectorAware]]:
35+
) -> typing.Optional[progressbars.ProgressbarAware[sectors.AbstractSector]]:
3736
...
3837

3938
@abc.abstractmethod
@@ -44,7 +43,7 @@ def get_progress(
4443
/,
4544
*,
4645
length: int = 20,
47-
) -> typing.Optional[progressbars.ProgressbarAware[sectors.SectorAware]]:
46+
) -> typing.Optional[progressbars.ProgressbarAware[sectors.AbstractSector]]:
4847
...
4948

5049
@abc.abstractmethod

multibar/api/contracts.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
from __future__ import annotations
22

3-
__all__ = ("ContractAware", "ContractCheck", "ContractManagerAware",)
3+
__all__ = (
4+
"ContractAware",
5+
"ContractCheck",
6+
"ContractManagerAware",
7+
)
48

59
import abc
6-
import typing
710
import dataclasses
11+
import typing
12+
13+
from returns.io import IO
814

9-
from .. import utils
15+
from multibar import utils
1016

1117

1218
@dataclasses.dataclass
1319
class ContractCheck:
1420
kept: bool
15-
metadata: dict[str, typing.Any] = dataclasses.field(default_factory=dict)
21+
metadata: typing.MutableMapping[typing.Any, typing.Any] = dataclasses.field(default_factory=dict)
1622
warnings: list[str] = dataclasses.field(default_factory=list)
1723
errors: list[str] = dataclasses.field(default_factory=list)
1824

1925
@classmethod
2026
def done(
2127
cls,
22-
metadata: typing.Optional[dict[str, typing.Any]] = None,
28+
metadata: typing.Optional[typing.MutableMapping[typing.Any, typing.Any]] = None,
2329
) -> ContractCheck:
2430
return cls(kept=True, metadata=utils.none_or({}, metadata))
2531

2632
@classmethod
33+
@typing.no_type_check
2734
def terminated(
2835
cls,
29-
metadata: typing.Optional[dict[str, typing.Any]] = None,
36+
metadata: typing.Optional[typing.MutableMapping[typing.Any, typing.Any]] = None,
3037
warnings: typing.Optional[list[str]] = None,
3138
errors: typing.Optional[list[str]] = None,
3239
) -> ContractCheck:
@@ -46,15 +53,15 @@ def check(self, *args: typing.Any, **kwargs: typing.Any) -> ContractCheck:
4653
...
4754

4855
@abc.abstractmethod
49-
def render_terminated_contract(self, check: ContractCheck, /, *, raise_errors: bool) -> None:
56+
def render_terminated_contract(self, check: ContractCheck, /, *, raise_errors: bool) -> IO[None]:
5057
...
5158

5259

5360
class ContractManagerAware(abc.ABC):
5461
__slots__ = ()
5562

5663
@abc.abstractmethod
57-
def trigger_contract(
64+
def check_contract(
5865
self,
5966
contract: ContractAware,
6067
*args: typing.Any,
@@ -63,7 +70,7 @@ def trigger_contract(
6370
...
6471

6572
@abc.abstractmethod
66-
def trigger_contracts(self, *args: typing.Any, **kwargs: typing.Any) -> None:
73+
def check_contracts(self, *args: typing.Any, **kwargs: typing.Any) -> None:
6774
...
6875

6976
@abc.abstractmethod

multibar/api/hooks.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
from __future__ import annotations
22

3-
__all__ = ("HookSignatureType", "HooksAware",)
3+
__all__ = (
4+
"HookSignatureType",
5+
"HooksAware",
6+
)
47

58
import abc
69
import typing
710

11+
import typing_extensions
12+
813
if typing.TYPE_CHECKING:
914
from . import clients
1015

11-
HookSignatureType: typing.TypeAlias = typing.Callable[..., typing.Optional[bool]]
16+
HookSignatureType: typing_extensions.TypeAlias = typing.Callable[..., typing.Optional[bool]]
1217

1318

1419
class HooksAware(abc.ABC):
1520
__slots__ = ()
1621

17-
def __or__(self, other: typing.Any) -> typing.Any:
18-
if not isinstance(other, HooksAware):
19-
return NotImplemented
20-
21-
self.on_error_hooks.extend(other.on_error_hooks)
22-
self.post_execution_hooks.extend(other.post_execution_hooks)
23-
self.pre_execution_hooks.extend(other.pre_execution_hooks)
24-
25-
return self
22+
@abc.abstractmethod
23+
def __bool__(self) -> bool:
24+
...
2625

2726
@abc.abstractmethod
2827
def add_to_client(self, writer: clients.ProgressbarClientAware, /) -> HooksAware:
2928
...
3029

30+
@abc.abstractmethod
31+
def update(self, other: HooksAware, /) -> HooksAware:
32+
...
33+
3134
@abc.abstractmethod
3235
def add_pre_execution(self, callback: HookSignatureType, /) -> HooksAware:
3336
...

multibar/api/progressbars.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from . import sectors
99

10-
SectorT = typing.TypeVar("SectorT", bound=sectors.SectorAware)
10+
SectorT = typing.TypeVar("SectorT", bound=sectors.AbstractSector)
1111

1212

1313
class ProgressbarAware(abc.ABC, typing.Generic[SectorT]):
@@ -16,7 +16,9 @@ def add_sector(self, sector: SectorT, /) -> ProgressbarAware[SectorT]:
1616
...
1717

1818
@abc.abstractmethod
19-
def replace_visual(self, sector_pos: int, new_visual: str, /) -> ProgressbarAware[SectorT]:
19+
def replace_visual(
20+
self, sector_pos: int, new_visual: typing.Union[str, bytes], /
21+
) -> ProgressbarAware[SectorT]:
2022
...
2123

2224
@property

multibar/api/sectors.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
__all__ = ("SectorAware",)
3+
__all__ = ("AbstractSector",)
44

55
import abc
66
import typing
@@ -11,18 +11,25 @@
1111
SelfT = typing.TypeVar("SelfT", bound="AbstractSector")
1212

1313

14-
class SectorAware(abc.ABC):
14+
class AbstractSector(abc.ABC):
15+
__slots__ = ("_name", "_is_filled", "_position")
16+
17+
def __init__(self, name: typing.Union[str, bytes], is_filled: bool, position: int) -> None:
18+
self._name = name
19+
self._is_filled = is_filled
20+
self._position = position
21+
1522
@abc.abstractmethod
1623
def add_to_progressbar(self: SelfT, progressbar: progressbars.ProgressbarAware[SelfT], /) -> SelfT:
1724
...
1825

1926
@property
2027
@abc.abstractmethod
21-
def name(self) -> typing.AnyStr:
28+
def name(self) -> typing.Union[str, bytes]:
2229
...
2330

2431
@abc.abstractmethod
25-
def change_name(self, value: str, /) -> SectorAware:
32+
def change_name(self, value: typing.Union[str, bytes], /) -> AbstractSector:
2633
...
2734

2835
@property

multibar/api/signatures.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
__all__ = ("SignatureSegmentProtocol", "ProgressbarSignatureProtocol",)
1+
__all__ = (
2+
"SignatureSegmentProtocol",
3+
"ProgressbarSignatureProtocol",
4+
)
25

36
import typing
47

58

69
@typing.runtime_checkable
710
class SignatureSegmentProtocol(typing.Protocol):
811
@property
9-
def on_filled(self) -> typing.AnyStr:
12+
def on_filled(self) -> typing.Union[str, bytes]:
13+
# TODO: mypy has some problems with typing.AnyStr
1014
raise NotImplementedError
1115

1216
@property
13-
def on_unfilled(self) -> typing.AnyStr:
17+
def on_unfilled(self) -> typing.Union[str, bytes]:
1418
raise NotImplementedError
1519

1620

multibar/api/writers.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
import abc
66
import typing
77

8-
from . import signatures
9-
from . import progressbars
10-
from . import sectors
118
from . import calculation_service as math_operations
9+
from . import progressbars, sectors, signatures
1210

13-
SectorT = typing.TypeVar("SectorT", bound=sectors.SectorAware)
11+
SectorT = typing.TypeVar("SectorT", bound=sectors.AbstractSector)
1412

1513

1614
class ProgressbarWriterAware(abc.ABC, typing.Generic[SectorT]):
@@ -28,7 +26,7 @@ def write(
2826
start_value: int,
2927
end_value: int,
3028
/,
31-
) -> typing.Optional[progressbars.ProgressbarAware[SectorT]]:
29+
) -> progressbars.ProgressbarAware[SectorT]:
3230
...
3331

3432
@typing.overload
@@ -40,7 +38,7 @@ def write(
4038
/,
4139
*,
4240
length: int,
43-
) -> typing.Optional[progressbars.ProgressbarAware[SectorT]]:
41+
) -> progressbars.ProgressbarAware[SectorT]:
4442
...
4543

4644
@abc.abstractmethod
@@ -51,7 +49,7 @@ def write(
5149
/,
5250
*,
5351
length: int = 20,
54-
) -> typing.Optional[progressbars.ProgressbarAware[SectorT]]:
52+
) -> progressbars.ProgressbarAware[SectorT]:
5553
...
5654

5755
@abc.abstractmethod
@@ -72,10 +70,10 @@ def sector_cls(self) -> typing.Type[SectorT]:
7270

7371
@property
7472
@abc.abstractmethod
75-
def progressbar_cls(self) -> typing.Type[progressbars.ProgressbarAware]:
73+
def progressbar_cls(self) -> typing.Type[progressbars.ProgressbarAware[SectorT]]:
7674
...
7775

7876
@property
7977
@abc.abstractmethod
80-
def calculation_cls(self) -> typing.Type[math_operations.CalculationServiceAware]:
78+
def calculation_cls(self) -> typing.Type[math_operations.AbstractCalculationService]:
8179
...

multibar/errors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import annotations
2+
23
import typing
34

45
if typing.TYPE_CHECKING:

multibar/impl/calculation_service.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,7 @@
66
from multibar.api import calculation_service
77

88

9-
class ProgressbarCalculationService(calculation_service.CalculationServiceAware):
10-
def __init__(
11-
self,
12-
start_value: typing.Union[int, float],
13-
end_value: typing.Union[int, float],
14-
length: int,
15-
) -> None:
16-
self._start_value = start_value
17-
self._end_value = end_value
18-
self._length = length
19-
9+
class ProgressbarCalculationService(calculation_service.AbstractCalculationService):
2010
def calculate_filled_indexes(self) -> iterators.AbstractIterator[int]:
2111
filled_range = range(round(self.progress_percents / (100 / self._length)))
2212
return iterators.Iterator(iter(filled_range)).indexes()
@@ -43,5 +33,5 @@ def end_value(self) -> typing.Union[int, float]:
4333
return self._end_value
4434

4535
@staticmethod
46-
def get_progress_percentage(start: int, end: int, /) -> float:
36+
def get_progress_percentage(start: typing.Union[int, float], end: typing.Union[int, float], /) -> float:
4737
return (start / end) * 100

0 commit comments

Comments
 (0)