-
Notifications
You must be signed in to change notification settings - Fork 162
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 initial type hints and run mypy in CI #339
Conversation
try: | ||
return super().to_internal_value(data) | ||
except ValidationError as err: | ||
if err.detail[0].code != 'no_match': | ||
if err.detail[0].code != 'no_match': # type: ignore[union-attr,index] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is a bit iffy. err.detail
isn't guaranteed to be a list of ErrorDetail
. It can be an arbitrarily nested combination of list
, dict
and ErrorDetail
.
@@ -1,6 +1,6 @@ | |||
""" | |||
Blank URLConf just to keep runtests.py happy. | |||
""" | |||
from rest_framework.compat import patterns | |||
from rest_framework.compat import patterns # type: ignore[attr-defined] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rest_framework.compat.patterns
doesn't actually exist, this crashes with ImportError
.
0c5303d
to
03e3fbc
Compare
This comment was marked as outdated.
This comment was marked as outdated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, I would try to keep typehints that enhance the legibility, not that enhance automated type checking. However, some compromise is possible in this front.
Instead of using stuff from typing
I would from __future__ import annotations
and go with str | None
and list
in opposition of Union[str, None]
and List[Any]
, for example.
Also, some parts for me confuses the reader more than helps them, like generics and so.
May be not the time to introduce Mypy as a linter. I am more leaned in typehints for humans.
I can contribute to this PR if you like, but am tight on time this last weeks.
Over and all, I really appreciate your the contribution.
Indeed, I hate the |
I think generics are useful for human understanding too. For example this tells the story "each
I can remove mypy linting from this repo. It was useful for double-checking that my added hints are correct. Though seems like a loss from my PoV. But some 3rd party projects that use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me, this PR is way more readable now. Am still concerned about some verbosity added but there is no much we can do about this iiuc.
Specifically about the cast
s, I would prefer to not include them, and mark only the result types instead.
Specifically about mypy
, I am in favor of letting it running in a non-mandatory way for PR merging. Or at least in a way very easy to turn to non-mandatory in the future.
I replied in 2 of the threads, that doesn't solve the issue. There are a few options: using
How to implement it as "non-mandatory"? Just a separate GitHub job, but still reporting the success/error status? Or ignoring its status entirely and always reporting success? |
I see :/ . Well, I prefer to go with
A separate GitHub job is better, as is just a GitHub config to let it mandatory or not. This way, the success/error status can be reported correctly always. |
# Type checking requirements | ||
mypy==1.10.0 | ||
django-stubs==5.0.0 | ||
djangorestframework-stubs==3.15.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Should these dependencies be included in
requirements-tox.txt
, as is now? - Should I create separate
requirements-mypy.txt
? - Should I embed them into
tox.ini
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am fine with any of the options, yet less files seems preferable.
I have split mypy into a separate job and removed some hints according to feedback. If what you mean by "non-mandatory" is jobs without these "Required" labels, then this is actually part of the repository admin confguration in GitHub web UI, not part of the workflow files. Jobs are "non-required" by default unless explicitly added there. So that part should be solved. |
That is great. Thanks a lot for the effort @intgr and the patience on the code review roundtrips. |
Thanks! Before this is usable for downstreams, we still need to add |
FYI: Is just released on PyPI a minor version with this PR included: https://pypi.org/project/drf-nested-routers/0.94.0/ |
Oops. Looks like I will need to release a patch very soon. |
Closes #337.