Skip to content

refactor(logger): use standard collections for types #6471

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

Merged
merged 5 commits into from
Apr 15, 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
3 changes: 2 additions & 1 deletion aws_lambda_powertools/logging/buffer/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import sys
import time
from typing import TYPE_CHECKING, Any, Mapping
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
import logging
from collections.abc import Mapping


def _create_buffer_record(
Expand Down
6 changes: 4 additions & 2 deletions aws_lambda_powertools/logging/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
from contextvars import ContextVar
from datetime import datetime, timezone
from functools import partial
from typing import TYPE_CHECKING, Any, Callable, Generator, Iterable
from typing import TYPE_CHECKING, Any

from aws_lambda_powertools.shared import constants
from aws_lambda_powertools.shared.functions import powertools_dev_is_set

if TYPE_CHECKING:
from collections.abc import Callable, Generator, Iterable

from aws_lambda_powertools.logging.types import LogRecord, LogStackTrace

RESERVED_LOG_ATTRS = (
Expand Down Expand Up @@ -68,7 +70,7 @@ def append_context_keys(self, **additional_keys: Any) -> Generator[None, None, N
yield

# These specific thread-safe methods are necessary to manage shared context in concurrent environments.
# They prevent race conditions and ensure data consistency across multiple threads.
# They prevent race conditions and ensure data consistency across multiple threads and logger.
def thread_safe_append_keys(self, **additional_keys) -> None:
raise NotImplementedError()

Expand Down
4 changes: 3 additions & 1 deletion aws_lambda_powertools/logging/formatters/datadog.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Callable
from typing import TYPE_CHECKING, Any

from aws_lambda_powertools.logging.formatter import LambdaPowertoolsFormatter

if TYPE_CHECKING:
from collections.abc import Callable

from aws_lambda_powertools.logging.types import LogRecord


Expand Down
4 changes: 3 additions & 1 deletion aws_lambda_powertools/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import sys
import warnings
from contextlib import contextmanager
from typing import IO, TYPE_CHECKING, Any, Callable, Generator, Iterable, Mapping, TypeVar, cast, overload
from typing import IO, TYPE_CHECKING, Any, TypeVar, cast, overload

from aws_lambda_powertools.logging.buffer.cache import LoggerBufferCache
from aws_lambda_powertools.logging.buffer.functions import _check_minimum_buffer_log_level, _create_buffer_record
Expand Down Expand Up @@ -45,6 +45,8 @@
from aws_lambda_powertools.warnings import PowertoolsUserWarning

if TYPE_CHECKING:
from collections.abc import Callable, Generator, Iterable, Mapping

from aws_lambda_powertools.logging.buffer.config import LoggerBufferConfig
from aws_lambda_powertools.shared.types import AnyCallableT

Expand Down
4 changes: 3 additions & 1 deletion aws_lambda_powertools/logging/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

import logging
from typing import TYPE_CHECKING, Callable
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from collections.abc import Callable

from aws_lambda_powertools.logging.logger import Logger

PACKAGE_LOGGER = "aws_lambda_powertools"
Expand Down
15 changes: 9 additions & 6 deletions tests/functional/logger/required_dependencies/test_logger.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import functools
import inspect
import io
Expand All @@ -9,8 +11,9 @@
import string
import sys
from collections import namedtuple
from collections.abc import Callable, Iterable
from datetime import datetime, timezone
from typing import Any, Callable, Dict, Iterable, List, Optional, Union
from typing import Any

import pytest

Expand Down Expand Up @@ -855,12 +858,12 @@ def test_logger_custom_powertools_formatter_clear_state(stdout, service_name, la
class CustomFormatter(LambdaPowertoolsFormatter):
def __init__(
self,
json_serializer: Optional[Callable[[Dict], str]] = None,
json_deserializer: Optional[Callable[[Union[Dict, str, bool, int, float]], str]] = None,
json_default: Optional[Callable[[Any], Any]] = None,
datefmt: Optional[str] = None,
json_serializer: Callable[[dict], str] | None = None,
json_deserializer: Callable[[dict, str, bool, int, float], str] | None = None,
json_default: Callable[[Any], Any] | None = None,
datefmt: str | None = None,
use_datetime_directive: bool = False,
log_record_order: Optional[List[str]] = None,
log_record_order: list[str] | None = None,
utc: bool = False,
**kwargs,
):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""aws_lambda_logging tests."""

from __future__ import annotations

import io
import json
import os
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import io
import json
import logging
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import io
import json
import logging
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""aws_lambda_logging tests."""

from __future__ import annotations

import io
import json
import random
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest

from aws_lambda_powertools.logging.buffer.cache import LoggerBufferCache
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest

from aws_lambda_powertools.logging.buffer import LoggerBufferConfig
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from aws_lambda_powertools.logging.buffer.functions import _check_minimum_buffer_log_level


Expand Down
Loading