Skip to content

Commit e028045

Browse files
authored
Add Github Action definition (#11320)
Define a Github Action that can be used as a canonical way of running mypy as part of Github CI workflows as easy as: ``` jobs: lint: runs-on: ubuntu-latest steps: - uses: python/mypy ``` The action is inspired by its equivalent in black and supports the following options: ``` with: options: "..." # mypy command-line options paths: "." # a list of paths to check version: "0.910" # mypy version to use install_types: yes|no # whether to auto-install stubs ```
1 parent 53c7bb2 commit e028045

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

action.yml

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: "Mypy"
2+
description: "Optional Static Typing for Python."
3+
author: "Jukka Lehtosalo and contributors"
4+
inputs:
5+
options:
6+
description: >
7+
Options passed to mypy. Use `mypy --help` to see available options.
8+
required: false
9+
paths:
10+
description: >
11+
Explicit paths to run mypy on. Defaults to the current directory.
12+
required: false
13+
default: "."
14+
version:
15+
description: >
16+
Mypy version to use (PEP440) - e.g. "0.910"
17+
required: false
18+
default: ""
19+
install_types:
20+
description: >
21+
Whether to automatically install missing library stub packages.
22+
('yes'|'no', default: 'yes')
23+
default: "yes"
24+
install_project_dependencies:
25+
description: >
26+
Whether to attempt to install project dependencies into mypy
27+
environment. ('yes'|'no', default: 'yes')
28+
default: "yes"
29+
branding:
30+
color: "blue"
31+
icon: "check-circle"
32+
runs:
33+
using: composite
34+
steps:
35+
- name: mypy setup
36+
shell: bash
37+
run: |
38+
echo ::group::Installing mypy...
39+
export PIP_DISABLE_PIP_VERSION_CHECK=1
40+
41+
if [ "$RUNNER_OS" == "Windows" ]; then
42+
HOST_PYTHON=python
43+
else
44+
HOST_PYTHON=python3
45+
fi
46+
47+
venv_script="import os.path; import venv; import sys;
48+
path = os.path.join(r'${{ github.action_path }}', '.mypy-venv');
49+
venv.main([path]);
50+
bin_subdir = 'Scripts' if sys.platform == 'win32' else 'bin';
51+
print(os.path.join(path, bin_subdir, 'python'));
52+
"
53+
54+
VENV_PYTHON=$(echo $venv_script | "$HOST_PYTHON")
55+
mypy_spec="mypy"
56+
57+
if [ -n "${{ inputs.version }}" ]; then
58+
mypy_spec+="==${{ inputs.version }}"
59+
fi
60+
61+
if ! "$VENV_PYTHON" -m pip install "$mypy_spec"; then
62+
echo "::error::Could not install mypy."
63+
exit 1
64+
fi
65+
echo ::endgroup::
66+
67+
if [ "${{ inputs.install_project_dependencies }}" == "yes" ]; then
68+
VENV=$("$VENV_PYTHON" -c 'import sys;print(sys.prefix)')
69+
echo ::group::Installing project dependencies...
70+
"$VENV_PYTHON" -m pip download --dest="$VENV"/deps .
71+
"$VENV_PYTHON" -m pip install -U --find-links="$VENV"/deps "$VENV"/deps/*
72+
echo ::endgroup::
73+
fi
74+
75+
echo ::group::Running mypy...
76+
mypy_opts=""
77+
if [ "${{ inputs.install_types }}" == "yes" ]; then
78+
mypy_opts+="--install-types --non-interactive"
79+
fi
80+
81+
echo "mypy $mypy_opts ${{ inputs.options }} ${{ inputs.paths }}"
82+
"$VENV_PYTHON" -m mypy $mypy_opts ${{ inputs.options }} ${{ inputs.paths }}
83+
echo ::endgroup::

0 commit comments

Comments
 (0)