Skip to content

chore(typing): move backwards compat types to shared types #3092

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

3 changes: 1 addition & 2 deletions aws_lambda_powertools/event_handler/middlewares/base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from abc import ABC, abstractmethod
from typing import Generic

from typing_extensions import Protocol

from aws_lambda_powertools.event_handler.api_gateway import Response
from aws_lambda_powertools.event_handler.types import EventHandlerInstance
from aws_lambda_powertools.shared.types import Protocol


class NextMiddleware(Protocol):
Expand Down
19 changes: 2 additions & 17 deletions aws_lambda_powertools/logging/types.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
from __future__ import annotations

import sys

if sys.version_info >= (3, 11):
from typing import NotRequired
else:
from typing_extensions import NotRequired

if sys.version_info >= (3, 8):
from typing import TypedDict
else:
from typing_extensions import TypedDict

if sys.version_info >= (3, 10):
from typing import TypeAlias
else:
from typing_extensions import TypeAlias

from typing import Any, Dict, List, Union

from aws_lambda_powertools.shared.types import NotRequired, TypeAlias, TypedDict

LogRecord: TypeAlias = Union[Dict[str, Any], "PowertoolsLogRecord"]


Expand Down
3 changes: 1 addition & 2 deletions aws_lambda_powertools/metrics/functions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from __future__ import annotations

from typing import List

from aws_lambda_powertools.metrics.provider.cloudwatch_emf.exceptions import (
MetricResolutionError,
MetricUnitError,
)
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.metric_properties import MetricResolution, MetricUnit
from aws_lambda_powertools.shared.types import List


def extract_cloudwatch_metric_resolution_value(metric_resolutions: List, resolution: int | MetricResolution) -> int:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from typing import List

from typing_extensions import NotRequired, TypedDict
from aws_lambda_powertools.shared.types import List, NotRequired, TypedDict


class CloudWatchEMFMetric(TypedDict):
Expand Down
2 changes: 1 addition & 1 deletion aws_lambda_powertools/metrics/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing_extensions import NotRequired, TypedDict
from aws_lambda_powertools.shared.types import NotRequired, TypedDict


class MetricNameUnitResolution(TypedDict):
Expand Down
4 changes: 3 additions & 1 deletion aws_lambda_powertools/shared/cookies.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from datetime import datetime
from enum import Enum
from io import StringIO
from typing import List, Optional
from typing import Optional

from aws_lambda_powertools.shared.types import List


class SameSite(Enum):
Expand Down
25 changes: 18 additions & 7 deletions aws_lambda_powertools/shared/types.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import sys
from typing import Any, Callable, Dict, List, TypeVar, Union

AnyCallableT = TypeVar("AnyCallableT", bound=Callable[..., Any]) # noqa: VNE001
# JSON primitives only, mypy doesn't support recursive tho
JSONType = Union[str, int, float, bool, None, Dict[str, Any], List[Any]]
if sys.version_info >= (3, 8):
from typing import Literal, Protocol, TypedDict
else:
from typing_extensions import Literal, Protocol, TypedDict


if sys.version_info >= (3, 8):
from typing import Protocol
if sys.version_info >= (3, 11):
from typing import NotRequired
else:
from typing_extensions import Protocol
from typing_extensions import NotRequired


if sys.version_info >= (3, 10):
from typing import TypeAlias
else:
from typing_extensions import TypeAlias

AnyCallableT = TypeVar("AnyCallableT", bound=Callable[..., Any]) # noqa: VNE001
# JSON primitives only, mypy doesn't support recursive tho
JSONType = Union[str, int, float, bool, None, Dict[str, Any], List[Any]]

__all__ = ["Protocol"]
__all__ = ["Protocol", "TypedDict", "Literal", "NotRequired", "TypeAlias"]
7 changes: 2 additions & 5 deletions aws_lambda_powertools/utilities/batch/types.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#
# type specifics
#
import sys
from typing import List, Optional, Type, Union
from typing import Optional, Type, Union

from typing_extensions import TypedDict
from aws_lambda_powertools.shared.types import List, TypedDict

has_pydantic = "pydantic" in sys.modules

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing_extensions import Literal

from aws_lambda_powertools.shared.types import Literal
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper


Expand Down
2 changes: 1 addition & 1 deletion aws_lambda_powertools/utilities/parameters/ssm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

import boto3
from botocore.config import Config
from typing_extensions import Literal

from aws_lambda_powertools.shared import constants
from aws_lambda_powertools.shared.functions import (
resolve_max_age,
resolve_truthy_env_var_choice,
slice_dictionary,
)
from aws_lambda_powertools.shared.types import Literal

from .base import DEFAULT_MAX_AGE_SECS, DEFAULT_PROVIDERS, BaseProvider, transform_value
from .exceptions import GetParameterError
Expand Down
2 changes: 1 addition & 1 deletion aws_lambda_powertools/utilities/parameters/types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from typing_extensions import Literal
from aws_lambda_powertools.shared.types import Literal

TransformOptions = Literal["json", "binary", "auto", None]
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import logging
import zlib
from datetime import datetime
from typing import List, Type, Union
from typing import Type, Union

from pydantic import BaseModel, Field, validator

from aws_lambda_powertools.shared.types import List

logger = logging.getLogger(__name__)


Expand Down
7 changes: 1 addition & 6 deletions aws_lambda_powertools/utilities/parser/types.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
"""Generics and other shared types used across parser"""

import sys
from typing import Any, Dict, Type, TypeVar, Union

from pydantic import BaseModel, Json

# We only need typing_extensions for python versions <3.8
if sys.version_info >= (3, 8):
from typing import Literal
else:
from typing_extensions import Literal
from aws_lambda_powertools.shared.types import Literal

Model = TypeVar("Model", bound=BaseModel)
EnvelopeModel = TypeVar("EnvelopeModel")
Expand Down
3 changes: 1 addition & 2 deletions aws_lambda_powertools/utilities/streaming/s3_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
overload,
)

from typing_extensions import Literal

from aws_lambda_powertools.shared.types import Literal
from aws_lambda_powertools.utilities.streaming._s3_seekable_io import _S3SeekableIO
from aws_lambda_powertools.utilities.streaming.transformations import (
CsvTransform,
Expand Down
50 changes: 33 additions & 17 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -1,46 +1,59 @@
# Enable rules.
select = [
"A", # flake8-builtins - https://beta.ruff.rs/docs/rules/#flake8-builtins-a
"B", # flake8-bugbear-b - https://beta.ruff.rs/docs/rules/#flake8-bugbear-b
"C4", # flake8-comprehensions - https://beta.ruff.rs/docs/rules/#flake8-comprehensions-c4
"A", # flake8-builtins - https://beta.ruff.rs/docs/rules/#flake8-builtins-a
"B", # flake8-bugbear-b - https://beta.ruff.rs/docs/rules/#flake8-bugbear-b
"C4", # flake8-comprehensions - https://beta.ruff.rs/docs/rules/#flake8-comprehensions-c4
"C90", # mccabe - https://beta.ruff.rs/docs/rules/#mccabe-c90
"COM", # flak8-commas - https://beta.ruff.rs/docs/rules/#flake8-commas-com
#"D", # pydocstyle - not enabled temporarily
"E", # pycodestyle error - https://beta.ruff.rs/docs/rules/#error-e
"E", # pycodestyle error - https://beta.ruff.rs/docs/rules/#error-e
"ERA", # flake8-eradicate - https://beta.ruff.rs/docs/rules/#eradicate-era
"FA", # flake8-future-annotations - https://beta.ruff.rs/docs/rules/#flake8-future-annotations-fa
"FA", # flake8-future-annotations - https://beta.ruff.rs/docs/rules/#flake8-future-annotations-fa
"FIX", # flake8-fixme - https://beta.ruff.rs/docs/rules/#flake8-fixme-fix
"F", # pyflakes - https://beta.ruff.rs/docs/rules/#pyflakes-f
"I", # isort - https://beta.ruff.rs/docs/rules/#isort-i
"F", # pyflakes - https://beta.ruff.rs/docs/rules/#pyflakes-f
"I", # isort - https://beta.ruff.rs/docs/rules/#isort-i
"ICN", # flake8-import-conventions - https://beta.ruff.rs/docs/rules/#flake8-import-conventions-icn
"ISC", # flake8-implicit-str-concat - https://beta.ruff.rs/docs/rules/#flake8-implicit-str-concat-isc
"PLE", # pylint error - https://beta.ruff.rs/docs/rules/#error-ple
"PLC", # pylint convention - https://beta.ruff.rs/docs/rules/#convention-plc
"PLR", # pylint refactoring - https://beta.ruff.rs/docs/rules/#refactor-plr
"PLW", # pylint warning - https://beta.ruff.rs/docs/rules/#warning-plw
"PL", # pylint - https://beta.ruff.rs/docs/rules/#pylint-pl
"PL", # pylint - https://beta.ruff.rs/docs/rules/#pylint-pl
"PYI", # flake8-pyi - https://beta.ruff.rs/docs/rules/#flake8-pyi-pyi
"Q", # flake8-quotes - https://beta.ruff.rs/docs/rules/#flake8-quotes-q
"Q", # flake8-quotes - https://beta.ruff.rs/docs/rules/#flake8-quotes-q
"PTH", # flake8-use-pathlib - https://beta.ruff.rs/docs/rules/#flake8-use-pathlib-pth
"T10", # flake8-debugger https://beta.ruff.rs/docs/rules/#flake8-debugger-t10
"TD", # flake8-todo - https://beta.ruff.rs/docs/rules/#flake8-todos-td
"W" # pycodestyle warning - https://beta.ruff.rs/docs/rules/#warning-w
"TD", # flake8-todo - https://beta.ruff.rs/docs/rules/#flake8-todos-td
"W", # pycodestyle warning - https://beta.ruff.rs/docs/rules/#warning-w
]

# Ignore specific rules
ignore = [
"W291", # https://beta.ruff.rs/docs/rules/trailing-whitespace/
"W291", # https://beta.ruff.rs/docs/rules/trailing-whitespace/
"PLR0913", # https://beta.ruff.rs/docs/rules/too-many-arguments/
"PLR2004", #https://beta.ruff.rs/docs/rules/magic-value-comparison/
"PLW0603", #https://beta.ruff.rs/docs/rules/global-statement/
"B904", # raise-without-from-inside-except - disabled temporarily
"B904", # raise-without-from-inside-except - disabled temporarily
"PLC1901", # Compare-to-empty-string - disabled temporarily
"PYI024",
"FA100" # Enable this rule when drop support to Python 3.7
]
"FA100", # Enable this rule when drop support to Python 3.7
]

# Exclude files and directories
exclude = ["docs", ".eggs", "setup.py", "example", ".aws-sam", ".git", "dist", ".md", ".yaml", "example/samconfig.toml", ".txt", ".ini"]
exclude = [
"docs",
".eggs",
"setup.py",
"example",
".aws-sam",
".git",
"dist",
".md",
".yaml",
"example/samconfig.toml",
".txt",
".ini",
]

# Maximum line length
line-length = 120
Expand All @@ -50,7 +63,10 @@ fix = true
fixable = ["I", "COM812", "W"]

# See: https://github.com/astral-sh/ruff/issues/128
typing-modules = ["aws_lambda_powertools.utilities.parser.types"]
typing-modules = [
"aws_lambda_powertools.utilities.parser.types",
"aws_lambda_powertools.shared.types",
]

[mccabe]
# Maximum cyclomatic complexity
Expand Down