Skip to content

Static typing: The add_handler API from logger was removed #2377

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
alexandreczg opened this issue Jun 3, 2023 · 11 comments
Closed

Static typing: The add_handler API from logger was removed #2377

alexandreczg opened this issue Jun 3, 2023 · 11 comments
Assignees
Labels
logger typing Static typing definition related issues (mypy, pyright, etc.)

Comments

@alexandreczg
Copy link

alexandreczg commented Jun 3, 2023

Expected Behaviour

In version 2.4.0 adding a handler to the log was easy. I would expect adding a handler to my log to be as simple as addHandler.

Current Behaviour

Method doesn't exist

image

Code snippet

logger = logging.Logger()
logger.addHandler(additional_handler)

But that API was removed on version 2.15.0. I depend on it to be able to log to cloudwatch and other places. I was reading the changelog but could not find any reference to this.

the only way around of this is to mess with the internals:

logger._logger.addHandler(additional_handler)


### Possible Solution

Semver would dictate that the public API doesn't change with a minor version upgrade. Did I miss something something regarding this breaking change? Is there another workaround?

### Steps to Reproduce

Above

### Powertools for AWS Lambda (Python) version

latest

### AWS Lambda function runtime

3.10

### Packaging format used

PyPi

### Debugging logs

_No response_
@alexandreczg alexandreczg added bug Something isn't working triage Pending triage from maintainers labels Jun 3, 2023
@boring-cyborg
Copy link

boring-cyborg bot commented Jun 3, 2023

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@alexandreczg
Copy link
Author

Apologies, closing, cannot reproduce it again.

@github-actions
Copy link
Contributor

github-actions bot commented Jun 3, 2023

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@heitorlessa
Copy link
Contributor

Apologies, closing, cannot reproduce it again.

Hey @alexandreczg - did you get a runtime error or was a static checker error saying the method doesn't exist?

The former would be a regression tho I can't reproduce it either. If it's the latter, we can address that with a PR to implement it and proxy it to _logger.addHandler so mypy is happy.

This is the change I made that would cause the latter but not the former (tested with a stream handler + file log handler now).

Please let us know ;)

PS: No need to apologise, better safe than sorry and we appreciate your time flagging it very much!!

#2334

@alexandreczg
Copy link
Author

Hey @heitorlessa, thank your for addressing this, commendable your dedication!

It was a typing error actually, not runtime. I use pyright/pylance (vscode). I freaked out since it had been a long hours day and when I was about to push my code I start getting that error. Couldn't really understand it since it had been working. I regressed to version 2.4.0 (it was my last stable version) but quickly realized that I had implemented the new batch partial processor (SQS). So, I went back to 2.16.0 and magically the linting error went way... I'm not sure what or how it all happened, but when I introspected the logger class, I couldn't find the addHandler method.

I'll keep your and this thread updated if something is to change now that I have pushed my code to AWS and using latest layer. 🤞

@alexandreczg
Copy link
Author

@heitorlessa , okay in version 2.16.1 the issue is back

image

@heitorlessa
Copy link
Contributor

@heitorlessa , okay in version 2.16.1 the issue is back

image

That's the piece I mentioned it's expected -- does it fail if you run it?

Pyright (what Pylance uses) can't infer through the object proxy so it believes it doesn't exist. At runtime, it works.

If you can confirm it works, feel free to make a PR adding a "addHandler" method that call self._logger.addHandler so Pylance will be happy in the next release - more than happy to accommodate that ;)

@heitorlessa heitorlessa reopened this Jun 5, 2023
@heitorlessa heitorlessa added need-more-information Pending information to continue logger and removed triage Pending triage from maintainers labels Jun 5, 2023
@heitorlessa heitorlessa self-assigned this Jun 5, 2023
@alexandreczg
Copy link
Author

alexandreczg commented Jun 5, 2023

Hi @heitorlessa, I can confirm it works. I will look into how I can open a PR to fix the linting. Another note, unrelated but would like your eyes on the batch_processor. I don't think it is working as it should:

image

It is doing records: List[Dict] = event.get("Records", []) but catching any AttributeError that comes from that line. I don't believe this will ever catch anything. BAsically, I'm passing any event to it, and it's just going into the:

with processor(records, record_handler, context):
        processor.process()

with an empty list of records which of course returns:

{'batchItemFailures': []}

I don't think this is supposed to happen. Please, correct me if I am wrong. I will create a new bug ticket if needed for tracking purposes.

Edit: Better screenshot with filename included.

@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Jun 6, 2023
@heitorlessa heitorlessa added typing Static typing definition related issues (mypy, pyright, etc.) and removed bug Something isn't working labels Jun 6, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jun 6, 2023

This is now released under 2.16.2 version!

@github-actions github-actions bot closed this as completed Jun 6, 2023
@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Jun 6, 2023
@heitorlessa heitorlessa changed the title Bug: The add_handler API from logger was removed Static typing: The add_handler API from logger was removed Jun 6, 2023
@heitorlessa
Copy link
Contributor

hey @alexandreczg - we included the addHandler as part of an emergency release we had to make, so v2.16.2 (or Layer v34) will make Pylance happy; there'll surely be many opportunities for contribution.

on Batch, please create an issue describing your intended outcome. The reason we're defensively catching AttributeError is that some customers don't pass the event like you are (expected usage) - if they pass a list of records this will fail with an AttributeError, therefore raising a more helpful error on why that's the case.

As to {'batchItemFailures': []}, this is expected when you don't have any error. Batch will include message IDs that failed to be processed. If you throw any exception in your record handler function, you'll see this populated.

An empty {'batchItemFailures': []} means: "Entire batch processed successfully, please delete all messages".

from aws_lambda_powertools.utilities.batch import (
    AsyncBatchProcessor,
    EventType,
    async_process_partial_response,
)
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
from aws_lambda_powertools.utilities.typing import LambdaContext

processor = AsyncBatchProcessor(event_type=EventType.SQS)


async def async_record_handler(record: SQSRecord):
    ...


def lambda_handler(event, context: LambdaContext):
    records = event.get("Records", [])

    # This will fail because `records` is no longer a Dict
    return async_process_partial_response(
        event=records, record_handler=async_record_handler, processor=processor, context=context
    )

@heitorlessa heitorlessa removed the need-more-information Pending information to continue label Jun 6, 2023
@alexandreczg
Copy link
Author

@heitorlessa thank your for the quick around on this 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
logger typing Static typing definition related issues (mypy, pyright, etc.)
Projects
None yet
Development

No branches or pull requests

2 participants