Skip to content

Commit 1b13503

Browse files
authored
Merge pull request #115 from eadwinCode/ruff_linting
Ruff linting and mypy upgrade
2 parents 819e91e + 7580494 commit 1b13503

File tree

255 files changed

+1592
-1389
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

255 files changed

+1592
-1389
lines changed

.github/workflows/test_full.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- name: Install Dependencies
2424
run: flit install --symlink
2525
- name: Test
26-
run: pytest tests
26+
run: pytest
2727

2828
codestyle:
2929
runs-on: ubuntu-latest
@@ -38,10 +38,8 @@ jobs:
3838
- name: Install Dependencies
3939
run: flit install --symlink
4040
- name: Black
41-
run: black --check ellar tests
42-
- name: isort
43-
run: isort --check ellar tests
44-
- name: Flake8
45-
run: flake8 ellar tests
41+
run: black --check ellar tests examples
42+
- name: Ruff Linting Check
43+
run: ruff check ellar tests examples
4644
- name: mypy
4745
run: mypy ellar

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,4 @@ dist
126126
test.py
127127

128128
docs/site
129-
site/
129+
site/

Makefile

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,19 @@ install-full: ## Install dependencies
1818
pre-commit install -f
1919

2020
lint: ## Run code linters
21-
black --check ellar tests
22-
isort --check ellar tests
23-
autoflake --remove-unused-variables --remove-unused-variables -r ellar tests
24-
flake8 ellar tests
21+
black --check ellar tests examples
22+
ruff check ellar tests examples
2523
mypy ellar
2624

2725
fmt format: ## Run code formatters
28-
black ellar tests
29-
isort ellar tests
26+
black ellar tests examples
27+
ruff check --fix ellar tests examples
3028

3129
test: ## Run tests
32-
pytest tests
30+
pytest
3331

3432
test-cov: ## Run tests with coverage
35-
pytest --cov=ellar --cov-report term-missing tests
33+
pytest --cov=ellar --cov-report term-missing
3634

3735
doc-deploy: ## Run Deploy Documentation
3836
make clean

ellar/auth/guard.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import typing as t
22
from functools import partial
33

4-
from starlette import status
5-
64
from ellar.common import APIException, GuardCanActivate, IExecutionContext
75
from ellar.di import injectable
6+
from starlette import status
87

98
from .constants import POLICY_KEYS
109
from .policy import BasePolicyHandler, PolicyType

ellar/auth/handlers/schemes/base.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import typing as t
22
from abc import ABC, abstractmethod
33

4-
from starlette.exceptions import HTTPException
5-
from starlette.status import HTTP_401_UNAUTHORIZED
6-
74
from ellar.common.exceptions import APIException
85
from ellar.common.interfaces import IHostContext
96
from ellar.common.serializer.guard import (
107
HTTPAuthorizationCredentials,
118
HTTPBasicCredentials,
129
)
10+
from starlette.exceptions import HTTPException
11+
from starlette.status import HTTP_401_UNAUTHORIZED
1312

1413
if t.TYPE_CHECKING: # pragma: no cover
1514
from ellar.core.connection import HTTPConnection

ellar/auth/handlers/schemes/http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def _get_credentials(self, connection: "HTTPConnection") -> HTTPBasicCredentials
9191

9292
if not separator:
9393
self._not_unauthorized_exception("Invalid authentication credentials")
94-
return HTTPBasicCredentials(username=username, password=password)
94+
return HTTPBasicCredentials(username=username, password=password) # type: ignore[arg-type]
9595

9696

9797
class HttpDigestAuth(HttpBearerAuth, ABC):

ellar/auth/services/auth_schemes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ def add_authentication(
2020
def find_authentication_scheme(self, scheme: str) -> AuthenticationHandlerType:
2121
try:
2222
return self._authentication_schemes[scheme]
23-
except KeyError:
23+
except KeyError as ex:
2424
raise RuntimeError(
2525
f'No Authentication Scheme found with the name:"{scheme}"'
26-
)
26+
) from ex
2727

2828
def get_authentication_schemes(
2929
self,

ellar/auth/session/strategy.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
try:
22
import itsdangerous
3-
except Exception: # pragma: no cover
3+
except Exception as ex: # pragma: no cover
44
raise RuntimeError(
55
"SessionClientStrategy requires itsdangerous package installed. Run `pip install itsdangerous`"
6-
)
6+
) from ex
77

88
import json
99
import typing as t
1010
from base64 import b64decode, b64encode
1111

12-
import itsdangerous
13-
from itsdangerous import BadSignature
14-
1512
from ellar.core import Config
1613
from ellar.di import injectable
14+
from itsdangerous import BadSignature
1715

1816
from .cookie_dict import SessionCookieObject
1917
from .interface import ISessionStrategy
@@ -26,12 +24,12 @@ def __init__(self, config: Config) -> None:
2624
self._signer = itsdangerous.TimestampSigner(str(config.SECRET_KEY))
2725
self.config = config
2826
self._session_config = SessionCookieOption(
29-
NAME=config.SESSION_COOKIE_NAME,
27+
NAME=config.SESSION_COOKIE_NAME or "",
3028
DOMAIN=config.SESSION_COOKIE_DOMAIN,
3129
PATH=config.SESSION_COOKIE_PATH or "/",
32-
HTTPONLY=config.SESSION_COOKIE_HTTPONLY,
33-
SECURE=config.SESSION_COOKIE_SECURE,
34-
SAME_SITE=config.SESSION_COOKIE_SAME_SITE,
30+
HTTPONLY=config.SESSION_COOKIE_HTTPONLY or False,
31+
SECURE=config.SESSION_COOKIE_SECURE or False,
32+
SAME_SITE=config.SESSION_COOKIE_SAME_SITE or "none",
3533
MAX_AGE=config.SESSION_COOKIE_MAX_AGE,
3634
)
3735

ellar/cache/backends/base.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ class _BasePylibMemcachedCacheSync(BaseCacheBackend, ABC):
1111
_cache_client: t.Any
1212

1313
@make_key_decorator
14-
def get(self, key: str, version: str = None) -> t.Any:
14+
def get(self, key: str, version: t.Optional[str] = None) -> t.Any:
1515
return self._cache_client.get(key)
1616

1717
@make_key_decorator_and_validate
1818
def set(
1919
self,
2020
key: str,
2121
value: t.Any,
22-
ttl: t.Union[float, int] = None,
23-
version: str = None,
22+
ttl: t.Union[float, int, None] = None,
23+
version: t.Optional[str] = None,
2424
) -> bool:
2525
result = self._cache_client.set(key, value, int(self.get_backend_ttl(ttl)))
2626
if not result:
@@ -31,24 +31,27 @@ def set(
3131
return bool(result)
3232

3333
@make_key_decorator
34-
def delete(self, key: str, version: str = None) -> bool:
34+
def delete(self, key: str, version: t.Optional[str] = None) -> bool:
3535
result = self._cache_client.delete(key)
3636
return bool(result)
3737

3838
@make_key_decorator
3939
def touch(
40-
self, key: str, ttl: t.Union[float, int] = None, version: str = None
40+
self,
41+
key: str,
42+
ttl: t.Union[float, int, None] = None,
43+
version: t.Optional[str] = None,
4144
) -> bool:
4245
result = self._cache_client.touch(key, self.get_backend_ttl(ttl))
4346
return bool(result)
4447

4548
@make_key_decorator
46-
def incr(self, key: str, delta: int = 1, version: str = None) -> int:
49+
def incr(self, key: str, delta: int = 1, version: t.Optional[str] = None) -> int:
4750
result = self._cache_client.incr(key, abs(delta))
4851
return t.cast(int, result)
4952

5053
@make_key_decorator
51-
def decr(self, key: str, delta: int = 1, version: str = None) -> int:
54+
def decr(self, key: str, delta: int = 1, version: t.Optional[str] = None) -> int:
5255
result = self._cache_client.decr(key, abs(delta))
5356
return t.cast(int, result)
5457

@@ -62,7 +65,9 @@ def clear(self) -> None:
6265
class BasePylibMemcachedCache(_BasePylibMemcachedCacheSync):
6366
MEMCACHE_CLIENT: t.Type
6467

65-
def __init__(self, servers: t.List[str], options: t.Dict = None, **kwargs: t.Any):
68+
def __init__(
69+
self, servers: t.List[str], options: t.Optional[t.Dict] = None, **kwargs: t.Any
70+
):
6671
super().__init__(**kwargs)
6772
self._servers = servers
6873

@@ -83,25 +88,28 @@ def _cache_client(self) -> t.Any:
8388
async def executor(self, func: t.Callable, *args: t.Any, **kwargs: t.Any) -> t.Any:
8489
return await run_in_threadpool(func, *args, **kwargs)
8590

86-
async def get_async(self, key: str, version: str = None) -> t.Any:
91+
async def get_async(self, key: str, version: t.Optional[str] = None) -> t.Any:
8792
return await self.executor(self.get, key, version=version)
8893

8994
async def set_async(
9095
self,
9196
key: str,
9297
value: t.Any,
93-
ttl: t.Union[float, int] = None,
94-
version: str = None,
98+
ttl: t.Union[float, int, None] = None,
99+
version: t.Optional[str] = None,
95100
) -> bool:
96101
result = await self.executor(self.set, key, value, ttl=ttl, version=version)
97102
return bool(result)
98103

99-
async def delete_async(self, key: str, version: str = None) -> bool:
104+
async def delete_async(self, key: str, version: t.Optional[str] = None) -> bool:
100105
result = await self.executor(self.delete, key, version=version)
101106
return bool(result)
102107

103108
async def touch_async(
104-
self, key: str, ttl: t.Union[float, int] = None, version: str = None
109+
self,
110+
key: str,
111+
ttl: t.Union[float, int, None] = None,
112+
version: t.Optional[str] = None,
105113
) -> bool:
106114
result = await self.executor(self.touch, key, ttl=ttl, version=version)
107115
return bool(result)
@@ -117,10 +125,14 @@ def validate_key(self, key: str) -> None:
117125
super().validate_key(key)
118126
self._memcache_key_warnings(key)
119127

120-
async def incr_async(self, key: str, delta: int = 1, version: str = None) -> int:
128+
async def incr_async(
129+
self, key: str, delta: int = 1, version: t.Optional[str] = None
130+
) -> int:
121131
res = await self.executor(self.incr, key, delta=delta, version=version)
122132
return t.cast(int, res)
123133

124-
async def decr_async(self, key: str, delta: int = 1, version: str = None) -> int:
134+
async def decr_async(
135+
self, key: str, delta: int = 1, version: t.Optional[str] = None
136+
) -> int:
125137
res = await self.executor(self.decr, key, delta=delta, version=version)
126138
return t.cast(int, res)

ellar/cache/backends/local_cache.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from collections import OrderedDict
66

77
from anyio import Lock
8-
98
from ellar.common.helper.event_loop import get_or_create_eventloop
109

1110
from ..interface import IBaseCacheBackendAsync
@@ -17,34 +16,37 @@ class _LocalMemCacheBackendSync(IBaseCacheBackendAsync, ABC):
1716
def _async_executor(self, func: t.Awaitable) -> t.Any:
1817
return get_or_create_eventloop().run_until_complete(func)
1918

20-
def get(self, key: str, version: str = None) -> t.Any:
19+
def get(self, key: str, version: t.Optional[str] = None) -> t.Any:
2120
return self._async_executor(self.get_async(key, version=version))
2221

23-
def delete(self, key: str, version: str = None) -> bool:
22+
def delete(self, key: str, version: t.Optional[str] = None) -> bool:
2423
res = self._async_executor(self.delete_async(key, version=version))
2524
return bool(res)
2625

2726
def set(
2827
self,
2928
key: str,
3029
value: t.Any,
31-
ttl: t.Union[float, int] = None,
32-
version: str = None,
30+
ttl: t.Union[float, int, None] = None,
31+
version: t.Optional[str] = None,
3332
) -> bool:
3433
res = self._async_executor(self.set_async(key, value, ttl=ttl, version=version))
3534
return bool(res)
3635

3736
def touch(
38-
self, key: str, ttl: t.Union[float, int] = None, version: str = None
37+
self,
38+
key: str,
39+
ttl: t.Union[float, int, None] = None,
40+
version: t.Optional[str] = None,
3941
) -> bool:
4042
res = self._async_executor(self.touch_async(key, ttl=ttl, version=version))
4143
return bool(res)
4244

43-
def incr(self, key: str, delta: int = 1, version: str = None) -> int:
45+
def incr(self, key: str, delta: int = 1, version: t.Optional[str] = None) -> int:
4446
res = self._async_executor(self.incr_async(key, delta=delta, version=version))
4547
return t.cast(int, res)
4648

47-
def decr(self, key: str, delta: int = 1, version: str = None) -> int:
49+
def decr(self, key: str, delta: int = 1, version: t.Optional[str] = None) -> int:
4850
res = self._async_executor(self.decr_async(key, delta=delta, version=version))
4951
return t.cast(int, res)
5052

@@ -59,7 +61,7 @@ def __init__(self, **kwargs: t.Any) -> None:
5961
self._lock = Lock()
6062

6163
@make_key_decorator
62-
async def get_async(self, key: str, version: str = None) -> t.Any:
64+
async def get_async(self, key: str, version: t.Optional[str] = None) -> t.Any:
6365
async with self._lock:
6466
if self._has_expired(key):
6567
await self._delete(key)
@@ -77,7 +79,7 @@ async def _delete(self, key: str) -> bool:
7779
return True
7880

7981
@make_key_decorator
80-
async def delete_async(self, key: str, version: str = None) -> bool:
82+
async def delete_async(self, key: str, version: t.Optional[str] = None) -> bool:
8183
async with self._lock:
8284
return await self._delete(key)
8385

@@ -86,8 +88,8 @@ async def set_async(
8688
self,
8789
key: str,
8890
value: t.Any,
89-
ttl: t.Union[float, int] = None,
90-
version: str = None,
91+
ttl: t.Union[float, int, None] = None,
92+
version: t.Optional[str] = None,
9193
) -> bool:
9294
async with self._lock:
9395
self._cache[key] = pickle.dumps(value, self.pickle_protocol)
@@ -99,7 +101,7 @@ def _has_expired(self, key: str) -> bool:
99101
return exp is not None and exp <= time.time()
100102

101103
@make_key_decorator
102-
async def has_key_async(self, key: str, version: str = None) -> bool:
104+
async def has_key_async(self, key: str, version: t.Optional[str] = None) -> bool:
103105
async with self._lock:
104106
if self._has_expired(key):
105107
await self._delete(key)
@@ -108,7 +110,10 @@ async def has_key_async(self, key: str, version: str = None) -> bool:
108110

109111
@make_key_decorator
110112
async def touch_async(
111-
self, key: str, ttl: t.Union[float, int] = None, version: str = None
113+
self,
114+
key: str,
115+
ttl: t.Union[float, int, None] = None,
116+
version: t.Optional[str] = None,
112117
) -> bool:
113118
async with self._lock:
114119
if self._has_expired(key):
@@ -117,7 +122,7 @@ async def touch_async(
117122
self._expire_track[key] = self.get_backend_ttl(ttl)
118123
return True
119124

120-
def has_key(self, key: str, version: str = None) -> bool:
125+
def has_key(self, key: str, version: t.Optional[str] = None) -> bool:
121126
res = self._async_executor(self.has_key_async(key, version=version))
122127
return bool(res)
123128

@@ -130,15 +135,19 @@ def _incr_decr_action(self, key: str, delta: int) -> int:
130135
return new_value
131136

132137
@make_key_decorator
133-
async def incr_async(self, key: str, delta: int = 1, version: str = None) -> int:
138+
async def incr_async(
139+
self, key: str, delta: int = 1, version: t.Optional[str] = None
140+
) -> int:
134141
async with self._lock:
135142
if self._has_expired(key):
136143
await self._delete(key)
137144
raise ValueError("Key '%s' not found" % key)
138145
return self._incr_decr_action(key, delta)
139146

140147
@make_key_decorator
141-
async def decr_async(self, key: str, delta: int = 1, version: str = None) -> int:
148+
async def decr_async(
149+
self, key: str, delta: int = 1, version: t.Optional[str] = None
150+
) -> int:
142151
async with self._lock:
143152
if self._has_expired(key):
144153
await self._delete(key)

0 commit comments

Comments
 (0)