-
Notifications
You must be signed in to change notification settings - Fork 801
Add Type hints to query.py
#1821
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
Conversation
💚 CLA has been signed |
chore: fix linting
@Caiofcas Awesome, thank you so much! I will review this over the next few days. |
Looks good overall, but I have a couple of notes/comments:
|
Thanks! was confused about what was the failure.
Yeah, had a look and it was not too much work to add them, so it probably makes sense to add them in this PR. The only thing is I had to add some "looser" type annotations to Don't know what the opinion of you guys is on these 'looser' but temporary annotations in exchange for a more reviewable PR, will defer to your judgement.
Added it.
Yeah, I don't like it as well but it did seem like to much to try to tackle now, since it would require more precise typing of DslBase and some of the baser classes like that. I think the Any allows people to handle it sufficiently well in their own checks. |
@Caiofcas I tested the type checking on Python 3.8, which is the oldest we support, and this is the output:
I think this isn't a huge deal because it is an internal part of the library. But if this was a public declaration it would be. What do you think about switching the type check task to use 3.8 so that we don't inadvertently start using newer typing constructs that will not work on the older Pythons? I guess we can also go for a matrix, but I don't believe I've seen other projects do that. Other than that I really have nothing more to suggest, so we can go ahead and merge this. |
@miguelgrinberg Yeah, i think it's best to keep it compatible, was trying to keep it that way for the other annotations. |
Thank you so much! |
* refactor: add type hints to query.py + type_checking in CI chore: fix linting * fix: fix typing for older versions of python * refactor: add typing to query tests * chore: add type_check to CI * fix: fix typing for older python versions * fix: fix python version for ci --------- Co-authored-by: Miguel Grinberg <[email protected]> (cherry picked from commit e68585b)
* refactor: add type hints to query.py + type_checking in CI chore: fix linting * fix: fix typing for older versions of python * refactor: add typing to query tests * chore: add type_check to CI * fix: fix typing for older python versions * fix: fix python version for ci --------- Co-authored-by: Miguel Grinberg <[email protected]> (cherry picked from commit e68585b) Co-authored-by: Caio Fontes <[email protected]>
Starts the process of adding type hints, as proposed in #1533 .
As this is the first PR in that sense some more general things are also added here, such as the
type_check
section innoxfile.py
. The implementation of it was based on https://sethmlarson.dev/tests-arent-enough-case-study-after-adding-types-to-urllib3 and linked issues in that blog post (specifically: urllib3/urllib3#1924 (comment)). The filtering of mypy errors allows us to use the--strict
flag while also implementing type hints incrementally.One other change in
noxfile.py
is adding theE704
code to the ignores list, as this conflicts with one line@overload def
statements, which is the default way black formats them (PyCQA/pycodestyle#1036 and PyCQA/flake8#1921). To me it seems harmless enough but don't know if this is the preferred solution.For the actual typing, there is one change that is worth mentioning in more detail:
In
query.py::Q
, the function performed a check to see ifname_or_query
was an instance ofcollections.abc.Mapping
, afterwards calling the methodscopy
andpopitem
on it. Both of these methods are not present on theMapping
base class, with thecopy
method not present in any collection.I changed the call to
.copy()
to a call tocopy.deepcopy
from the standardlib, and changed the check tocollections.abc.MutableMapping
since this is the class that defines the.popitem()
module.