Skip to content

Commit e2281e3

Browse files
committed
fix code quality
1 parent e2f69bf commit e2281e3

8 files changed

Lines changed: 49 additions & 30 deletions

File tree

src/sap_cloud_sdk/core/odata/_async_transport.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,21 @@ async def request(
9191
if method.upper() in MUTATING_METHODS and self._csrf_enabled:
9292
extra[CSRF_HEADER] = await self._get_csrf_token()
9393
try:
94-
return await self._execute(method, path, params=params, json=json, extra_headers=extra)
94+
return await self._execute(
95+
method, path, params=params, json=json, extra_headers=extra
96+
)
9597
except ODataAuthError as exc:
9698
if exc.status_code == 403:
9799
await self._invalidate_csrf_token()
98100
extra[CSRF_HEADER] = await self._get_csrf_token()
99-
return await self._execute(method, path, params=params, json=json, extra_headers=extra)
101+
return await self._execute(
102+
method, path, params=params, json=json, extra_headers=extra
103+
)
100104
raise
101105

102-
return await self._execute(method, path, params=params, json=json, extra_headers=extra)
106+
return await self._execute(
107+
method, path, params=params, json=json, extra_headers=extra
108+
)
103109

104110
def absolute_url(self, path: str) -> str:
105111
return self._base_url + "/" + path.lstrip("/")

src/sap_cloud_sdk/core/odata/_csrf.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
import requests as _requests
99

10-
from sap_cloud_sdk.core.odata._constants import CSRF_FETCH_TIMEOUT, CSRF_FETCH_VALUE, CSRF_HEADER
10+
from sap_cloud_sdk.core.odata._constants import (
11+
CSRF_FETCH_TIMEOUT,
12+
CSRF_FETCH_VALUE,
13+
CSRF_HEADER,
14+
)
1115
from sap_cloud_sdk.core.odata.exceptions import ODataCsrfError
1216

1317
if TYPE_CHECKING:

src/sap_cloud_sdk/core/odata/_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from dataclasses import dataclass, field
5+
from dataclasses import dataclass
66
from typing import Any, ClassVar
77

88

src/sap_cloud_sdk/core/odata/_query.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,7 @@ def to_params(self) -> dict[str, str]:
133133
if self._filter is not None:
134134
params[QUERY_FILTER] = str(self._filter)
135135
if self._orderby:
136-
params[QUERY_ORDERBY] = ",".join(
137-
f"{f} {d.value}" for f, d in self._orderby
138-
)
136+
params[QUERY_ORDERBY] = ",".join(f"{f} {d.value}" for f, d in self._orderby)
139137
if self._top is not None:
140138
params[QUERY_TOP] = str(self._top)
141139
if self._skip is not None:

src/sap_cloud_sdk/core/odata/_request_builders.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
import dataclasses
6-
from typing import Any, Generic, Iterator, TypeVar, TYPE_CHECKING
6+
from typing import Any, Generic, Iterator, TypeVar, TYPE_CHECKING, cast
77

88
from sap_cloud_sdk.core.odata._constants import (
99
DELETE,
@@ -14,7 +14,10 @@
1414
PUT,
1515
)
1616
from sap_cloud_sdk.core.odata._query import OrderDirection, StructuredQuery
17-
from sap_cloud_sdk.core.odata._response import deserialize_collection, deserialize_single
17+
from sap_cloud_sdk.core.odata._response import (
18+
deserialize_collection,
19+
deserialize_single,
20+
)
1821
from sap_cloud_sdk.core.odata._pagination import ODataPageIterator
1922

2023
if TYPE_CHECKING:
@@ -57,9 +60,7 @@ class GetAllRequestBuilder(Generic[T]):
5760
)
5861
"""
5962

60-
def __init__(
61-
self, transport: "ODataHttpTransport", entity_type: type[T]
62-
) -> None:
63+
def __init__(self, transport: "ODataHttpTransport", entity_type: type[T]) -> None:
6364
self._transport = transport
6465
self._entity_type = entity_type
6566
self._query = StructuredQuery()
@@ -105,10 +106,13 @@ def iterate_pages(self) -> Iterator[list[T]]:
105106
params = self._query.to_params()
106107
if params:
107108
from urllib.parse import urlencode
109+
108110
first_url += "?" + urlencode(params)
109111

110112
iterator = ODataPageIterator(
111-
fetch_page=lambda url: self._transport.request(GET, _strip_base(url, self._transport._base_url)),
113+
fetch_page=lambda url: self._transport.request(
114+
GET, _strip_base(url, self._transport._base_url)
115+
),
112116
entity_type=self._entity_type,
113117
first_url=first_url,
114118
)
@@ -124,7 +128,7 @@ def _strip_base(url: str, base_url: str) -> str:
124128
"""Strip *base_url* prefix from *url* to get a relative path."""
125129
prefix = base_url + "/"
126130
if url.startswith(prefix):
127-
return url[len(prefix):]
131+
return url[len(prefix) :]
128132
return url
129133

130134

@@ -160,17 +164,16 @@ def execute(self) -> T:
160164
class CreateRequestBuilder(Generic[T]):
161165
"""Builder for OData entity creation (POST)."""
162166

163-
def __init__(
164-
self, transport: "ODataHttpTransport", entity: T
165-
) -> None:
167+
def __init__(self, transport: "ODataHttpTransport", entity: T) -> None:
166168
self._transport = transport
167169
self._entity = entity
168170

169171
def execute(self) -> T:
170172
"""Create the entity and return the server response as the same type."""
171173
entity_type = type(self._entity)
172174
path = _entity_set_path(entity_type)
173-
body = self._entity.to_dict() if hasattr(self._entity, "to_dict") else dataclasses.asdict(self._entity) # type: ignore[arg-type]
175+
e: Any = cast(Any, self._entity)
176+
body = e.to_dict() if hasattr(e, "to_dict") else dataclasses.asdict(e)
174177
data = self._transport.request(POST, path, json=body)
175178
return deserialize_single(data, entity_type)
176179

@@ -205,7 +208,8 @@ def execute(self) -> T:
205208
)
206209
key = {k: getattr(self._entity, k) for k in key_fields}
207210
path = _entity_set_path(entity_type) + _build_key_segment(key)
208-
body = self._entity.to_dict() if hasattr(self._entity, "to_dict") else dataclasses.asdict(self._entity) # type: ignore[arg-type]
211+
e: Any = cast(Any, self._entity)
212+
body = e.to_dict() if hasattr(e, "to_dict") else dataclasses.asdict(e)
209213
method = PUT if self._use_put else PATCH
210214
extra: dict[str, str] = {}
211215
if self._etag is not None:

src/sap_cloud_sdk/core/odata/_response.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ def deserialize_single(data: dict[str, Any], entity_type: type[T]) -> T:
2323
f"{entity_type!r} is not a dataclass — cannot deserialize"
2424
)
2525
try:
26-
payload = data.get(RESPONSE_VALUE, data) if isinstance(data.get(RESPONSE_VALUE), dict) else data
26+
payload = (
27+
data.get(RESPONSE_VALUE, data)
28+
if isinstance(data.get(RESPONSE_VALUE), dict)
29+
else data
30+
)
2731
known = {f.name for f in dataclasses.fields(entity_type)} # type: ignore[arg-type]
2832
kwargs = {k: v for k, v in payload.items() if k in known}
2933
return entity_type(**kwargs) # type: ignore[call-arg]

src/sap_cloud_sdk/core/odata/_transport.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,21 @@ def request(
9292
if method.upper() in MUTATING_METHODS and self._csrf is not None:
9393
extra[CSRF_HEADER] = self._csrf.get()
9494
try:
95-
return self._execute(method, path, params=params, json=json, extra_headers=extra)
95+
return self._execute(
96+
method, path, params=params, json=json, extra_headers=extra
97+
)
9698
except ODataAuthError as exc:
9799
if exc.status_code == 403:
98100
self._csrf.invalidate()
99101
extra[CSRF_HEADER] = self._csrf.get()
100-
return self._execute(method, path, params=params, json=json, extra_headers=extra)
102+
return self._execute(
103+
method, path, params=params, json=json, extra_headers=extra
104+
)
101105
raise
102106

103-
return self._execute(method, path, params=params, json=json, extra_headers=extra)
107+
return self._execute(
108+
method, path, params=params, json=json, extra_headers=extra
109+
)
104110

105111
def absolute_url(self, path: str) -> str:
106112
"""Return the full URL for *path* relative to the service base."""

src/sap_cloud_sdk/core/odata/exceptions.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING
6-
7-
if TYPE_CHECKING:
8-
import requests
5+
from typing import Any
96

107

118
class ODataError(Exception):
@@ -15,8 +12,8 @@ class ODataError(Exception):
1512
class ODataRequestError(ODataError):
1613
"""HTTP-level error from an OData service (non-2xx response)."""
1714

18-
def __init__(self, response: "requests.Response") -> None:
19-
self.status_code = response.status_code
15+
def __init__(self, response: Any) -> None:
16+
self.status_code: int = response.status_code
2017
self.response = response
2118
try:
2219
body = response.json()

0 commit comments

Comments
 (0)