Provide more precise type hints for Table.get#602
Merged
msiemens merged 3 commits intomsiemens:masterfrom Jan 17, 2026
Merged
Conversation
The `doc_ids` branch always returns a list, never None.
Owner
|
Hey, thanks for the PR! It seems like the type checking unit tests fail: Can you take a closer look at that? See eg. https://github.com/msiemens/tinydb/actions/runs/20165183963/job/59009114221?pr=602 |
Contributor
Author
|
Ah, this looks like it relates to the comment I made:
I've now added that missing case, and the error no longer occurs. |
Owner
|
Seems like the CI failures are unrelated to this PR. Thanks for contributing! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When working with pyright (at least), the return value from calls to Table.get is the declared type
Document | list[Document] | None. To avoid type errors when using the return value, one therefore has do one of the following:assert isinstance(obj, Document)if instance(obj, Document)instead ofif obj is not Noneorif objIt would be highly convenient if static type checkers could understand the logic of the method, and knew that it returns a list if and only if
doc_idis None and a list is passed todoc_ids.Here are the typing overloads that do the trick. I've confirmed that pytest and mypy remain happy with these in place. I haven't covered the case that passing no arguments leads to a
NoReturnreturn type, as that's adding more information than was already there, but I could if you wanted.I could provide some
assert_typetests if you like but I'm not so familiar with the best place to put them (and they would involve atyping_extensionsdependency on older Pythons).