Switch base methods to return Self instead of BoundLoggerBase#659
Switch base methods to return Self instead of BoundLoggerBase#659hynek merged 13 commits intohynek:mainfrom
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 |
|
|
||
|
|
||
| def typecheck_bound_logger_return() -> None: | ||
| blogger: structlog.BoundLogger = structlog.get_logger(__name__) |
There was a problem hiding this comment.
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.
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
Summary
This Pull Request switches several
BoundLoggerBasemethods to annotate a return type oftyping_extensions.Selfinstead 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
mainbranch – use a separate branch!api.py.docs/api.rstby hand.versionadded,versionchanged, ordeprecateddirectives..rstand.mdfiles is written using semantic newlines.