Skip to content

Commit b0043d0

Browse files
authored
Add new ruff lint rules (#392)
* Add ruff lint rule - C4, I, PIE, RUF, T, UP * Re-run ruff format * Fix typing issue in ApiKeySigner * Fix import order in tests for sub-projects
1 parent 548b3d0 commit b0043d0

File tree

29 files changed

+118
-113
lines changed

29 files changed

+118
-113
lines changed

packages/aws-event-stream/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ build-backend = "hatchling.build"
1717
exclude = [
1818
"tests",
1919
]
20+
21+
[tool.ruff]
22+
src = ["src"]

packages/aws-event-stream/src/aws_event_stream/events.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from dataclasses import dataclass
1616
from io import BytesIO
1717
from struct import pack, unpack
18+
from types import MappingProxyType
1819
from typing import Literal, Self
1920

2021
from smithy_core.aio.interfaces import AsyncByteStream
@@ -610,11 +611,13 @@ class _DecodeUtils:
610611
INT64_BYTE_FORMAT = "!q"
611612

612613
# uint byte size to unpack format
613-
UINT_BYTE_FORMAT: dict[_ArraySize, str] = {
614-
1: UINT8_BYTE_FORMAT,
615-
2: UINT16_BYTE_FORMAT,
616-
4: UINT32_BYTE_FORMAT,
617-
}
614+
UINT_BYTE_FORMAT: Mapping[_ArraySize, str] = MappingProxyType(
615+
{
616+
1: UINT8_BYTE_FORMAT,
617+
2: UINT16_BYTE_FORMAT,
618+
4: UINT32_BYTE_FORMAT,
619+
}
620+
)
618621

619622
@staticmethod
620623
def unpack_uint8(data: BytesLike) -> tuple[int, int]:
@@ -749,20 +752,24 @@ class EventHeaderDecoder(Iterator[tuple[str, HEADER_VALUE]]):
749752

750753
# Maps header type to appropriate unpacking function
751754
# These unpacking functions return the value and the amount unpacked
752-
_HEADER_TYPE_MAP: dict[int, Callable[[BytesLike], tuple[HEADER_VALUE, int]]] = {
753-
# Boolean headers have no data bytes following the type signifier, so they
754-
# can just return static values.
755-
0: lambda b: (True, 0), # boolean_true
756-
1: lambda b: (False, 0), # boolean_false
757-
2: _DecodeUtils.unpack_int8, # byte
758-
3: _DecodeUtils.unpack_int16, # short
759-
4: _DecodeUtils.unpack_int32, # integer
760-
5: _DecodeUtils.unpack_int64, # long
761-
6: _DecodeUtils.unpack_byte_array, # byte_array
762-
7: _DecodeUtils.unpack_utf8_string, # string
763-
8: _DecodeUtils.unpack_timestamp, # timestamp
764-
9: _DecodeUtils.unpack_uuid, # uuid
765-
}
755+
_HEADER_TYPE_MAP: Mapping[int, Callable[[BytesLike], tuple[HEADER_VALUE, int]]] = (
756+
MappingProxyType(
757+
{
758+
# Boolean headers have no data bytes following the type signifier, so they
759+
# can just return static values.
760+
0: lambda b: (True, 0), # boolean_true
761+
1: lambda b: (False, 0), # boolean_false
762+
2: _DecodeUtils.unpack_int8, # byte
763+
3: _DecodeUtils.unpack_int16, # short
764+
4: _DecodeUtils.unpack_int32, # integer
765+
5: _DecodeUtils.unpack_int64, # long
766+
6: _DecodeUtils.unpack_byte_array, # byte_array
767+
7: _DecodeUtils.unpack_utf8_string, # string
768+
8: _DecodeUtils.unpack_timestamp, # timestamp
769+
9: _DecodeUtils.unpack_uuid, # uuid
770+
}
771+
)
772+
)
766773

767774
def __init__(self, header_bytes: BytesLike) -> None:
768775
"""Initialize an event header decoder.

packages/smithy-aws-core/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ build-backend = "hatchling.build"
1616
exclude = [
1717
"tests",
1818
]
19+
20+
[tool.ruff]
21+
src = ["src"]

packages/smithy-core/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ build-backend = "hatchling.build"
1919
exclude = [
2020
"tests",
2121
]
22+
23+
[tool.ruff]
24+
src = ["src"]

packages/smithy-core/src/smithy_core/documents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def _wrap_map(self, value: Mapping[str, DocumentValue]) -> dict[str, "Document"]
230230
member_schema = self._schema.members["value"]
231231
return {k: self._new_document(v, member_schema) for k, v in value.items()}
232232

233-
result: dict[str, "Document"] = {}
233+
result: dict[str, Document] = {}
234234
for k, v in value.items():
235235
result[k] = self._new_document(v, self._schema.members[k])
236236
return result

packages/smithy-core/src/smithy_core/identity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3-
from datetime import datetime, timezone
3+
from datetime import UTC, datetime
44

55
from .interfaces import identity as identity_interface
66
from .utils import ensure_utc
@@ -28,4 +28,4 @@ def is_expired(self) -> bool:
2828
"""Whether the identity is expired."""
2929
if self.expiration is None:
3030
return False
31-
return datetime.now(tz=timezone.utc) >= self.expiration
31+
return datetime.now(tz=UTC) >= self.expiration

packages/smithy-core/src/smithy_core/interceptors.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ def read_before_execution(
148148
the `response`. If multiple `read_before_execution` methods throw exceptions,
149149
the latest will be used and earlier ones will be logged and dropped.
150150
"""
151-
pass
152151

153152
def modify_before_serialization(
154153
self, context: InterceptorContext[Request, None, None, None]
@@ -193,7 +192,6 @@ def read_before_serialization(
193192
If exceptions are thrown by this hook, execution will jump to
194193
`modify_before_completion` with the thrown exception as the `response`.
195194
"""
196-
pass
197195

198196
def read_after_serialization(
199197
self, context: InterceptorContext[Request, None, TransportRequest, None]
@@ -214,7 +212,6 @@ def read_after_serialization(
214212
If exceptions are thrown by this hook, execution will jump to
215213
`modify_before_completion` with the thrown exception as the `response`.
216214
"""
217-
pass
218215

219216
def modify_before_retry_loop(
220217
self, context: InterceptorContext[Request, None, TransportRequest, None]
@@ -259,7 +256,6 @@ def read_before_attempt(
259256
exception as the `response` If multiple `read_before_attempt` methods throw
260257
exceptions, the latest will be used and earlier ones will be logged and dropped.
261258
"""
262-
pass
263259

264260
def modify_before_signing(
265261
self, context: InterceptorContext[Request, None, TransportRequest, None]
@@ -309,7 +305,6 @@ def read_before_signing(
309305
If exceptions are thrown by this hook, execution will jump to
310306
`modify_before_attempt_completion` with the thrown exception as the `response`.
311307
"""
312-
pass
313308

314309
def read_after_signing(
315310
self, context: InterceptorContext[Request, None, TransportRequest, None]
@@ -333,7 +328,6 @@ def read_after_signing(
333328
If exceptions are thrown by this hook, execution will jump to
334329
`modify_before_attempt_completion` with the thrown exception as the `response`.
335330
"""
336-
pass
337331

338332
def modify_before_transmit(
339333
self, context: InterceptorContext[Request, None, TransportRequest, None]
@@ -384,7 +378,6 @@ def read_before_transmit(
384378
If exceptions are thrown by this hook, execution will jump to
385379
`modify_before_attempt_completion` with the thrown exception as the `response`.
386380
"""
387-
pass
388381

389382
def read_after_transmit(
390383
self,
@@ -411,7 +404,6 @@ def read_after_transmit(
411404
If exceptions are thrown by this hook, execution will jump to
412405
`modify_before_attempt_completion` with the thrown exception as the `response`.
413406
"""
414-
pass
415407

416408
def modify_before_deserialization(
417409
self,
@@ -467,7 +459,6 @@ def read_before_deserialization(
467459
If exceptions are thrown by this hook, execution will jump to
468460
`modify_before_attempt_completion` with the thrown exception as the `response`.
469461
"""
470-
pass
471462

472463
def read_after_deserialization(
473464
self,
@@ -495,7 +486,6 @@ def read_after_deserialization(
495486
If exceptions are thrown by this hook, execution will jump to
496487
`modify_before_attempt_completion` with the thrown exception as the `response`.
497488
"""
498-
pass
499489

500490
def modify_before_attempt_completion(
501491
self,
@@ -554,7 +544,6 @@ def read_after_attempt(
554544
execution will then jump to `read_before_attempt`. Otherwise, execution will
555545
jump to `modify_before_completion` with the thrown exception as the `response`.
556546
"""
557-
pass
558547

559548
def modify_before_completion(
560549
self,
@@ -605,4 +594,3 @@ def read_after_execution(
605594
final response. If multiple `read_after_execution` methods throw exceptions,
606595
the latest will be used and earlier ones will be logged and dropped.
607596
"""
608-
pass

packages/smithy-core/src/smithy_core/interfaces/identity.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ def is_expired(self) -> bool:
2525
class IdentityProperties(TypedDict):
2626
"""Properties used to help determine the identity to return."""
2727

28-
...
29-
3028

3129
IdentityPropertiesType = TypeVar("IdentityPropertiesType", bound=IdentityProperties)
3230
IdentityPropertiesType_contra = TypeVar(

packages/smithy-core/src/smithy_core/retries.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,3 @@ def refresh_retry_token_for_retry(
242242

243243
def record_success(self, *, token: retries_interface.RetryToken) -> None:
244244
"""Not used by this retry strategy."""
245-
pass

packages/smithy-core/src/smithy_core/schemas.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def __init__(
5050
if any(_member_props) and not all(_member_props):
5151
raise SmithyException(
5252
"If any member property is set, all member properties must be set. "
53-
f"member_name: {repr(id.member)}, member_target: "
54-
f"{repr(member_target)}, member_index: {repr(member_index)}"
53+
f"member_name: {id.member!r}, member_target: "
54+
f"{member_target!r}, member_index: {member_index!r}"
5555
)
5656

5757
# setattr is required because the class is frozen
@@ -67,7 +67,7 @@ def __init__(
6767

6868
if members:
6969
if isinstance(members, list):
70-
m: dict[str, "Schema"] = {}
70+
m: dict[str, Schema] = {}
7171
for member in members:
7272
m[member.expect_member_name()] = member
7373
members = m
@@ -140,7 +140,7 @@ def collection(
140140
constructor, this is a dict of member names to a simplified dict containing
141141
only ``traits`` and a ``target``. Member schemas will be generated from this.
142142
"""
143-
struct_members: dict[str, "Schema"] = {}
143+
struct_members: dict[str, Schema] = {}
144144
if members:
145145
for k in members.keys():
146146
struct_members[k] = cls.member(

0 commit comments

Comments
 (0)