Skip to content

Static typing: Metrics.set_timestamp does not allow datetime.datetime, although supported #7581

@czechnology

Description

@czechnology

Static type checker used

mypy (project's standard)

AWS Lambda function runtime

3.13

Powertools for AWS Lambda (Python) version

latest

Static type checker info

Docstring is correct, the underlying provider.set_timestamp() is correct as well, but Metrics.set_timestamp() only allows and int argument.

$ uvx --with aws_lambda_powertools mypy test.py test.py:7:25: error: Argument 1 to "set_timestamp" of "Metrics" has incompatible type "datetime"; expected "int" [arg-type] Found 1 error in 1 file (checked 1 source file)

Code snippet

from datetime import UTC, datetime from aws_lambda_powertools import Metrics now = datetime.now(UTC) Metrics().set_timestamp(int(now.timestamp() * 1_000)) # ok Metrics().set_timestamp(now) # incompatible type Metrics().provider.set_timestamp(int(now.timestamp() * 1_000)) # ok Metrics().provider.set_timestamp(now) # ok

Possible Solution

Add datetime.datetime as a possible type to Metrics.set_timestamp(timestamp: int).

Metadata

Metadata

Assignees

Labels

typingStatic typing definition related issues (mypy, pyright, etc.)

Projects

Status

Coming soon

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions