Skip to content

Commit f91076e

Browse files
Merge remote-tracking branch 'origin/develop' into pr8734
# Conflicts: # aws_lambda_powertools/utilities/idempotency/idempotency.py
2 parents 6b73d62 + ff823e7 commit f91076e

47 files changed

Lines changed: 1678 additions & 149 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.agents/skills/building-lambda-functions-with-powertools-python/references/idempotency.md‎

Lines changed: 18 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.agents/skills/building-lambda-functions-with-powertools-python/references/metrics.md‎

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.github/workflows/agent_skill_references.yml‎

Lines changed: 0 additions & 52 deletions
This file was deleted.

‎.github/workflows/publish_v3_layer.yml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ jobs:
277277

278278
- name: Replace layer versions in documentation
279279
run: ./layer_v3/scripts/update_layer_arn_v3.sh ${{ inputs.layer_documentation_version }}
280+
# The agent skill references are generated from the docs; regenerate them here so the release PR carries both
281+
- name: Regenerate agent skill references
282+
run: python3 scripts/build_agent_skill_references.py
280283
# NOTE: It felt unnecessary creating yet another PR to update changelog w/ latest tag
281284
# since this is the only step in the release where we update docs from a temp branch
282285
- name: Update changelog with latest tag
@@ -285,7 +288,7 @@ jobs:
285288
id: create-pr
286289
uses: ./.github/actions/create-pr
287290
with:
288-
files: "docs/index.md docs/getting-started/install.md docs/includes/_layer_homepage_arm64.md docs/includes/_layer_homepage_x86.md examples CHANGELOG.md"
291+
files: "docs/index.md docs/getting-started/install.md docs/includes/_layer_homepage_arm64.md docs/includes/_layer_homepage_x86.md examples CHANGELOG.md .agents/skills/building-lambda-functions-with-powertools-python/references"
289292
temp_branch_prefix: "ci-layer-docs"
290293
pull_request_title: "chore(ci): layer docs update"
291294
github_token: ${{ secrets.GITHUB_TOKEN }}

‎AGENTS.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ make mypy # static typing, must pass
1818
uv run pytest tests/unit tests/functional/<utility> # fast loop for one utility
1919
make test # full unit + functional + performance, what CI runs
2020
make pr # everything CI checks, run it before opening the PR
21-
make agent-skill # optional: regenerate .agents/skills/*/references after changing docs/; pre-commit does it, and CI does it after merge
21+
make agent-skill # optional: regenerate .agents/skills/*/references after changing docs/; pre-commit does it, and the release does it
2222
```
2323

2424
Do not run the full suite with `pytest -n auto`; collection differs between workers and it fails for reasons unrelated to your change.
@@ -58,6 +58,6 @@ Do not run the full suite with `pytest -n auto`; collection differs between work
5858
| Sample events | `tests/events/` |
5959
| Docs and examples | `docs/<area>/<utility>.md`, `examples/<utility>/src/` |
6060
| Project premises | `docs/adr/` |
61-
| Agent skill for library users | `.agents/skills/` (the `references/` folder is generated from `docs/` by `scripts/build_agent_skill_references.py`, and regenerated by CI after every docs merge; do not edit it or use it as a source, change `docs/`) |
61+
| Agent skill for library users | `.agents/skills/` (the `references/` folder is generated from `docs/` by `scripts/build_agent_skill_references.py`, and regenerated on every release; do not edit it or use it as a source, change `docs/`) |
6262
| Layer build | `layer_v3/` |
6363
| Maintainer process | [MAINTAINERS.md](MAINTAINERS.md), [docs/maintainers.md](docs/maintainers.md) |

‎aws_lambda_powertools/event_handler/openapi/schema_generator.py‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def generate_openapi_path(
6565
"""
6666
Generate the OpenAPI path spec and definitions for a single route.
6767
"""
68-
from aws_lambda_powertools.event_handler.openapi.dependant import get_flat_params
68+
from aws_lambda_powertools.event_handler.openapi.dependant import get_flat_params, get_path_param_names
6969

7070
definitions: dict[str, Any] = {}
7171

@@ -91,6 +91,7 @@ def generate_openapi_path(
9191
all_route_params=all_route_params,
9292
model_name_map=model_name_map,
9393
field_mapping=field_mapping,
94+
path_param_names=get_path_param_names(openapi_path),
9495
)
9596

9697
if parameters:
@@ -162,6 +163,7 @@ def _build_operation_parameters(
162163
all_route_params: Sequence[ModelField],
163164
model_name_map: dict[TypeModelOrEnum, str],
164165
field_mapping: dict[tuple[ModelField, Literal["validation", "serialization"]], JsonSchemaValue],
166+
path_param_names: set[str],
165167
) -> list[dict[str, Any]]:
166168
"""Build the list of OpenAPI operation parameters."""
167169
from aws_lambda_powertools.event_handler.openapi.params import Param
@@ -176,7 +178,7 @@ def _build_operation_parameters(
176178
if _is_pydantic_model_param(field_info):
177179
generated_parameters = _expand_pydantic_model_parameters(field_info)
178180
else:
179-
generated_parameters = [_create_regular_parameter(param, model_name_map, field_mapping)]
181+
generated_parameters = [_create_regular_parameter(param, model_name_map, field_mapping, path_param_names)]
180182

181183
parameters.extend(
182184
parameter
@@ -339,10 +341,18 @@ def _create_pydantic_field_parameter(
339341
return individual_param
340342

341343

344+
def _resolve_parameter_name(param: ModelField, field_info: Param, path_param_names: set[str]) -> str:
345+
"""Resolve the OpenAPI parameter name, aligning path parameters with route-template placeholders."""
346+
if field_info.in_.value == "path" and param.alias not in path_param_names and param.name in path_param_names:
347+
return param.name
348+
return param.alias
349+
350+
342351
def _create_regular_parameter(
343352
param: ModelField,
344353
model_name_map: dict[TypeModelOrEnum, str],
345354
field_mapping: dict[tuple[ModelField, Literal["validation", "serialization"]], JsonSchemaValue],
355+
path_param_names: set[str],
346356
) -> dict[str, Any]:
347357
"""Create an OpenAPI parameter from a regular ModelField."""
348358
from aws_lambda_powertools.event_handler.openapi.compat import get_schema_from_model_field
@@ -355,13 +365,8 @@ def _create_regular_parameter(
355365
field_mapping=field_mapping,
356366
)
357367

358-
# Path parameter names must match the route-template placeholders, which are
359-
# keyed by the Python argument name - an explicit alias would otherwise emit
360-
# a name that does not exist in the path template.
361-
param_name = param.name if field_info.in_.value == "path" else param.alias
362-
363368
parameter: dict[str, Any] = {
364-
"name": param_name,
369+
"name": _resolve_parameter_name(param, field_info, path_param_names),
365370
"in": field_info.in_.value,
366371
"required": param.required,
367372
"schema": param_schema,

‎aws_lambda_powertools/logging/formatter.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ def set_context_keys(**kwargs: dict[str, Any]) -> None:
490490

491491
def remove_context_keys(keys: Iterable[str]) -> None:
492492
context = _get_context()
493-
context_values = context.get()
493+
# Copy instead of mutating in place: asyncio tasks and copied contexts share this dict object
494+
context_values = dict(context.get())
494495

495496
for k in keys:
496497
context_values.pop(k, None)

‎aws_lambda_powertools/metrics/base.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import functools
1111
import json
1212
import logging
13+
import math
1314
import numbers
1415
import os
1516
import warnings
@@ -38,6 +39,7 @@
3839
)
3940
from aws_lambda_powertools.shared import constants
4041
from aws_lambda_powertools.shared.functions import is_durable_context, resolve_env_var_choice
42+
from aws_lambda_powertools.warnings import PowertoolsUserWarning
4143

4244
if TYPE_CHECKING:
4345
from collections.abc import Callable, Generator
@@ -152,6 +154,14 @@ def add_metric(
152154

153155
unit = self._extract_metric_unit_value(unit=unit)
154156
resolution = self._extract_metric_resolution_value(resolution=resolution)
157+
if not math.isfinite(value):
158+
warnings.warn(
159+
f"The metric {name} has a non-finite value ({value}) and won't be added. "
160+
"CloudWatch EMF only accepts finite numbers.",
161+
category=PowertoolsUserWarning,
162+
stacklevel=2,
163+
)
164+
return
155165
metric: dict = self.metric_set.get(name, defaultdict(list))
156166
metric["Unit"] = unit
157167
metric["StorageResolution"] = resolution
@@ -657,6 +667,9 @@ def single_metric(
657667
metric.add_dimension(name=dim_name, value=dim_value)
658668

659669
yield metric
660-
metric_set = metric.serialize_metric_set()
670+
# add_metric drops a non-finite value with a warning, leaving nothing to publish
671+
if metric.metric_set:
672+
metric_set = metric.serialize_metric_set()
661673
finally:
662-
print(json.dumps(metric_set, separators=(",", ":")))
674+
if metric_set is not None:
675+
print(json.dumps(metric_set, separators=(",", ":")))

‎aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import datetime as dt
44
import json
55
import logging
6+
import math
67
import numbers
78
import os
89
import warnings
@@ -165,6 +166,14 @@ def add_metric(
165166
metric_resolutions=self._metric_resolutions,
166167
resolution=resolution,
167168
)
169+
if not math.isfinite(value):
170+
warnings.warn(
171+
f"The metric {name} has a non-finite value ({value}) and won't be added. "
172+
"CloudWatch EMF only accepts finite numbers.",
173+
category=PowertoolsUserWarning,
174+
stacklevel=2,
175+
)
176+
return
168177
metric: dict = self.metric_set.get(name, defaultdict(list))
169178
metric["Unit"] = unit
170179
metric["StorageResolution"] = resolution

‎aws_lambda_powertools/utilities/data_classes/ses_event.py‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,35 @@ def value(self) -> str:
2020

2121
class SESMailCommonHeaders(DictWrapper):
2222
@property
23-
def return_path(self) -> str:
23+
def return_path(self) -> str | None:
2424
"""The values in the Return-Path header of the email."""
25-
return self["returnPath"]
25+
return self.get("returnPath")
2626

2727
@property
2828
def get_from(self) -> list[str]:
2929
"""The values in the From header of the email."""
3030
# Note: this name conflicts with existing python builtins
31-
return self["from"]
31+
return self.get("from") or []
3232

3333
@property
34-
def date(self) -> str:
34+
def date(self) -> str | None:
3535
"""The date and time when Amazon SES received the message."""
36-
return self["date"]
36+
return self.get("date")
3737

3838
@property
3939
def to(self) -> list[str]:
4040
"""The values in the To header of the email."""
41-
return self["to"]
41+
return self.get("to") or []
4242

4343
@property
44-
def message_id(self) -> str:
44+
def message_id(self) -> str | None:
4545
"""The ID of the original message."""
46-
return str(self["messageId"])
46+
return self.get("messageId")
4747

4848
@property
49-
def subject(self) -> str:
49+
def subject(self) -> str | None:
5050
"""The value of the Subject header for the email."""
51-
return str(self["subject"])
51+
return self.get("subject")
5252

5353
@property
5454
def cc(self) -> list[str]:
@@ -148,10 +148,10 @@ def function_arn(self) -> str:
148148
return self["functionArn"]
149149

150150
@property
151-
def invocation_type(self) -> str:
151+
def invocation_type(self) -> str | None:
152152
"""String that contains the invocation type of the Lambda function. Possible values are RequestResponse
153153
and Event. Present only for the Lambda action type."""
154-
return self["invocationType"]
154+
return self.get("invocationType")
155155

156156

157157
class SESReceipt(DictWrapper):

0 commit comments

Comments
 (0)