Skip to content

Commit 55c78e2

Browse files
authored
Merge pull request #6 from cdce8p/dev
Release v0.2.0
2 parents 2eac84a + 549997b commit 55c78e2

File tree

105 files changed

+2247
-76
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+2247
-76
lines changed

.github/workflows/ci.yaml

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- dev
8+
pull_request: ~
9+
10+
env:
11+
CACHE_VERSION: 0
12+
DEFAULT_PYTHON: 3.8
13+
LIB_FOLDER: python_typing_update
14+
PRE_COMMIT_CACHE: ~/.cache/pre-commit
15+
16+
17+
jobs:
18+
prepare-base:
19+
name: Prepare base dependencies
20+
runs-on: ubuntu-latest
21+
outputs:
22+
python-key: ${{ steps.generate-python-key.outputs.key }}
23+
pre-commit-key: ${{ steps.generate-pre-commit-key.outputs.key }}
24+
steps:
25+
- name: Check out code from GitHub
26+
uses: actions/[email protected]
27+
- name: Set up Python ${{ env.DEFAULT_PYTHON }}
28+
id: python
29+
uses: actions/[email protected]
30+
with:
31+
python-version: ${{ env.DEFAULT_PYTHON }}
32+
- name: Generate partial Python venv restore key
33+
id: generate-python-key
34+
run: >-
35+
echo "::set-output name=key::base-venv-${{ env.CACHE_VERSION }}-${{
36+
hashFiles('requirements.txt', 'requirements_test.txt',
37+
'requirements_test_pre_commit.txt') }}"
38+
- name: Restore Python virtual environment
39+
id: cache-venv
40+
uses: actions/[email protected]
41+
with:
42+
path: venv
43+
key: >-
44+
${{ runner.os }}-${{ steps.python.outputs.python-version }}-${{
45+
steps.generate-python-key.outputs.key }}
46+
restore-keys: |
47+
${{ runner.os }}-${{ steps.python.outputs.python-version }}-base-venv-${{ env.CACHE_VERSION }}-
48+
- name: Create Python virtual environment
49+
if: steps.cache-venv.outputs.cache-hit != 'true'
50+
run: |
51+
python -m venv venv
52+
. venv/bin/activate
53+
python -m pip install -U pip setuptools wheel
54+
pip install -U -r requirements_test.txt
55+
pip install -e .
56+
- name: Generate pre-commit restore key
57+
id: generate-pre-commit-key
58+
run: >-
59+
echo "::set-output name=key::pre-commit-${{ env.CACHE_VERSION }}-${{
60+
hashFiles('.pre-commit-config.yaml') }}"
61+
- name: Restore pre-commit environment
62+
id: cache-precommit
63+
uses: actions/[email protected]
64+
with:
65+
path: ${{ env.PRE_COMMIT_CACHE }}
66+
key: >-
67+
${{ runner.os }}-${{ steps.generate-pre-commit-key.outputs.key }}
68+
restore-keys: |
69+
${{ runner.os }}-pre-commit-${{ env.CACHE_VERSION }}-
70+
- name: Install pre-commit dependencies
71+
if: steps.cache-precommit.outputs.cache-hit != 'true'
72+
run: |
73+
. venv/bin/activate
74+
pre-commit install --install-hooks
75+
76+
formatting:
77+
name: Run pre-commit checks
78+
runs-on: ubuntu-latest
79+
needs: prepare-base
80+
steps:
81+
- name: Check out code from GitHub
82+
uses: actions/[email protected]
83+
- name: Set up Python ${{ env.DEFAULT_PYTHON }}
84+
id: python
85+
uses: actions/[email protected]
86+
with:
87+
python-version: ${{ env.DEFAULT_PYTHON }}
88+
- name: Restore Python virtual environment
89+
id: cache-venv
90+
uses: actions/[email protected]
91+
with:
92+
path: venv
93+
key: ${{ runner.os }}-${{ steps.python.outputs.python-version }}-${{
94+
needs.prepare-base.outputs.python-key }}
95+
- name: Fail job if Python cache restore failed
96+
if: steps.cache-venv.outputs.cache-hit != 'true'
97+
run: |
98+
echo "Failed to restore Python venv from cache"
99+
exit 1
100+
- name: Restore pre-commit environment
101+
id: cache-precommit
102+
uses: actions/[email protected]
103+
with:
104+
path: ${{ env.PRE_COMMIT_CACHE }}
105+
key: ${{ runner.os }}-${{ needs.prepare-base.outputs.pre-commit-key }}
106+
- name: Fail job if pre-commit cache restore failed
107+
if: steps.cache-precommit.outputs.cache-hit != 'true'
108+
run: |
109+
echo "Failed to restore pre-commit environment from cache"
110+
exit 1
111+
- name: Run formatting check
112+
run: |
113+
. venv/bin/activate
114+
pip install -e .
115+
pre-commit run --all-files
116+
117+
pylint:
118+
name: Check pylint
119+
runs-on: ubuntu-latest
120+
needs: prepare-base
121+
steps:
122+
- name: Check out code from GitHub
123+
uses: actions/[email protected]
124+
- name: Set up Python ${{ env.DEFAULT_PYTHON }}
125+
id: python
126+
uses: actions/[email protected]
127+
with:
128+
python-version: ${{ env.DEFAULT_PYTHON }}
129+
- name: Restore Python virtual environment
130+
id: cache-venv
131+
uses: actions/[email protected]
132+
with:
133+
path: venv
134+
key: ${{ runner.os }}-${{ steps.python.outputs.python-version }}-${{
135+
needs.prepare-base.outputs.python-key }}
136+
- name: Fail job if Python cache restore failed
137+
if: steps.cache-venv.outputs.cache-hit != 'true'
138+
run: |
139+
echo "Failed to restore Python venv from cache"
140+
exit 1
141+
- name: Run pylint
142+
run: |
143+
. venv/bin/activate
144+
pip install -e .
145+
pylint ${{ env.LIB_FOLDER }} tests
146+
147+
mypy:
148+
name: Check mypy
149+
runs-on: ubuntu-latest
150+
needs: prepare-base
151+
steps:
152+
- name: Check out code from GitHub
153+
uses: actions/[email protected]
154+
- name: Set up Python ${{ env.DEFAULT_PYTHON }}
155+
id: python
156+
uses: actions/[email protected]
157+
with:
158+
python-version: ${{ env.DEFAULT_PYTHON }}
159+
- name: Restore Python virtual environment
160+
id: cache-venv
161+
uses: actions/[email protected]
162+
with:
163+
path: venv
164+
key: ${{ runner.os }}-${{ steps.python.outputs.python-version }}-${{
165+
needs.prepare-base.outputs.python-key }}
166+
- name: Fail job if Python cache restore failed
167+
if: steps.cache-venv.outputs.cache-hit != 'true'
168+
run: |
169+
echo "Failed to restore Python venv from cache"
170+
exit 1
171+
- name: Run mypy
172+
run: |
173+
. venv/bin/activate
174+
mypy ${{ env.LIB_FOLDER }} tests
175+
176+
177+
prepare-tests-linux:
178+
name: Prepare tests for Python ${{ matrix.python-version }} (Linux)
179+
runs-on: ubuntu-latest
180+
strategy:
181+
matrix:
182+
python-version: [3.8, 3.9]
183+
outputs:
184+
python-key: ${{ steps.generate-python-key.outputs.key }}
185+
steps:
186+
- name: Check out code from GitHub
187+
uses: actions/[email protected]
188+
- name: Set up Python ${{ matrix.python-version }}
189+
id: python
190+
uses: actions/[email protected]
191+
with:
192+
python-version: ${{ matrix.python-version }}
193+
- name: Generate partial Python venv restore key
194+
id: generate-python-key
195+
run: >-
196+
echo "::set-output name=key::venv-${{ env.CACHE_VERSION }}-${{
197+
hashFiles('requirements.txt', 'requirements_test.txt',
198+
'requirements_test_pre_commit.txt') }}"
199+
- name: Restore Python virtual environment
200+
id: cache-venv
201+
uses: actions/[email protected]
202+
with:
203+
path: venv
204+
key: >-
205+
${{ runner.os }}-${{ steps.python.outputs.python-version }}-${{
206+
steps.generate-python-key.outputs.key }}
207+
restore-keys: |
208+
${{ runner.os }}-${{ steps.python.outputs.python-version }}-venv-${{ env.CACHE_VERSION }}-
209+
- name: Create Python virtual environment
210+
if: steps.cache-venv.outputs.cache-hit != 'true'
211+
run: |
212+
python -m venv venv
213+
. venv/bin/activate
214+
python -m pip install -U pip setuptools wheel
215+
pip install -U -r requirements_test.txt
216+
pip install -e .
217+
218+
pytest-linux:
219+
name: Run tests Python ${{ matrix.python-version }} (Linux)
220+
runs-on: ubuntu-latest
221+
needs: prepare-tests-linux
222+
strategy:
223+
fail-fast: false
224+
matrix:
225+
python-version: [3.8, 3.9]
226+
steps:
227+
- name: Check out code from GitHub
228+
uses: actions/[email protected]
229+
- name: Set up Python ${{ matrix.python-version }}
230+
id: python
231+
uses: actions/[email protected]
232+
with:
233+
python-version: ${{ matrix.python-version }}
234+
- name: Restore Python virtual environment
235+
id: cache-venv
236+
uses: actions/[email protected]
237+
with:
238+
path: venv
239+
key: ${{ runner.os }}-${{ steps.python.outputs.python-version }}-${{
240+
needs.prepare-tests-linux.outputs.python-key }}
241+
- name: Fail job if Python cache restore failed
242+
if: steps.cache-venv.outputs.cache-hit != 'true'
243+
run: |
244+
echo "Failed to restore Python venv from cache"
245+
exit 1
246+
- name: Run pytest
247+
run: |
248+
. venv/bin/activate
249+
pytest tests/

.pre-commit-config.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
repos:
2+
- repo: https://github.com/asottile/pyupgrade
3+
rev: v2.10.1
4+
hooks:
5+
- id: pyupgrade
6+
args: [--py38-plus]
7+
exclude: &fixtures ^tests/fixtures/.+\.py$
8+
- repo: https://gitlab.com/pycqa/flake8
9+
rev: 3.9.0
10+
hooks:
11+
- id: flake8
12+
files: ^(python_typing_update|script|tests)/.+\.py$
13+
exclude: *fixtures
14+
- repo: https://github.com/PyCQA/isort
15+
rev: 5.7.0
16+
hooks:
17+
- id: isort
18+
exclude: *fixtures
19+
- repo: https://github.com/pre-commit/pre-commit-hooks
20+
rev: v3.4.0
21+
hooks:
22+
- id: trailing-whitespace
23+
exclude: .+\.md
24+
- id: end-of-file-fixer

.vscode/settings.default.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
"editor.insertSpaces": true,
44
"files.trimFinalNewlines": true,
55
"files.insertFinalNewline": true,
6+
"python.linting.enabled": true,
7+
"python.linting.flake8Enabled": true,
8+
"python.linting.pylintEnabled": true,
9+
"python.linting.mypyEnabled": true,
10+
"python.testing.pytestArgs": [],
11+
"python.testing.unittestEnabled": false,
12+
"python.testing.nosetestsEnabled": false,
13+
"python.testing.pytestEnabled": true,
614
"[markdown]": {
715
"files.trimTrailingWhitespace": false
816
},

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Python typing update
22

33
Small tool to update Python typing syntax.
4-
It uses
4+
It uses token analysis and
55
- [python-reorder-imports][pri]
66
- [pyupgrade][pyu]
77
- [isort][isort]
@@ -62,6 +62,13 @@ pre-commit run --hook-stage manual python-typing-update --all-files
6262
**`--verbose`**
6363
Always print verbose logging.
6464

65+
**`--limit`**
66+
Max number of files that should be changed. No performance improvements,
67+
since the limit is only applied **after** all files have been processed.
68+
69+
**`--concurrent-files`**
70+
Number of files to process concurrently during initial load.
71+
6572
**`--full-reorder`**
6673
Use additional options from [python-reorder-imports][pri] to rewrite
6774
- `--py38-plus` (default): Imports from `mypy_extensions` and `typing_extensions` when possible.
@@ -77,6 +84,10 @@ Check if files would be modified. Return with exitcode `1` or `0` if not. Useful
7784
Don't revert changes if a modified comment is detected.
7885
Check `git diff` before committing!
7986

87+
**`--only-force`**
88+
Only update files which are likely to require extra work.
89+
Check `git diff` before committing!
90+
8091
**`--py39-plus`**
8192
Set the minimum Python syntax to **3.9**. (Default: **3.8**)
8293

pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ disable=
2828
max-line-length=119
2929

3030
[REPORTS]
31-
score = false
31+
score = no

python_typing_update/__main__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,21 @@
1313
logger = logging.getLogger(__name__)
1414

1515

16+
class CustomHelpFormatter(argparse.HelpFormatter):
17+
def __init__(
18+
self, prog: str, indent_increment: int = 2,
19+
max_help_position: int = 24, width: int | None = None,
20+
) -> None:
21+
max_help_position = 40
22+
super().__init__(
23+
prog, indent_increment=indent_increment,
24+
max_help_position=max_help_position, width=width)
25+
26+
1627
async def async_main(argv: list[str] | None = None) -> int:
1728
parser = argparse.ArgumentParser(
1829
description="Tool to update Python typing syntax.",
30+
formatter_class=CustomHelpFormatter,
1931
)
2032
parser.add_argument(
2133
'-v', '--verbose',
@@ -26,6 +38,14 @@ async def async_main(argv: list[str] | None = None) -> int:
2638
'filenames',
2739
nargs='+',
2840
)
41+
parser.add_argument(
42+
'--limit', type=int, default=0,
43+
help="Max number of files that should be changed. No performance improvement!",
44+
)
45+
parser.add_argument(
46+
'--concurrent-files', metavar="NUM", type=int, default=100,
47+
help="Number of files to process concurrently during initial load. (default: %(default)s)"
48+
)
2949
parser.add_argument(
3050
'--full-reorder',
3151
action='store_true',
@@ -54,6 +74,11 @@ async def async_main(argv: list[str] | None = None) -> int:
5474
action='store_true',
5575
help="Update all files. Double check changes afterwards!",
5676
)
77+
group1.add_argument(
78+
'--only-force',
79+
action='store_true',
80+
help="Only update files which are likely to require extra work",
81+
)
5782

5883
group2 = parser.add_mutually_exclusive_group()
5984
group2.add_argument(

0 commit comments

Comments
 (0)