hynek / structlog

Simple, powerful, and fast logging for Python.
https://www.structlog.org/
Other
3.6k stars 225 forks source link

Switch base methods to return Self instead of BoundLoggerBase #658

Closed aThorp96 closed 2 months ago

aThorp96 commented 2 months ago

Summary

This Pull Request switches several BoundLoggerBase methods to annotate a return type of typing_extensions.Self instead of BoundLoggerBase. This change is consistent with the code which returns specifically self.__class__() and with the tests that ensure the type is preserved when .bind() is called.

This should not have any runtime changes, but instead fixes a typing bug which caused the following code to incorrectly fail type-checking:

from structlog import get_logger, BoundLogger

log: BoundLogger = get_logger()
log.info("This is OK")
log = log.bind() #  error: Type "BoundLoggerBase" is not assignable to declared type "BoundLogger"
log.info("This should also be okay")

Before this change: Screenshot 2024-10-03 at 4 48 17 PM

After this change: Screenshot 2024-10-03 at 4 48 54 PM

Pull Request Check List

It does not appear to me that any of these apply, but I am happy to make any necessary changes to the PR or do any upkeep tasks

aThorp96 commented 2 months ago

Forgot to switch PR from my main branch to my non-main branch