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

Lint and type hints for REPL #1364

Merged
merged 11 commits into from
Feb 20, 2023
Merged

Lint and type hints for REPL #1364

merged 11 commits into from
Feb 20, 2023

Conversation

alexrudd2
Copy link
Collaborator

This removes all the mypy warnings for REPL, and eliminates a couple other lints also.

Unverified

The committer email address is not verified.

Unverified

The committer email address is not verified.

Unverified

The committer email address is not verified.

Unverified

The committer email address is not verified.

Unverified

The committer email address is not verified.

Unverified

The committer email address is not verified.

Unverified

The committer email address is not verified.

Unverified

The committer email address is not verified.

Unverified

The committer email address is not verified.

Unverified

The committer email address is not verified.
Copy link
Collaborator

@janiversen janiversen left a comment

Choose a reason for hiding this comment

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

@dhoomakethu can you please review the REPL parts carefully, I am not sure if there are side effects of these changes.

The SImulator changes are OK.


import click
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will not work !!! the import needs to be behind a try/except, otherwise you require click to be installed always (you will see the same in client/serial and other places).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The import is only needed for REPL, which can't be used anyway without click. the existing code which I removed calls sys.exit(1)

try:
import click
except ImportError:
print('click not installed!! Install with "pip install click"')
sys.exit(1)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes but we have had problems, latest with sqlalchemy because python loads the files and thus tries to do the import. The problem arises is the package is referenced (jnit) so for optional libraries we prefer to use try/except.

Having the try/except allows python to pass the module without having click installed (which is optional).

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is the "modern" way we handle optional packages:

try:
    import serial
except ImportError:
    pass

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

#1114 was the import error with pyserial, which was caused because it was imported in the generic server class. This is called by the normal server/client code paths.
#1319 was the import error with sqlalchemy and redis, which was caused because they were imported in the datastore. This is called by the normal server/client code paths.

I don't think this is true here, since no other code in the normal server/client code paths calls the REPL. If something did, it would already trigger the (existing) sys.exit(1). I have tested using the pymodbus client without click installed, and it's fine. So the Try/Except is not protecting anything.

As far as I can tell, if click is not installed the REPL is useless. So the Try/Except only changes the ImportError to a NameError.

I can add the Try/Except, of course, but don't think it has any use.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Your analysis is correct, I was merely preparing for the future where init contain all external classes, and deep links are not allowed.

Lets leave it for now.


import click
from prompt_toolkit import PromptSession, print_formatted_text
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should also be in try/except.

@janiversen
Copy link
Collaborator

Once the try/except is in place (in the modern way) I will approve/merge. Of course if dhoomakethu have comments before that they should be addressed, if not they can be addressed later.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@janiversen janiversen merged commit 031c457 into dev Feb 20, 2023
@janiversen janiversen deleted the repl-lint branch February 20, 2023 17:41
@janiversen
Copy link
Collaborator

As you might have noticed I work heavily in pymodbus/client (mainly base.py), so please do not make a lot of changes in that directory.

@alexrudd2
Copy link
Collaborator Author

As you might have noticed I work heavily in pymodbus/client (mainly base.py), so please do not make a lot of changes in that directory.

These are the current mypy problems there. I will let your work settle down before touching it.

pymodbus/client/base.py:131: error: Too many arguments for "ModbusFramer"  [call-arg]
pymodbus/client/base.py:188: error: "ModbusBaseClient" has no attribute "protocol"; maybe "use_protocol"?  [attr-defined]
pymodbus/client/base.py:190: error: "ModbusBaseClient" has no attribute "protocol"; maybe "use_protocol"?  [attr-defined]
pymodbus/client/base.py:191: error: "connect" of "ModbusBaseClient" does not return a value  [func-returns-value]

alexrudd2 added a commit to alexrudd2/pymodbus that referenced this pull request Feb 21, 2023
* avoid re-using `file` variable

* Click version >=8 dropped this warning for Python2

* Make `click` an explicit requirement

* annotations is already part of Python3.8

* fix flake8 warnings

* avoid redefining extra_args

* Fix mangled docstring

* Use slave argument

* Give default values, and fix values/write_registers mismatch

* `list` is native type, `List[...]` is a type hint

---------

Co-authored-by: jan iversen <jancasacondor@gmail.com>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants