Skip to content

Commit

Permalink
[#215][Python/Pre-commit] Simplify the mypy Driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
pishoyg committed Sep 1, 2024
1 parent 5bd2052 commit 090d78d
Showing 1 changed file with 26 additions and 36 deletions.
62 changes: 26 additions & 36 deletions pre-commit/mypy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,41 @@
# NOTE: We can't simply run `mypy` for the whole repo because there is a
# bug with modules having duplicate names.
# See https://github.com/python/mypy/issues/10428.
# Instead, we define the prompt manually, to run at one repo at a time.
# We are currently in the process of migrating from the dynamic, slow,
# tedious, `type_enforced` to `mypy`.
# For each project that gets migrated, we should add a line to execute
# `mypy` on it here.

# NOTE: To guard against developers forgetting to include their Python
# projects in this list, we also have a generic line that runs `mypy`
# against all files that don't belong to any of the projects.
# Thus, every Python file will be either part of a project (in which case
# it should be ignored by the generic prompt), or should be covered by
# the generic prompt.
# Instead, we define the prompt manually, to run at one subproject at a time.

set -o errexit # Exit upon encountering a failure.
set -o nounset # Consider an undefined variable to be an error.

source .helpers

_mypy() {
PREFIX="${1}"
MATCHES="$(echo "${@:2}" | tr ' ' '\n' | _grep "^${PREFIX}")"
if [ -n "${MATCHES}" ]; then
# shellcheck disable=SC2046
mypy $(echo "${MATCHES}" | tr '\n' ' ') --check-untyped-defs
fi
# shellcheck disable=SC2046
mypy $(echo "${@}" | tr '\n' ' ') --check-untyped-defs
}

# TODO: (#215) Maintain one list of projects instead of duplicating it.
_mypy "bible" "${@}"
_mypy "dictionary/copticsite.com" "${@}"
_mypy "dictionary/kellia.uni-goettingen.de" "${@}"
_mypy "dictionary/marcion.sourceforge.net" "${@}"
_mypy "flashcards" "${@}"
_mypy "morphology" "${@}"
_mypy "site" "${@}"
SUBPROJECTS=(
"bible"
"dictionary/copticsite.com"
"dictionary/kellia.uni-goettingen.de"
"dictionary/marcion.sourceforge.net"
"flashcards"
"morphology"
"site"
)

NONMATCHES=$(echo "${@}" | tr ' ' '\n' \
| _grep --invert "^bible/" \
| _grep --invert "^dictionary/copticsite.com/" \
| _grep --invert "^dictionary/kellia.uni-goettingen.de/" \
| _grep --invert "^dictionary/marcion.sourceforge.net/" \
| _grep --invert "^flashcards/" \
| _grep --invert "^morphology/" \
| _grep --invert "^site/")
# Run `mypy` once against each of the prefixes above.
NONMATCHES=$(echo "${@}" | tr ' ' '\n')
for PREFIX in "${SUBPROJECTS[@]}"; do
MATCHES="$(echo "${@}" | tr ' ' '\n' | _grep "^${PREFIX}")"
if [ -n "${MATCHES}" ]; then
# NOTE: For a matching prefix, we run `mypy` against the entire subtree,
# not just the affected files.
_mypy "${PREFIX}"
fi
NONMATCHES=$(echo "${NONMATCHES}" | grep --invert "^${PREFIX}/")
done

# Run against the affected files that didn't match any of the prefixes above.
if [ -n "${NONMATCHES}" ]; then
# shellcheck disable=SC2046
mypy $(echo "${NONMATCHES}" | tr '\n' ' ') --check-untyped-defs
_mypy "${NONMATCHES}"
fi

0 comments on commit 090d78d

Please sign in to comment.