-
Notifications
You must be signed in to change notification settings - Fork 1k
Lint and type hints for REPL #1364
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b7fdd6b
avoid re-using `file` variable
alexrudd2 00a58ca
Click version >=8 dropped this warning for Python2
alexrudd2 848c18e
Make `click` an explicit requirement
alexrudd2 3c2a78d
annotations is already part of Python3.8
alexrudd2 c9c3699
fix flake8 warnings
alexrudd2 7a55f4b
avoid redefining extra_args
alexrudd2 c0fa491
Fix mangled docstring
alexrudd2 621f925
Use slave argument
alexrudd2 11d9c55
Give default values, and fix values/write_registers mismatch
alexrudd2 9ba05ee
`list` is native type, `List[...]` is a type hint
alexrudd2 898c43e
Merge branch 'dev' into repl-lint
janiversen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,9 @@ | ||
| """Pymodbus REPL Entry point.""" | ||
| # pylint: disable=anomalous-backslash-in-string | ||
| # flake8: noqa | ||
| import logging | ||
| import pathlib | ||
| import sys | ||
|
|
||
|
|
||
| try: | ||
| import click | ||
| except ImportError: | ||
| print('click not installed!! Install with "pip install click"') | ||
| sys.exit(1) | ||
| try: | ||
| from prompt_toolkit import PromptSession, print_formatted_text | ||
| except ImportError: | ||
| print( | ||
| "prompt toolkit is not installed!! " | ||
| 'Install with "pip install prompt_toolkit --upgrade"' | ||
| ) | ||
| sys.exit(1) | ||
|
|
||
| import click | ||
| from prompt_toolkit import PromptSession, print_formatted_text | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also be in try/except. |
||
| from prompt_toolkit.auto_suggest import AutoSuggestFromHistory | ||
| from prompt_toolkit.formatted_text import HTML | ||
| from prompt_toolkit.history import FileHistory | ||
|
|
@@ -46,9 +30,7 @@ | |
|
|
||
| _logger = logging.getLogger() | ||
|
|
||
| click.disable_unicode_literals_warning = True | ||
|
|
||
| TITLE = f""" | ||
| TITLE = rf""" | ||
alexrudd2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ---------------------------------------------------------------------------- | ||
| __________ _____ .___ __________ .__ | ||
| \______ \___.__. / \ ____ __| _/ \______ \ ____ ______ | | | ||
|
|
@@ -124,8 +106,8 @@ def convert(self, value, param, ctx): | |
| return None | ||
|
|
||
|
|
||
| def process_args(args: list, string: bool = True): | ||
| """Internal function to parse arguments provided on command line. | ||
| def _process_args(args: list, string: bool = True): | ||
| """Parse arguments provided on command line. | ||
|
|
||
| :param args: Array of argument values | ||
| :param string: True if arguments values are strings, false if argument values are integers | ||
|
|
@@ -226,7 +208,7 @@ def _(event): | |
| text = text.strip().split() | ||
| cmd = text[0].split(".")[1] | ||
| args = text[1:] | ||
| kwargs, execute = process_args(args, string=False) | ||
| kwargs, execute = _process_args(args, string=False) | ||
| if execute: | ||
| if text[0] in CLIENT_ATTRIBUTES: | ||
| result = Result(getattr(client, cmd)) | ||
|
|
@@ -242,7 +224,7 @@ def _(event): | |
| result.raw() | ||
| if words[0] == "result.decode": | ||
| args = words[1:] | ||
| kwargs, execute = process_args(args) | ||
| kwargs, execute = _process_args(args) | ||
| if execute: | ||
| result.decode(**kwargs) | ||
| except KeyboardInterrupt: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 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).
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.
The import is only needed for REPL, which can't be used anyway without
click. the existing code which I removed callssys.exit(1)pymodbus/pymodbus/repl/client/main.py
Lines 9 to 13 in d2c7c77
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.
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).
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 is the "modern" way we handle optional packages:
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.
#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
sqlalchemyandredis, 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 withoutclickinstalled, and it's fine. So the Try/Except is not protecting anything.As far as I can tell, if
clickis not installed the REPL is useless. So the Try/Except only changes theImportErrorto aNameError.I can add the Try/Except, of course, but don't think it has any 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.
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.