Skip to content

Commit 9b00149

Browse files
committed
Require "parse" subcommand when calling
We intend to add more functionality to this tool, so it no longer makes sense to have the error parsing functionality at the top level when calling the script. This change introduces (and requires) a new "parse" subcommand, under which the existing parsing function is moved. Because we're using argparse to do this, we also get a new "--help" flag on the command.
1 parent 605c28f commit 9b00149

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- *Action required:* Move existing behaviour under "parse" subcommand.
6+
Invocations of `mypy-json-report` should now be replaced with `mypy-json-report parse`.
57
- Use GA version of Python 3.11 in test matrix.
68

79
## v0.1.3 [2022-09-07]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Pipe the output of mypy through the `mypy-json-report` CLI app.
1414
Store the output to a file, and commit it to your git repo.
1515

1616
```
17-
mypy . --strict | mypy-json-report > known-mypy-errors.json
17+
mypy . --strict | mypy-json-report parse > known-mypy-errors.json
1818
git add known-mypy-errors.json
1919
git commit -m "Add mypy errors lockfile"
2020
```
@@ -80,7 +80,7 @@ jobs:
8080
8181
- name: Run mypy
8282
run: |
83-
mypy . --strict | mypy-json-report > known-mypy-errors.json
83+
mypy . --strict | mypy-json-report parse > known-mypy-errors.json
8484
8585
- name: Check for mypy changes
8686
run: |

mypy_json_report.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,45 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import argparse
16+
import enum
1517
import json
1618
import sys
1719
from collections import Counter, defaultdict
1820
from dataclasses import dataclass
1921
from typing import Counter as CounterType, Dict, Iterator
2022

2123

24+
class ErrorCodes(enum.IntEnum):
25+
DEPRECATED = 1
26+
27+
2228
def main() -> None:
29+
parser = argparse.ArgumentParser()
30+
subparsers = parser.add_subparsers(title="subcommand")
31+
32+
parser.set_defaults(func=_deprecated_command)
33+
34+
parse_parser = subparsers.add_parser(
35+
"parse", help="Transform Mypy output into JSON."
36+
)
37+
38+
parse_parser.set_defaults(func=_parser_command)
39+
40+
args = sys.argv[1:]
41+
parsed = parser.parse_args(args)
42+
parsed.func(parsed)
43+
44+
45+
def _parser_command(args: object) -> None:
2346
report_errors()
2447

2548

49+
def _deprecated_command(args: object) -> None:
50+
print("A subcommand is required. Pass --help for usage info.")
51+
sys.exit(ErrorCodes.DEPRECATED)
52+
53+
2654
def report_errors() -> None:
2755
errors = parse_errors_report(sys.stdin)
2856
error_json = json.dumps(errors, sort_keys=True, indent=2)

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ allowlist_externals =
3939
commands_pre =
4040
poetry install
4141
commands =
42-
poetry run bash -c "mypy . --strict | mypy-json-report > known-mypy-errors.json"
42+
poetry run bash -c "mypy . --strict | mypy-json-report parse > known-mypy-errors.json"
4343
git diff --exit-code known-mypy-errors.json

0 commit comments

Comments
 (0)