Provide more precise type hints for Table.get #602
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).