Skip to content
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

Merged

Conversation

gilles-peskine-arm
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm commented Dec 10, 2020

This pull request combines several improvements to the structure of our Python code.

  • Pass mypy (version 0.780 or above).
  • tests/scripts/check-python-files.sh now runs mypy if available. This PR makes mypy run on Travis. It doesn't yet run on Jenkins.
  • Pylint 1.5.2 from Ubuntu 16.04 no longer works. Pylint 1.8.3 from Ubuntu 18.04 does.
  • Python scripts in tests/scripts can now import scripts from the toplevel scripts directory without undue hassle and without angering pylint or mypy.
  • Create a Python library directory for maintainer scripts: scripts/mbedtls_dev.
  • As a demonstration, and because I need it for some ongoing work, move the bulk of the run_c function from test_psa_constant_names.py to a new module mbedtls_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.

.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>
@gilles-peskine-arm gilles-peskine-arm added mbed TLS team needs-review Every commit must be reviewed by at least two team members, needs-backports Backports are missing or are pending review and approval. component-platform Portability layer and build scripts needs-reviewer This PR needs someone to pick it up for review labels Dec 10, 2020
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>
@ronald-cron-arm ronald-cron-arm self-requested a review January 12, 2021 18:00
daverodgman
daverodgman previously approved these changes Jan 12, 2021
Copy link
Contributor

@daverodgman daverodgman left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@ronald-cron-arm ronald-cron-arm left a 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.

@gilles-peskine-arm
Copy link
Contributor Author

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>
@ronald-cron-arm
Copy link
Contributor

@daverodgman are you still ok with the last version?

Copy link
Contributor

@daverodgman daverodgman left a 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

@ronald-cron-arm ronald-cron-arm merged commit 88a8035 into Mbed-TLS:development Jan 29, 2021
$PYTHON -m pylint -j 2 scripts/*.py tests/scripts/*.py
check_version () {
$PYTHON - "$2" <<EOF
import packaging.version
Copy link
Contributor Author

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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
component-platform Portability layer and build scripts needs-backports Backports are missing or are pending review and approval. needs-review Every commit must be reviewed by at least two team members,
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants