Skip to content

Type hints for tests and examples #1859

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

Merged

Conversation

miguelgrinberg
Copy link
Collaborator

@miguelgrinberg miguelgrinberg commented Jul 4, 2024

Type hints for all remaining tests and examples.

There are some notes embedded in the code, but the main changes are:

  • All the code, tests and examples now pass mypy strict 🎉
  • I've added pyright to the type checking pass, but only for examples, to make sure vscode/vim/etc. do not produce any errors. This also passes.
  • I'm closing make fields typed as lists optional #1858 as those changes are included in this PR as well. The description of the change in that PR is still useful for you to read.
  • In test files that work with Document instances I have silenced a number of mypy errors that come as a result of using the old way of creating document fields without type hints. This is because I want to make sure these continue to be supported and tested.
  • For consistency, I have added a generic type to AttrList. In all current usages we are doing AttrList[Any] and AttrDict[Any], but this might change in the future.

Work that remains wrt types:

  • Responses are typed with Any. It would be useful to create specific types for all the fields.
  • There are inconsistencies in typing lists with List vs. Sequence and dicts with Dict and Mapping or MutableMapping that I will try to clean up at some point.

@miguelgrinberg miguelgrinberg force-pushed the types-tests-and-examples branch 3 times, most recently from e63d0ce to bfbf67a Compare July 4, 2024 22:56
@miguelgrinberg miguelgrinberg changed the title type hits for tests and examples Type hints for tests and examples Jul 5, 2024
self,
bucket: bool,
name: str,
agg_type: Union[Dict[str, Any], Agg[_R], str],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy was perfectly happy with my naive agg_type: str type, but pyright flagged an issue in one of the examples that pointed at this as the problem.

tags = Keyword(multi=True)
content = Text()
if TYPE_CHECKING:
_id: int
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a trick to get the dataclass support to accept these extra arguments in the constructor.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is an example, should we explain the trick?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I should add a comment here, thanks.

# these are still supported and should be kept functional in spite
# of not having appropriate type hints. For that reason the comment
# below disables many mypy checks that fails as a result of this.
# mypy: disable-error-code="assignment, index, arg-type, call-arg, operator, comparison-overlap, attr-defined"
Copy link
Collaborator Author

@miguelgrinberg miguelgrinberg Jul 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple of tests files that have a lot of errors because of documents that are defined using old syntax withtout the type hints. I felt it was best to silence those errors and keep some of those older syntax examples.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also keep an untyped example without all the typing "clutter"?

@miguelgrinberg miguelgrinberg force-pushed the types-tests-and-examples branch 2 times, most recently from 6f44786 to 7341cc9 Compare July 5, 2024 14:12
@miguelgrinberg miguelgrinberg marked this pull request as ready for review July 5, 2024 14:22
@miguelgrinberg miguelgrinberg requested a review from pquentin July 5, 2024 14:22
@miguelgrinberg miguelgrinberg added the backport 8.x Backport to 8.x label Jul 5, 2024
noxfile.py Outdated
@@ -38,8 +36,6 @@
"tests/test_query.py",
"tests/test_utils.py",
"tests/test_wrappers.py",
"examples/vectors.py",
"examples/async/vectors.py",
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should TYPED_FILES be removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I forgot.

@@ -50,6 +52,7 @@ async def test_alias_migration(async_write_client):
# _matches work which means we get BlogPost instance
bp = (await BlogPost.search().execute())[0]
assert isinstance(bp, BlogPost)
print("**", bp.published)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: print

Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM.

@miguelgrinberg miguelgrinberg force-pushed the types-tests-and-examples branch from 7341cc9 to eaff847 Compare July 9, 2024 14:24
@miguelgrinberg miguelgrinberg merged commit 663d6f4 into elastic:main Jul 9, 2024
16 checks passed
@miguelgrinberg miguelgrinberg deleted the types-tests-and-examples branch July 9, 2024 15:27
github-actions bot pushed a commit that referenced this pull request Jul 9, 2024
* Type hints for tests and examples

* add pyright check for examples only

* add pyright check for examples only

* simplified examples

* review feedback

(cherry picked from commit 663d6f4)
miguelgrinberg added a commit that referenced this pull request Jul 9, 2024
* Type hints for tests and examples

* add pyright check for examples only

* add pyright check for examples only

* simplified examples

* review feedback

(cherry picked from commit 663d6f4)

Co-authored-by: Miguel Grinberg <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 8.x Backport to 8.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants