Skip to content

Commit

Permalink
added mypy-diff and mypy-commit targets to makefile (#1511)
Browse files Browse the repository at this point in the history
* added mypy-diff and mypy-commit targets to makefile
  • Loading branch information
sanderr authored and wouterdb committed Oct 24, 2019
1 parent 6ea0554 commit d0aff50
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ charset = utf-8
[*.py]
max_line_length = 128

[Makefile]
indent_style = tab
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ mypy
types
mypy.xml
coverage
.mypy-baseline

# Duplicate Files for text_common package, copied by tests_common/copy_files_from_core.py
tests_common/src/inmanta_tests/conftest.py
Expand Down
43 changes: 41 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,48 @@ pep8:
pip install -c requirements.txt pep8-naming flake8-black flake8-isort
flake8 src tests tests_common

.PHONY: mypy
.PHONY: mypy mypy-diff mypy-save
RUN_MYPY=MYPYPATH=stubs:src python -m mypy --html-report mypy -p inmanta

mypy:
MYPYPATH=stubs:src python -m mypy --html-report mypy -p inmanta
$(RUN_MYPY)

# baseline file mypy-diff will compare to
MYPY_BASELINE_FILE=.mypy-baseline
# temporary file used to store most recent mypy run
MYPY_TMP_FILE=.mypy-tmp
# temporary file used to store baseline with line numbers filtered out
MYPY_BASELINE_FILE_NO_LN_NB=$(MYPY_BASELINE_FILE).nolnnb
# prepare file for diff: remove last 2 lines and filter out line numbers
MYPY_DIFF_PREPARE=head -n -2 | sed 's/^\(.\+:\)[0-9]\+\(:\)/\1\2/'
# read old/new line number (format +n for new or -n for old) from stdin and transform to old/new line
MYPY_DIFF_FETCH_LINES=xargs -I{} sh -c 'sed -n -e "s/^/$(MYPY_SET_COLOUR)$$(echo {} | cut -c 1 -) /" -e "$$(echo {} | cut -c 2- -)p" $(MYPY_SELECT_FILE)'
MYPY_SELECT_FILE=$$(if [[ "{}" == +* ]]; then echo $(MYPY_TMP_FILE); else echo $(MYPY_BASELINE_FILE); fi)
MYPY_SET_COLOUR=$$(if [[ "{}" == +* ]]; then tput setaf 1; else tput setaf 2; fi)
# diff line format options
LFMT_LINE_NB=%dn
LFMT_NEWLINE=%c'\\012'

# compare mypy output with baseline file, show newly introduced and resolved type errors
mypy-diff:
@ # run mypy and temporarily save result
@ $(RUN_MYPY) > $(MYPY_TMP_FILE) || true
@ # prepare baseline for diff and temporarily save result
@ cat $(MYPY_BASELINE_FILE) | $(MYPY_DIFF_PREPARE) > $(MYPY_BASELINE_FILE_NO_LN_NB) || true
@ # prepare most recent mypy output and run diff, returing +n for new lines and -n for old lines, where n is the line number
@ cat $(MYPY_TMP_FILE) | $(MYPY_DIFF_PREPARE) | diff $(MYPY_BASELINE_FILE_NO_LN_NB) - \
--new-line-format="+$(LFMT_LINE_NB)$(LFMT_NEWLINE)" \
--old-line-format="-$(LFMT_LINE_NB)$(LFMT_NEWLINE)" \
--unchanged-line-format='' \
--unidirectional-new-file \
| $(MYPY_DIFF_FETCH_LINES) \
|| true
@ # cleanup
@ rm -f $(MYPY_TMP_FILE) $(MYPY_BASELINE_FILE_NO_LN_NB)

# save mypy output to baseline file
mypy-save:
$(RUN_MYPY) > $(MYPY_BASELINE_FILE) || true

.PHONY: test
test:
Expand Down

0 comments on commit d0aff50

Please sign in to comment.