Skip to content

tests: metrics tests fail when PYTHONWARNINGS="ignore" #993

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

Closed
michaelbrewer opened this issue Feb 3, 2022 · 3 comments · Fixed by #994
Closed

tests: metrics tests fail when PYTHONWARNINGS="ignore" #993

michaelbrewer opened this issue Feb 3, 2022 · 3 comments · Fixed by #994
Labels
bug Something isn't working

Comments

@michaelbrewer
Copy link
Contributor

michaelbrewer commented Feb 3, 2022

What were you trying to accomplish?

Run make test when PYTHONWARNINGS="ignore"

Expected Behavior

Test should pass

Current Behavior

_____________________________________________________________________________________________________ test_log_metrics_decorator_no_metrics_warning _____________________________________________________________________________________________________

dimensions = [{'name': 'test_dimension', 'value': 'test'}, {'name': 'test_dimension_2', 'value': 'test'}], namespace = 'test_namespace', service = 'test_service'

    def test_log_metrics_decorator_no_metrics_warning(dimensions, namespace, service):
        # GIVEN Metrics is initialized
        my_metrics = Metrics(namespace=namespace, service=service)
    
        # WHEN using the log_metrics decorator and no metrics have been added
        @my_metrics.log_metrics
        def lambda_handler(evt, context):
            pass
    
        # THEN it should raise a warning instead of throwing an exception
        with warnings.catch_warnings(record=True) as w:
            lambda_handler({}, {})
>           assert len(w) == 1
E           assert 0 == 1
E             +0
E             -1

tests/functional/test_metrics.py:555: AssertionError

Possible Solution

Temp set warnings.simplefilter("default")

Steps to Reproduce (for bugs)

  1. export PYTHONWARNINGS="ignore"
  2. make test

Environment

  • Powertools version used:
  • Packaging format (Layers, PyPi):
  • AWS Lambda function runtime:
  • Debugging logs
@michaelbrewer michaelbrewer added bug Something isn't working triage Pending triage from maintainers labels Feb 3, 2022
michaelbrewer added a commit to gyft/aws-lambda-powertools-python that referenced this issue Feb 3, 2022
Changes:
- run 'make format'
- set 'warnings.simplefilter(default)'

closes aws-powertools#993
@heitorlessa
Copy link
Contributor

@michaelbrewer could you provide more details about this issue before we merge? How did you run into it since this flag isn't set by default?

Looking at the docs, it seems we should be doing this for all utilities not just metrics: https://docs.python.org/3/library/warnings.html#overriding-the-default-filter

@michaelbrewer
Copy link
Contributor Author

i was trying to disabled some setup issues with poetry on my m1 mac and it prints out warnings.

@michaelbrewer
Copy link
Contributor Author

@heitorlessa this is based on pythons recommended way for testing warnings:

import warnings

def fxn():
    warnings.warn("deprecated", DeprecationWarning)

with warnings.catch_warnings(record=True) as w:
    # Cause all warnings to always be triggered.
    warnings.simplefilter("always")
    # Trigger a warning.
    fxn()
    # Verify some things
    assert len(w) == 1
    assert issubclass(w[-1].category, DeprecationWarning)
    assert "deprecated" in str(w[-1].message)

@heitorlessa heitorlessa removed the triage Pending triage from maintainers label Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants