Skip to content

Commit df36449

Browse files
legatusAlexMityuha
andauthored
Feature/42 ruff (#51)
* Feature/42 ruff * Feature/42 ruff * ignore D213 instead * fix D212 warning * remove all from pyi * fix requirements * update dependencies in pipeline * update typing-extensions for pipeline * update all libs; remove types-dataclasses * update pipeline dependencies * fix pre-commit * auto fix linting problems * remove unused shape.py file * fix docstring * check for format in pipeline as well * fix typo * add ruff options * reformat file; disable D413 * bump library version --------- Co-authored-by: mityuha <mit.makaroff@gmail.com>
1 parent 2a13aae commit df36449

36 files changed

+722
-1460
lines changed

.github/workflows/build.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ jobs:
1313
strategy:
1414
matrix:
1515
include:
16-
- { python-version: "3.6", runs-on: "ubuntu-20.04" }
17-
- { python-version: "3.7" }
1816
- { python-version: "3.8" }
1917
- { python-version: "3.9" }
2018
- { python-version: "3.10" }

.pre-commit-config.yaml

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
repos:
22
- repo: local
33
hooks:
4-
- id: pydocstringformatter
5-
name: pydocstringformatter
6-
description: "Formats docstrings to follow PEP8 and PEP257."
7-
language: system
8-
entry: poetry run pydocstringformatter
9-
args:
10-
[
11-
"-w",
12-
"--max-line-length=120",
13-
]
14-
types: [python]
15-
- id: isort
16-
name: isort
4+
- id: ruff
5+
name: ruff
176
stages: [commit]
18-
language: system
19-
entry: poetry run isort
20-
types: [python]
21-
- id: black
22-
name: black
7+
language: python
8+
types_or: [python, pyi]
9+
entry: poetry run ruff check
10+
exclude: examples
11+
- id: ruff-format
12+
name: ruff-format
2313
stages: [commit]
24-
language: system
25-
entry: poetry run black
26-
types: [python]
14+
language: python
15+
types_or: [python, pyi]
16+
entry: poetry run ruff format
17+
exclude: examples
2718
- id: mypy
2819
name: mypy
2920
stages: [commit]
@@ -32,13 +23,6 @@ repos:
3223
types: [python]
3324
require_serial: true
3425
exclude: examples
35-
- id: pylint
36-
name: pylint
37-
stages: [commit]
38-
language: system
39-
entry: poetry run pylint
40-
types: [python]
41-
exclude: examples
4226
- id: sort-all
4327
name: sort-all
4428
stages: [commit]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ It is [nearly] production-ready, and gives you the following:
3232

3333
## Requirements
3434

35-
Python 3.6+
35+
Python 3.8+
3636

3737
## Installation
3838

bakery/__init__.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
"""Bakery house."""
22

3-
from typing import Optional
3+
from __future__ import annotations
44

55
from .stuff import BakeryLogger, DefaultLogger
66

7+
logger: BakeryLogger | None = DefaultLogger()
78

8-
logger: Optional[BakeryLogger] = DefaultLogger()
9-
9+
# ruff: noqa: F403, E402
1010
from .bakery import *
1111
from .baking import *
1212
from .cake import *
1313
from .piece_of_cake import *
1414
from .stuff import *
1515

16-
16+
# ruff: noqa: F405, PLE0604
1717
__all__ = [
18-
*bakery.__all__, # type: ignore # pylint: disable=undefined-variable
19-
*baking.__all__, # type: ignore # pylint: disable=undefined-variable
20-
*cake.__all__, # type: ignore # pylint: disable=undefined-variable
21-
*piece_of_cake.__all__, # type: ignore # pylint: disable=undefined-variable
22-
*stuff.__all__, # type: ignore # pylint: disable=undefined-variable
18+
*bakery.__all__, # type: ignore[name-defined]
19+
*baking.__all__, # type: ignore[name-defined]
20+
*cake.__all__, # type: ignore[name-defined]
21+
*piece_of_cake.__all__, # type: ignore[name-defined]
22+
*stuff.__all__, # type: ignore[name-defined]
2323
]

bakery/bakery.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,31 @@
33
Opened around the clock.
44
"""
55

6+
from __future__ import annotations
7+
68
__all__ = ["Bakery"]
79

8-
from typing import Any, Dict, List, Optional, Type, TypeVar, Union
10+
from typing import Any, TypeVar
911

1012
from .baking import bake, unbake
1113
from .cake import Cake
12-
from .stuff import _LOGGER as logger
14+
from .stuff import _LOGGER as logger # noqa: N811
1315
from .stuff import Cakeable, is_cake
1416

15-
16-
T = TypeVar('T', bound='Bakery')
17+
T = TypeVar("T", bound="Bakery")
1718

1819

1920
class Bakery:
2021
"""Your bakery."""
2122

2223
__bakery_visitors__: int
23-
__bakery_items__: Dict[str, Cakeable[Any]]
24+
__bakery_items__: dict[str, Cakeable[Any]]
2425

2526
async def __aenter__(self: T) -> T:
2627
"""Open up your real bakery."""
2728
return await type(self).aopen()
2829

29-
async def __aexit__(self, *_args: Any) -> None:
30+
async def __aexit__(self, *_args: object) -> None:
3031
"""Close up bakery.
3132
3233
Unbake all cakes.
@@ -42,13 +43,14 @@ def __getattribute__(self, attr: str) -> Any:
4243

4344
def __init_subclass__(cls, **kwargs: Any) -> None:
4445
"""Initialize bakery subclass."""
45-
bakery_items: Dict[str, Cakeable[Any]] = {}
46+
bakery_items: dict[str, Cakeable[Any]] = {}
4647
# Do filter __dict__, because iterating
4748
# over __annotations__ forces to annotate
4849
# every cake
49-
for cake_name, cake in cls.__dict__.items():
50+
for cake_name, _cake in cls.__dict__.items():
5051
if cake_name.startswith("__") and cake_name.endswith("__"):
5152
continue
53+
cake = _cake
5254
if not is_cake(cake):
5355
# wrap value into cake
5456
cake = Cake(cake)
@@ -60,9 +62,8 @@ def __init_subclass__(cls, **kwargs: Any) -> None:
6062
cls.__bakery_visitors__ = 0
6163

6264
@classmethod
63-
async def aopen(cls: Type[T]) -> T:
65+
async def aopen(cls: type[T]) -> T:
6466
"""Open bakery."""
65-
6667
if cls.__bakery_visitors__:
6768
cls.__bakery_visitors__ += 1
6869
# no concurrency yet (like aopen/aopen/aopen)
@@ -71,31 +72,25 @@ async def aopen(cls: Type[T]) -> T:
7172

7273
cls.__bakery_visitors__ += 1
7374

74-
exception: Optional[Union[Exception, BaseException]] = None
75-
7675
# let's bake all your cakes
77-
cake: Cakeable[Any]
78-
for cake in cls.__bakery_items__.values():
79-
try:
76+
cake: Cakeable | None = None
77+
try:
78+
for cake in cls.__bakery_items__.values():
8079
await bake(cake)
81-
except (Exception, BaseException) as exc: # pylint: disable=broad-except
82-
exception = exc
83-
logger.error(f'{cake} cannot be baked: {exc}')
84-
break
85-
86-
if exception:
80+
except (Exception, BaseException) as exc:
81+
logger.error(f"{cake} cannot be baked: {exc}")
8782
await cls.aclose()
88-
raise exception
83+
raise exc from None
8984

9085
logger.debug(f"Bakery '{cls}' is opened. Welcome!")
9186
return cls()
9287

9388
@classmethod
9489
async def aclose(
9590
cls,
96-
exc_type: Optional[type] = None,
97-
exc_value: Optional[Exception] = None,
98-
traceback: Optional[Any] = None,
91+
exc_type: type | None = None,
92+
exc_value: Exception | None = None,
93+
traceback: Any | None = None,
9994
) -> None:
10095
"""Close bakery."""
10196
cls.__bakery_visitors__ -= 1
@@ -104,22 +99,22 @@ async def aclose(
10499
logger.debug(f"Bakery '{cls}' is working till the last visitor!")
105100
return
106101

107-
exceptions: List[Union[Exception, BaseException]] = []
102+
exceptions: list[Exception | BaseException] = []
108103
item: Cakeable[Any]
109104
for item in reversed( # it's important to unbake in reverse order
110105
# dict views are reversible since 3.8
111106
# https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects
112-
list(cls.__bakery_items__.values())
107+
list(cls.__bakery_items__.values()),
113108
):
114109
try:
115110
await unbake(item, exc_type, exc_value, traceback)
116-
except (Exception, BaseException) as exc: # pylint: disable=broad-except
111+
except (Exception, BaseException) as exc: # noqa: PERF203
117112
exceptions.append(exc)
118113

119114
logger.debug(f"Bakery '{cls}' is closed.")
120115

121116
if exceptions:
122-
# For now raise the first exception occured.
117+
# For now raise the first exception occurred.
123118
# Use trio MultiError (inception) on demand
124119
# https://github.com/python-trio/trio/blob/v0.21.0/trio/_core/_multierror.py#L154
125120
# or use exception groups (python 3.11)

bakery/bakery.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# isort: skip_file
2-
from typing import Any, Dict, Type, TypeVar
2+
from typing import Any, TypeVar
33

44
from .stuff import Cakeable
55

6-
T = TypeVar('T', bound='Bakery')
6+
T = TypeVar("T", bound=Bakery) # noqa: PYI001
77

88
class Bakery:
99
__bakery_visitors__: int
10-
__bakery_items__: Dict[str, Cakeable[Any]]
10+
__bakery_items__: dict[str, Cakeable[Any]]
1111
async def __aenter__(self: T) -> T: ...
12-
async def __aexit__(self, *_args: Any) -> None: ...
12+
async def __aexit__(self, *_args: object) -> None: ...
1313
@classmethod
14-
async def aopen(cls: Type[T]) -> T: ...
14+
async def aopen(cls: type[T]) -> T: ...
1515
@classmethod
1616
async def aclose(
1717
cls,

0 commit comments

Comments
 (0)