-
Notifications
You must be signed in to change notification settings - Fork 974
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
Enable mypy in CI #1388
Enable mypy in CI #1388
Conversation
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.
@janiversen I know you did not like adding adding type stubs as development dependencies, but I don't see another way to make it work.
Please have a look at the config I sent you #1360 Home Assistant uses mypy in a way where whole files are ignored, we should copy that way, otherwise e.g. base.py will continue to give problems (I will be making a lot of changes there, and do not want to fight with mypy every time). As far as I can see, we simply need to add the files to mypy.ini (keeping it for now at the level you want, by commenting out the parts not used now). By adding the files we remove the need for stubs, something I feel rather strongly about. The reason I am against stubs, if due to maintenance, the stub have its own version, meaning when we upgrade de main e.g. pyserial, we also need to secure that the stub is upgrade...a real PITA !!! Mypy is something (very) nice to have but is not mandatory, so it should not cause more maintenance (as with stubs). |
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.
what does the file py.typed do ? I cannot see references and it is empty.
pymodbus/client/base.py
Outdated
@@ -121,7 +121,7 @@ def __init__( | |||
self.params.kwargs = kwargs | |||
|
|||
# Common variables. | |||
self.framer = self.params.framer(ClientDecoder(), self) | |||
self.framer = self.params.framer(ClientDecoder(), self) # type: ignore |
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.
Please have the whole file ignored, since there will be a lot of changes over the next short time.
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 think it makes sense for whomever is working on a file to
(1) don't make type errors OR
(2) disable the type checking temporarily during the refactor.
This can be done by adding # type: ignore
of the file, I believe.
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.
Then please do that.
Adding ignore on a line basis in a file that is being modified is just putting more work towards your fellow maintainers.
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've left this below in the config so you can see how to ignore files. It's not needed currently.
[mypy]
# exclude = pymodbus/client/base.py
Does this work for you?
https://peps.python.org/pep-0561/ It's supposed to be empty, indicating that the module is typed, allowing other users/applications of the module to so static analysis across modules works. |
I looked at that config. There is currently no need to ignore files since
With this PR
The stub packages are automatically generated by a bot in These PyPI packages follow PEP 561 and are automatically released (multiple times a day, when needed) by typeshed internal machinery.
|
For instance, here is the blank file in https://github.com/pallets/click/blob/main/src/click/py.typed |
I did not know about py.typed, that is a good idea to add. I disagree with the stubs, but will not block the PR on that (my opinion is no better than your opinion). So once base.py is clear I will approve the PR, The best option is to ignore the file in the configuration, because that way we can easily see what is being ignored. Ignoring files in the configuration, also make it more transparent to activate new checks, when activating the check, the number of ignored files increases for a short while. |
Let me just explain in detail why base.py as file should be ignored. Whenever an ignore (that be flake/pylint or mypy) is used a line level, it signal that the code is correct and cannot be changed to avoid the ignore (like e.g. pylint “too many methods”). In case we do not want to refactor the code then it is better to ignore the whole file as a clear statement. Before I add an ignore I try to see if the code can be refactored. We do not want to refactor base.py right now (because that is being done in another PR), hence ignore the file. If we start adding ignore just to get green light from mypy, then the whole exercise is pointless, because the purpose is to get better and more stable code. Adding an ignore in the top of the file, which should only be used when the tool do not allow an ignore list in the setup. The reason is that an ignore list is very visible, whereas an ignore in a file is just one more. |
This reverts commit f09c4e7.
After the refactor, someone will have to go through and fix the mypy errors anyways. Actually, it was just easier to fix them now. (If you refactor again, OK, it didn't take long). |
Co-authored-by: jan iversen <jancasacondor@gmail.com>
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.
LGTM.
Three typing errors remain in with the default
mypy
config. They are all inbase.py
and can be ignored since it will be refactored soon.By setting
py.typed
in the module folder, other programs built on top ofpymodbus
can use its typing. This works locally on my system when installed withpython3 -m pip install -e .
. I'm not sure if this will happen automatically when installed from PyPI; we may need to look at thetypeshed
project.Current config: