-
-
Notifications
You must be signed in to change notification settings - Fork 225
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
Switch base methods to return Self instead of BoundLoggerBase #659
Conversation
All of the CI here is failing because it cannot import |
typing extensions needs to be a conditional import as done in #642 – and speaking of it, can you check please if your problems aren't fixed by that already? If not, feel free to import |
Thanks @hynek! I missed that Unfortunately #642 does not fix the type error I am seeing, since |
In that case, thanks for catching it before shipping #642. :) Is there maybe a way to add a typing test to https://github.com/hynek/structlog/blob/main/tests/typing/api.py that would've caught it? Either way, you haven't pushed you changes yet. 🤓 |
I think github is being odd; aThorp96:switch-error-type-narrowing is up to date with the change. I rebased the branch on my end to squash the changes, and I think it messed w/ Github's remote ref to my branch.... I'll try to add a test to the API and open a new PR against the branch, since you can't change the PR's HEAD ref |
@@ -323,6 +323,19 @@ async def typecheck_stdlib_async() -> None: | |||
await logger.alog(logging.CRITICAL, "async log") | |||
|
|||
|
|||
def typecheck_bound_logger_return() -> None: | |||
blogger: structlog.BoundLogger = structlog.get_logger(__name__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting find here, and perhaps the reason that this hasn't come up sooner:
This test would fail with structlog.BoundLogger
before this PR, but it would pass with structlog.stdlib.BoundLogger
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmm, after hitting this, I think this is actually the root of the issue I'm experiencing; I don't think I realized that in some places I was typing as a structlog.BoundLogger
and other places I was typing as a structlog.stdlib.BoundLogger
, and if I did, I did not expect there to be a difference. My assumption would have been that the former was a top-level export of the latter. At least then I think my type error woes are gone. I'll still get this PR over the finish line, since i think it's correct and valuable.
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Summary
This Pull Request switches several
BoundLoggerBase
methods to annotate a return type oftyping_extensions.Self
instead ofBoundLoggerBase
. This change is consistent with the code which returns specificallyself.__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:
Before this change:
After this change:
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
main
branch – use a separate branch!api.py
.docs/api.rst
by hand.versionadded
,versionchanged
, ordeprecated
directives..rst
and.md
files is written using semantic newlines.