Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: Apache-2.0
import asyncio
import datetime
import logging
from collections.abc import Callable

from smithy_core.aio.interfaces import AsyncByteStream
Expand All @@ -25,6 +26,8 @@
)
from smithy_core.traits import EventHeaderTrait

logger = logging.getLogger(__name__)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
logger = logging.getLogger(__name__)
LOGGER = logging.getLogger(__name__)

Copy link
Contributor Author

@jonathan343 jonathan343 Mar 17, 2025

Choose a reason for hiding this comment

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

It seems wrong to make this variable all-caps since it doesn't represent a constant and instead represents a <class 'logging.Logger'> object. Also, for what it's worth, logger seems to be the standard in docs and other projects I've seen.

I don't feel too strongly, so happy to change if you disagree.

Copy link
Contributor

Choose a reason for hiding this comment

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

But it is a constant. You're never mutating it in any scope.

Copy link
Contributor

Choose a reason for hiding this comment

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

You could more accurately describe it as Final, which is more or less the same concept. Final symbols in Python are generally upper-cased. In fact, upper casing a symbol name implicitly finalizes it. So LOGGER is equivalent to LOGGER: Final.

PEP8 doesn't really have anything to say about Final, but that was written 20 years before Final was an actual construct in the language. But most programming language naming conventions upper-case finalized properties and constants

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea I totally agree with you now. I've updated this in 74881a1

In fact, upper casing a symbol name implicitly finalizes it. So LOGGER is equivalent to LOGGER: Final.

Also, I had no idea this was a thing. Pretty cool

 error: "LOGGER" is constant (because it is uppercase) and cannot be redefined (reportConstantRedefinition)

Copy link
Contributor

Choose a reason for hiding this comment

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

We can add the typing, I don't know if we need to all caps it as a constant. logger is pretty much the universal standard for implementing your file-scope logger. It's in the docs and we already use it that way in other files. It's also convention in other languages like Java (ref) where constant is considered some immutable value.

If we really feel strongly about it, lets split that work into a separate PR to update everything at once rather than having the logger name be inconsistent across libraries.

Copy link
Contributor Author

@jonathan343 jonathan343 Mar 18, 2025

Choose a reason for hiding this comment

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

Sure, I reverted the change for now and will follow up with a separate PR to address all instances if we decide to switch to LOGGER


INITIAL_MESSAGE_TYPES = (INITIAL_REQUEST_EVENT_TYPE, INITIAL_RESPONSE_EVENT_TYPE)


Expand Down Expand Up @@ -55,14 +58,17 @@ async def receive(self) -> E | None:
raise

if event is None:
logger.debug("No event received from the source.")
return None
logger.debug("Received raw event: %s", event)

deserializer = EventDeserializer(
event=event,
payload_codec=self._payload_codec,
is_client_mode=self._is_client_mode,
)
result = self._deserializer(deserializer)
logger.debug("Successfully deserialized event: %s", result)
if isinstance(getattr(result, "value"), Exception):
raise result.value # type: ignore
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: Apache-2.0
import asyncio
import datetime
import logging
from collections.abc import Callable, Iterator
from contextlib import contextmanager
from io import BytesIO
Expand Down Expand Up @@ -29,6 +30,8 @@
)
from smithy_core.traits import ErrorTrait, EventHeaderTrait, MediaTypeTrait

logger = logging.getLogger(__name__)

_DEFAULT_STRING_CONTENT_TYPE = "text/plain"
_DEFAULT_BLOB_CONTENT_TYPE = "application/octet-stream"

Expand All @@ -55,6 +58,7 @@ def __init__(
async def send(self, event: E) -> None:
if self._closed:
raise IOError("Attempted to write to closed stream.")
logger.debug("Preparing to publish event: %s", event)
event.serialize(self._serializer)
result = self._serializer.get_result()
if result is None:
Expand All @@ -66,6 +70,7 @@ async def send(self, event: E) -> None:

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()
Expand Down