Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use base ruff config for all projects #462

Merged
merged 3 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ test-protocols: ## Generates and runs the restJson1 protocol tests.

lint-py: ## Runs linters and formatters on the python packages.
uv run docformatter packages --in-place || true
uv run ruff check packages --fix
uv run ruff format packages
uv run ruff check packages --fix --config pyproject.toml
uv run ruff format packages --config pyproject.toml


check-py: ## Runs checks (formatting, lints, type-checking) on the python packages.
uv run docformatter packages
uv run ruff check packages
uv run ruff format --check
uv run ruff check packages --config pyproject.toml
uv run ruff format --check --config pyproject.toml
uv run pyright packages


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
from smithy_core.schemas import Schema

from smithy_core.traits import EventPayloadTrait

INITIAL_REQUEST_EVENT_TYPE = "initial-request"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
from smithy_core.schemas import Schema
from smithy_core.shapes import ShapeType
from smithy_core.traits import EventHeaderTrait
from smithy_core.utils import expect_type

from ..events import HEADERS_DICT, Event
Expand All @@ -20,7 +21,6 @@
INITIAL_RESPONSE_EVENT_TYPE,
get_payload_member,
)
from smithy_core.traits import EventHeaderTrait

logger = logging.getLogger(__name__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
SpecificShapeSerializer,
)
from smithy_core.shapes import ShapeType
from smithy_core.traits import ErrorTrait, EventHeaderTrait, MediaTypeTrait

from ..events import EventHeaderEncoder, EventMessage, HEADER_VALUE, Short, Byte, Long
from ..events import HEADER_VALUE, Byte, EventHeaderEncoder, EventMessage, Long, Short
from ..exceptions import InvalidHeaderValue
from . import (
INITIAL_REQUEST_EVENT_TYPE,
INITIAL_RESPONSE_EVENT_TYPE,
get_payload_member,
)
from smithy_core.traits import ErrorTrait, EventHeaderTrait, MediaTypeTrait

logger = logging.getLogger(__name__)

Expand Down
21 changes: 9 additions & 12 deletions packages/aws-event-stream/src/aws_event_stream/aio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,27 @@
import asyncio
import logging
from collections.abc import Callable
from typing import Protocol

from smithy_core.aio.interfaces import AsyncByteStream
from smithy_core.aio.interfaces import AsyncByteStream, AsyncWriter
from smithy_core.aio.interfaces.eventstream import EventPublisher, EventReceiver
from smithy_core.codecs import Codec
from smithy_core.deserializers import DeserializeableShape, ShapeDeserializer
from smithy_core.exceptions import ExpectationNotMetException
from smithy_core.serializers import SerializeableShape
from smithy_core.aio.interfaces.eventstream import EventPublisher, EventReceiver
from smithy_core.aio.interfaces import AsyncWriter

from .._private.serializers import EventSerializer as _EventSerializer
from .._private.deserializers import EventDeserializer as _EventDeserializer
from .._private.serializers import EventSerializer as _EventSerializer
from ..events import Event, EventHeaderEncoder, EventMessage
from ..exceptions import EventError

from typing import Protocol

logger = logging.getLogger(__name__)


class EventSigner(Protocol):
"""A signer to manage credentials and EventMessages for an Event Stream lifecyle."""

def sign_event(
async def sign_event(
self,
*,
event_message: EventMessage,
Expand All @@ -50,7 +48,7 @@ def __init__(

async def send(self, event: E) -> None:
if self._closed:
raise IOError("Attempted to write to closed stream.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers also wondering, this is related to: https://docs.astral.sh/ruff/rules/os-error-alias/

raise OSError("Attempted to write to closed stream.")
logger.debug("Preparing to publish event: %s", event)
event.serialize(self._serializer)
result = self._serializer.get_result()
Expand All @@ -60,19 +58,18 @@ async def send(self, event: E) -> None:
)
if self._signer is not None:
encoder = self._serializer.event_header_encoder_cls
result = await self._signer.sign_event( # type: ignore
result = await self._signer.sign_event(
event_message=result,
event_encoder_cls=encoder,
)

assert isinstance(result, EventMessage)
encoded_result = result.encode()
try:
logger.debug("Publishing serialized event: %s", result)
await self._writer.write(encoded_result)
except Exception as e:
await self.close()
raise IOError("Failed to write to stream.") from e
raise OSError("Failed to write to stream.") from e

async def close(self) -> None:
if self._closed:
Expand Down Expand Up @@ -111,7 +108,7 @@ async def receive(self) -> E | None:
except Exception as e:
await self.close()
if not isinstance(e, EventError):
raise IOError("Failed to read from stream.") from e
raise OSError("Failed to read from stream.") from e
raise

if event is None:
Expand Down
5 changes: 2 additions & 3 deletions packages/aws-event-stream/tests/unit/_private/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dataclasses import dataclass
from typing import Any, ClassVar, Literal, Self

from aws_event_stream.events import Byte, EventMessage, Long, Short
from smithy_core.deserializers import ShapeDeserializer
from smithy_core.exceptions import SmithyException
from smithy_core.prelude import (
Expand All @@ -20,15 +21,13 @@
from smithy_core.serializers import ShapeSerializer
from smithy_core.shapes import ShapeID, ShapeType
from smithy_core.traits import (
ErrorTrait,
EventHeaderTrait,
EventPayloadTrait,
ErrorTrait,
RequiredTrait,
StreamingTrait,
)

from aws_event_stream.events import Byte, EventMessage, Long, Short

EVENT_HEADER_TRAIT = EventHeaderTrait()
EVENT_PAYLOAD_TRAIT = EventPayloadTrait()
ERROR_TRAIT = ErrorTrait("client")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
from typing import Any

import pytest
from smithy_core.aio.types import AsyncBytesReader
from smithy_core.deserializers import DeserializeableShape
from smithy_json import JSONCodec

from aws_event_stream.aio import AWSEventReceiver
from aws_event_stream._private.deserializers import EventDeserializer
from aws_event_stream.aio import AWSEventReceiver
from aws_event_stream.events import Event, EventMessage
from aws_event_stream.exceptions import UnmodeledEventError
from smithy_core.aio.types import AsyncBytesReader
from smithy_core.deserializers import DeserializeableShape
from smithy_json import JSONCodec

from . import (
EVENT_STREAM_SERDE_CASES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
from typing import Any

import pytest
from smithy_core.serializers import SerializeableShape
from smithy_core.aio.types import AsyncBytesProvider
from smithy_json import JSONCodec

from aws_event_stream.aio import AWSEventPublisher
from aws_event_stream._private.serializers import EventSerializer
from aws_event_stream.aio import AWSEventPublisher
from aws_event_stream.events import EventMessage
from smithy_core.aio.types import AsyncBytesProvider
from smithy_core.serializers import SerializeableShape
from smithy_json import JSONCodec

from . import EVENT_STREAM_SERDE_CASES, INITIAL_REQUEST_CASE, INITIAL_RESPONSE_CASE

Expand Down
3 changes: 1 addition & 2 deletions packages/aws-event-stream/tests/unit/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from io import BytesIO

import pytest
from smithy_core.aio.types import AsyncBytesReader

from aws_event_stream.events import (
MAX_HEADER_VALUE_BYTE_LENGTH,
MAX_HEADERS_LENGTH,
Expand All @@ -31,6 +29,7 @@
InvalidIntegerValue,
InvalidPayloadLength,
)
from smithy_core.aio.types import AsyncBytesReader

EMPTY_MESSAGE = (
(
Expand Down
13 changes: 7 additions & 6 deletions packages/aws-sdk-signers/src/aws_sdk_signers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
such as AioHTTP, Curl, Postman, Requests, urllib3, etc."""

from __future__ import annotations

import importlib.metadata

from ._http import URI, AWSRequest, Field, Fields
from ._http import AWSRequest, Field, Fields, URI
from ._identity import AWSCredentialIdentity
from ._io import AsyncBytesReader
from .signers import (
AsyncSigV4Signer,
AsyncEventSigner,
AsyncSigV4Signer,
SigV4Signer,
SigV4SigningProperties,
)
Expand All @@ -20,14 +21,14 @@
__version__ = importlib.metadata.version("aws-sdk-signers")

__all__ = (
"AsyncBytesReader",
"AsyncSigV4Signer",
"AsyncEventSigner",
"URI",
"AWSCredentialIdentity",
"AWSRequest",
"AsyncBytesReader",
"AsyncEventSigner",
"AsyncSigV4Signer",
"Field",
"Fields",
"SigV4Signer",
"SigV4SigningProperties",
"URI",
)
4 changes: 2 additions & 2 deletions packages/aws-sdk-signers/src/aws_sdk_signers/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(
kind: interfaces_http.FieldPosition = interfaces_http.FieldPosition.HEADER,
):
self.name = name
self.values: list[str] = [val for val in values] if values is not None else []
self.values: list[str] = list(values) if values is not None else []
self.kind = kind

def add(self, value: str) -> None:
Expand Down Expand Up @@ -114,7 +114,7 @@ def __init__(
:param encoding: The string encoding to be used when converting the ``Field``
name and value from ``str`` to ``bytes`` for transmission.
"""
init_fields = [fld for fld in initial] if initial is not None else []
init_fields = list(initial) if initial is not None else []
init_field_names = [self._normalize_field_name(fld.name) for fld in init_fields]
fname_counter = Counter(init_field_names)
repeated_names_exist = (
Expand Down
4 changes: 0 additions & 4 deletions packages/aws-sdk-signers/src/aws_sdk_signers/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ class AWSSDKWarning(UserWarning): ...
class BaseAWSSDKException(Exception):
"""Top-level exception to capture SDK-related errors."""

...


class MissingExpectedParameterException(BaseAWSSDKException, ValueError):
"""Some APIs require specific signing properties to be present."""

...
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from collections.abc import Mapping
from typing import Protocol


type HEADER_VALUE = bool | int | bytes | str | datetime.datetime | uuid.UUID
"""A union of valid value types for event headers."""

Expand Down
12 changes: 6 additions & 6 deletions packages/aws-sdk-signers/src/aws_sdk_signers/signers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# ruff: noqa: S101
import asyncio
import datetime
import hmac
Expand All @@ -11,18 +11,18 @@
from collections.abc import AsyncIterable, Iterable
from copy import deepcopy
from hashlib import sha256
from typing import Required, TypedDict, TYPE_CHECKING
from typing import TYPE_CHECKING, Required, TypedDict
from urllib.parse import parse_qsl, quote

from .interfaces.io import AsyncSeekable, Seekable
from ._http import URI, AWSRequest, Field
from ._http import AWSRequest, Field, URI
from ._identity import AWSCredentialIdentity
from .interfaces.identity import AWSCredentialsIdentity as _AWSCredentialsIdentity
from ._io import AsyncBytesReader
from .exceptions import AWSSDKWarning, MissingExpectedParameterException
from .interfaces.identity import AWSCredentialsIdentity as _AWSCredentialsIdentity
from .interfaces.io import AsyncSeekable, Seekable

if TYPE_CHECKING:
from .interfaces.events import EventMessage, EventHeaderEncoder
from .interfaces.events import EventHeaderEncoder, EventMessage

HEADERS_EXCLUDED_FROM_SIGNING: tuple[str, ...] = (
"accept",
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-sdk-signers/tests/unit/auth/test_sigv4.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import pytest
from aws_sdk_signers import (
URI,
AsyncBytesReader,
AWSCredentialIdentity,
AWSRequest,
Field,
Fields,
URI,
)
from aws_sdk_signers.exceptions import AWSSDKWarning
from aws_sdk_signers.signers import (
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-sdk-signers/tests/unit/test_signers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

import pytest
from aws_sdk_signers import (
URI,
AsyncSigV4Signer,
AWSCredentialIdentity,
AWSRequest,
Fields,
SigV4Signer,
SigV4SigningProperties,
URI,
)

SIGV4_RE = re.compile(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from typing import Final

from smithy_aws_core.traits import RestJson1Trait
from smithy_http.aio.protocols import HttpBindingClientProtocol
from smithy_core.codecs import Codec
from smithy_core.shapes import ShapeID
from smithy_http.aio.protocols import HttpBindingClientProtocol
from smithy_json import JSONCodec

from ..traits import RestJson1Trait


class RestJsonClientProtocol(HttpBindingClientProtocol):
"""An implementation of the aws.protocols#restJson1 protocol."""
Expand Down
5 changes: 3 additions & 2 deletions packages/smithy-aws-core/src/smithy_aws_core/auth/sigv4.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
from dataclasses import dataclass
from typing import Protocol

from smithy_aws_core.identity import AWSCredentialsIdentity
from aws_sdk_signers import AsyncSigV4Signer, SigV4SigningProperties
from smithy_core.aio.interfaces.identity import IdentityResolver
from smithy_core.exceptions import SmithyIdentityException
from smithy_core.interfaces.identity import IdentityProperties
from smithy_http.aio.interfaces.auth import HTTPAuthScheme, HTTPSigner
from aws_sdk_signers import SigV4SigningProperties, AsyncSigV4Signer

from ..identity import AWSCredentialsIdentity


class SigV4Config(Protocol):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
from .environment import EnvironmentCredentialsResolver
from .static import StaticCredentialsResolver
from .imds import IMDSCredentialsResolver
from .static import StaticCredentialsResolver

__all__ = (
"EnvironmentCredentialsResolver",
"StaticCredentialsResolver",
"IMDSCredentialsResolver",
"StaticCredentialsResolver",
)
Loading