Skip to content
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

refactor(metrics): Improve type annotations for metrics decorator #6000

Merged
merged 3 commits into from
Feb 3, 2025

Conversation

leandrodamascena
Copy link
Contributor

Issue number: #4088

Summary

Changes

Description

This PR improves type annotations for the metrics decorator @metrics.log_metrics. While we strive to enhance type safety, it's important to note that supporting strict typing is a best-effort approach and not guaranteed in Powertools.

This new annotation type supports dependency injection scenario as well. We cannot anticipate all possible return types for a Lambda function, so the best approach is to define Callable[..., Any] as the expected return for this decorator.

from typing import Any
from aws_lambda_powertools import Metrics, Logger
from aws_lambda_powertools.utilities.typing import LambdaContext

metrics = Metrics()
logger = Logger()


@metrics.log_metrics(capture_cold_start_metric=True) 
def lambda_handler(event: Any, context: LambdaContext, logger: Logger | None = None) -> dict[str, Any]:
    return {"a": "b"}
(venv) ➜  mymetrics mypy --strict metricsnew.py 
Success: no issues found in 1 source file

User experience

This will improve pyright/mypy errors report.

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@leandrodamascena leandrodamascena requested a review from a team as a code owner February 3, 2025 10:22
@boring-cyborg boring-cyborg bot added the metrics label Feb 3, 2025
@pull-request-size pull-request-size bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Feb 3, 2025
@leandrodamascena leandrodamascena self-assigned this Feb 3, 2025
@leandrodamascena leandrodamascena linked an issue Feb 3, 2025 that may be closed by this pull request
Copy link

codecov bot commented Feb 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.24%. Comparing base (02a2490) to head (3a3c3ed).
Report is 3 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #6000   +/-   ##
========================================
  Coverage    96.24%   96.24%           
========================================
  Files          234      234           
  Lines        11016    11016           
  Branches       798      798           
========================================
  Hits         10602    10602           
  Misses         327      327           
  Partials        87       87           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@anafalcao anafalcao left a comment

Choose a reason for hiding this comment

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

approved 👏

@leandrodamascena leandrodamascena merged commit 10a5cf5 into develop Feb 3, 2025
5 of 6 checks passed
@leandrodamascena leandrodamascena deleted the fix/metrics-decorator-typing branch February 3, 2025 12:32
Copy link

sonarqubecloud bot commented Feb 3, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement metrics size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Static typing: @metrics.log_metrics decorator
2 participants