Skip to content
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

Add type hints to wrappers.py #1835

Merged
merged 6 commits into from
Jun 17, 2024
Merged

Add type hints to wrappers.py #1835

merged 6 commits into from
Jun 17, 2024

Conversation

Caiofcas
Copy link
Contributor

@Caiofcas Caiofcas commented Jun 6, 2024

Adds type hints to wrappers.py and starts to add typing to utils.AttrDict. Continuation of #1533

This one is more complex then previous typing MRs, so I'm open to suggestions. The main points are:

  • The definition of the SupportsComparison TypeAlias to ensure that the operators in Range can be compared. This could posibly be avoided by utilizing the typeshed library, but at this point it seems like overkill to add it as a development dependency for only these 4/5 types.

  • The use of cast in some instances where I needed to convey information to mypy that can be easily verified by the code validations, but it's not inferred by the type-checking. I feel that is the cleaner solution and feel that the 3 cases where it's used are justifiable.

Leave it up to you to decide on these matters due to lacking the context on how the project maintainers feel about these kind of issues.

# assign the inner dict manually to prevent __setattr__ from firing
super().__setattr__("_d_", d)

def __contains__(self, key):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would this type be _KeyT instead of object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I kept is as object to be consistent with the dict.__contains__() typing, conceptually I think is also not a type error to ask if an object of a wrong type is in the dictionary, is just gonna be false.

@miguelgrinberg
Copy link
Collaborator

The definition of the SupportsComparison TypeAlias to ensure that the operators in Range can be compared. This could posibly be avoided by utilizing the typeshed library, but at this point it seems like overkill to add it as a development dependency for only these 4/5 types.

I don't understand this. Mypy bundles typeshed, so aren't all these types already available? This seems to work:

if TYPE_CHECKING:
    from _operator import _SupportsComparison

I'm going to go ahead and add a commit here with this simplification, since I have it working here. If you think this is missing something feel free to remove this commit and continue with your version.

@miguelgrinberg
Copy link
Collaborator

@Caiofcas Also added some minor changes to the casts, to help mypy do a better job of it and have less explicit casts in the code. Let me know what you think of all these changes.

@Caiofcas
Copy link
Contributor Author

I don't understand this. Mypy bundles typeshed, so aren't all these types already available? This seems to work:

if TYPE_CHECKING:
    from _operator import _SupportsComparison

I'm going to go ahead and add a commit here with this simplification, since I have it working here. If you think this is missing something feel free to remove this commit and continue with your version.

Hm, thanks for catching this, I did miss that ofc mypy would have it installed if it was giving the error with these types 🙃 .

@Caiofcas Also added some minor changes to the casts, to help mypy do a better job of it and have less explicit casts in > the code. Let me know what you think of all these changes.

They seem ok to me.

if d is None:
      data = cast(Dict[ComparisonOperators, RangeValT], kwargs)
else:
      data = d

If you wanna be super strict this cast is technically wrong since the validation that the keys are ComparisonOperators and not str happens in the following lines, but doing it this way does avoid the 2 casts. Maybe one day can be removed if mypys inference gets a bit stronger, but I don't think it makes a difference now, can keep it like this (I agree that it looks cleaner)

@Caiofcas
Copy link
Contributor Author

Looks good to me, thanks for the improvements!

@miguelgrinberg miguelgrinberg added the backport 8.x Backport to 8.x label Jun 17, 2024
@miguelgrinberg miguelgrinberg merged commit 2c79b48 into elastic:main Jun 17, 2024
17 checks passed
github-actions bot pushed a commit that referenced this pull request Jun 17, 2024
* refactor: add type hints to wrappers.py

* use _SupportsComparison type from typeshed

* escape imported types in quotes

* simplify casts

* fixed linter errors

---------

Co-authored-by: Miguel Grinberg <miguel.grinberg@gmail.com>
(cherry picked from commit 2c79b48)
@miguelgrinberg
Copy link
Collaborator

Thanks!

Note that I started #1845 to support using type hints when defining documents.

miguelgrinberg pushed a commit that referenced this pull request Jun 17, 2024
* refactor: add type hints to wrappers.py

* use _SupportsComparison type from typeshed

* escape imported types in quotes

* simplify casts

* fixed linter errors

---------

Co-authored-by: Miguel Grinberg <miguel.grinberg@gmail.com>
(cherry picked from commit 2c79b48)

Co-authored-by: Caio Fontes <38675540+Caiofcas@users.noreply.github.com>
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