-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Python upscale: pass mypy and create library directory #3953
Python upscale: pass mypy and create library directory #3953
Conversation
.py files may be modules which are not standalone program, so allow them not to be executable. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Executable scripts must have shebang (#!) line to be effectively executable on most Unix-like systems. Enforce this, and conversely enforce that files with a shebang line are executable. Check that the specified interperter is consistent with the file extension. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Add enough type annotations to pass mypy 0.782 with Python 3.5. The source code will still run normally under older Python versions. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This makes the code cleaner. As a bonus, mypy no longer gets confused. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Since no typing stubs are available for mbed_host_tests.py, mypy errors out on mbedtls_test.py with error: Skipping analyzing 'mbed_host_tests': found module but no type hints or library stubs Ignore this import to get at least some benefit from mypy without spending significant effort to write stubs. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Allow Python scripts in tests/scripts to import modules located in the scripts directory. To do this, use ``` import scripts_path # pylint: disable=unused-import ``` Declare the scripts directory to pylint and to mypy. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Generalize the very ad hoc run_c function into a function to generate a C program to print the value of a list of expressions. Refactor the code into several functions to make it more manageable. No intended behavior change. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Create a directory mbedtls_dev intended to contain various Python module for use by Python scripts located anywhere in the Mbed TLS source tree. Move get_c_expression_values and its auxiliary functions into a new Python module mbedtls_dev.c_build_helper. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Tell mypy to support packages without an __init__.py (PEP 420 namespace packages). Python 3.3 and (modern) Pylint support them out of the box, but mypy needs to be told to support them. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
mypy automatically checks the modules when it encounters them as imports. Don't make it check them twice, because it would complain about encountering them through different paths (via the command line as scripts/mbedtls_dev/*.py and via imports as just mbedtls_dev/*.py). Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
With just the option --can-pylint or --can-mypy, check whether the requisite tool is available with an acceptable version and exit. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
d3084e2
to
faa8c06
Compare
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
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.
Looks almost good to me. I have only minor comments.
faa8c06
to
1cc6a8e
Compare
Since the Docker images in our CI have been updated with Pylint 2.4.4, I've rescinded faa8c06. I now expect the CI to pass. |
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Now that the script might additionally run mypy, it's more user-friendly to indicate what's going on at the beginning as well. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This reduces dependencies, doesn't require maintainers to know awk, and makes the version parsing more robust. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
As indicated in the comments in the can_mypy function, we don't just need a mypy executable to be present, we need it to work. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Make sure to run mypy with the desired Python version. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
0.780 works. The previous release, 0.770, does not. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
`tests/scripts/all.sh check_python_files` now runs mypy (in addition to pylint) if it's available. So install mypy. Install mypy 0.780, which is the earliest version that works on our code at this time. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
0849bfc
to
f71ff1f
Compare
@daverodgman are you still ok with the last version? |
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.
Latest updates look good
$PYTHON -m pylint -j 2 scripts/*.py tests/scripts/*.py | ||
check_version () { | ||
$PYTHON - "$2" <<EOF | ||
import packaging.version |
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 hadn't realized when I wrote this that packaging
is a third-party module.
This pull request combines several improvements to the structure of our Python code.
mypy
(version 0.780 or above).tests/scripts/check-python-files.sh
now runsmypy
if available. This PR makes mypy run on Travis. It doesn't yet run on Jenkins.tests/scripts
can now import scripts from the toplevelscripts
directory without undue hassle and without angering pylint or mypy.scripts/mbedtls_dev
.run_c
function fromtest_psa_constant_names.py
to a new modulembedtls_dev.c_build_helper
.Backports: not strictly necessary, but it would be better to backport the changes to the Python scripts that exist in LTS branches to keep them aligned and facilitate future backports.