| 1 | #!/usr/bin/env bash
|
| 2 | #
|
| 3 | # Usage:
|
| 4 | # types/run.sh <function name>
|
| 5 |
|
| 6 | set -o nounset
|
| 7 | set -o pipefail
|
| 8 | set -o errexit
|
| 9 |
|
| 10 | source devtools/common.sh
|
| 11 |
|
| 12 | readonly PY_PATH='.:vendor/' # note: could consolidate with other scripts
|
| 13 |
|
| 14 | deps() {
|
| 15 | set -x
|
| 16 | #pip install typing pyannotate
|
| 17 |
|
| 18 | # got error with 0.67.0
|
| 19 | #pip3 install 'mypy==0.660'
|
| 20 |
|
| 21 | # Without --upgrade, it won't install the latest version.
|
| 22 | # In .travis.yaml we apparently install the latest version too (?)
|
| 23 | pip3 install --user --upgrade 'mypy'
|
| 24 | }
|
| 25 |
|
| 26 | checkable-files() {
|
| 27 | # syntax_abbrev.py is "included" in _devbuild/gen/syntax_asdl.py; it's not a standalone module
|
| 28 | metrics/source-code.sh osh-files | grep -v syntax_abbrev.py
|
| 29 | metrics/source-code.sh oil-lang-files
|
| 30 | }
|
| 31 |
|
| 32 | need-typechecking() {
|
| 33 | # This command is useful to find files to annotate and add to
|
| 34 | # $MORE_OIL_MANIFEST.
|
| 35 | # It shows all the files that are not included in
|
| 36 | # $MORE_OIL_MANIFEST or $OSH_PARSE_MANIFEST, and thus are not yet
|
| 37 | # typechecked by typecheck-more-oil here or
|
| 38 | # `types/oil-slice.sh soil-run`.
|
| 39 |
|
| 40 | build/dynamic-deps.sh osh-eval
|
| 41 | echo
|
| 42 |
|
| 43 | comm -2 -3 \
|
| 44 | <(checkable-files | sort | grep '.py$') \
|
| 45 | <({ more-oil-manifest; cat _build/NINJA/osh_eval/typecheck.txt; } | sort) \
|
| 46 | | xargs wc -l | sort -n
|
| 47 | }
|
| 48 |
|
| 49 | readonly -a COMMON_TYPE_MODULES=(_devbuild/gen/runtime_asdl.py _devbuild/gen/syntax_asdl.py)
|
| 50 |
|
| 51 | add-imports() {
|
| 52 | # Temporary helper to add missing class imports to the 'if
|
| 53 | # TYPE_CHECKING:' block of a single module, if the relevant
|
| 54 | # classes are found in one of COMMON_TYPE_MODULES
|
| 55 |
|
| 56 | # Also, this saves the typechecking output to the file named by
|
| 57 | # $typecheck_out, to make it possible to avoid having to run two
|
| 58 | # redundant (and slow) typechecking commands. You can just cat that
|
| 59 | # file after running this function.
|
| 60 | local module=$1
|
| 61 | export PYTHONPATH=$PY_PATH
|
| 62 | readonly module_tmp=_tmp/add-imports-module.tmp
|
| 63 | readonly typecheck_out=_tmp/add-imports-typecheck-output
|
| 64 | set +o pipefail
|
| 65 | # unbuffer is just to preserve colorization (it tricks the command
|
| 66 | # into thinking it's writing to a pty instead of a pipe)
|
| 67 | unbuffer types/run.sh typecheck-files "$module" | tee "$typecheck_out" | \
|
| 68 | grep 'Name.*is not defined' | sed -r 's/.*'\''(\w+)'\''.*/\1/' | \
|
| 69 | sort -u | python devtools/findclassdefs.py "${COMMON_TYPE_MODULES[@]}" | \
|
| 70 | xargs python devtools/typeimports.py "$module" > "$module_tmp"
|
| 71 | set -o pipefail
|
| 72 |
|
| 73 | if ! diff -q "$module_tmp" "$module" > /dev/null
|
| 74 | then
|
| 75 | cp $module "_tmp/add-imports.$(basename $module).bak"
|
| 76 | mv "$module_tmp" "$module"
|
| 77 | echo "Updated $module"
|
| 78 | fi
|
| 79 | }
|
| 80 |
|
| 81 | #
|
| 82 | # PyAnnotate
|
| 83 | #
|
| 84 |
|
| 85 | # This has a bug
|
| 86 | #pyannotate() { ~/.local/bin/pyannotate "$@"; }
|
| 87 |
|
| 88 | readonly PYANN_REPO=~/git/oilshell/pyannotate/
|
| 89 |
|
| 90 | pyann-patched() {
|
| 91 | local tool=$PYANN_REPO/pyannotate_tools/annotations
|
| 92 | export PYTHONPATH=$PYANN_REPO
|
| 93 | # --dump can help
|
| 94 | python $tool "$@"
|
| 95 | }
|
| 96 |
|
| 97 | collect-types() {
|
| 98 | export PYTHONPATH=".:$PYANN_REPO"
|
| 99 | types/pyann_driver.py "$@"
|
| 100 |
|
| 101 | ls -l type_info.json
|
| 102 | wc -l type_info.json
|
| 103 | }
|
| 104 |
|
| 105 | osh-pyann() {
|
| 106 | export PYTHONPATH=".:$PYANN_REPO"
|
| 107 | PYANN_OUT='a1.json' bin/oil.py osh "$@"
|
| 108 | }
|
| 109 |
|
| 110 | pyann-demo() {
|
| 111 | rm -f -v *.json
|
| 112 | osh-pyann -c 'pushd /; echo hi; popd'
|
| 113 | ls -l *.json
|
| 114 | }
|
| 115 |
|
| 116 | pyann-interactive() {
|
| 117 | osh-pyann --rcfile /dev/null "$@"
|
| 118 | }
|
| 119 |
|
| 120 | pyann-spec-demo() {
|
| 121 | local dir=_tmp/pyann-spec
|
| 122 | mkdir -p $dir
|
| 123 | export OSH_LIST=bin/osh-pyann
|
| 124 | test/spec.sh assign --pyann-out-dir $dir "$@"
|
| 125 |
|
| 126 | ls -l $dir
|
| 127 | }
|
| 128 |
|
| 129 | peek-type-info() {
|
| 130 | grep path type_info.json | sort | uniq -c | sort -n
|
| 131 | }
|
| 132 |
|
| 133 | apply-types() {
|
| 134 | local json=${1:-type_info.json}
|
| 135 | shift
|
| 136 | local -a files=(osh/builtin_comp.py core/completion.py)
|
| 137 |
|
| 138 | #local -a files=( $(cat _tmp/osh-parse-src.txt | grep -v syntax_asdl.py ) )
|
| 139 |
|
| 140 | pyann-patched --type-info $json "${files[@]}" "$@"
|
| 141 | }
|
| 142 |
|
| 143 | apply-many() {
|
| 144 | for j in _tmp/pyann-spec/*.json; do
|
| 145 | apply-types $j -w
|
| 146 | done
|
| 147 | }
|
| 148 |
|
| 149 | sub() {
|
| 150 | local f=$1
|
| 151 | types/refactor.py sub < $f > _tmp/sub.txt
|
| 152 | diff -u _tmp/sub.txt $f
|
| 153 | }
|
| 154 |
|
| 155 | audit-hacks() {
|
| 156 | # I used a trailing _ in a couple places to indicates hacks
|
| 157 | # A MyPy upgrade might fix this?
|
| 158 | #egrep -n --context 1 '[a-z]+_ ' osh/*_parse.py
|
| 159 |
|
| 160 | # spids on base class issue
|
| 161 | egrep --color -n --context 1 '_temp' osh/*_parse.py
|
| 162 |
|
| 163 | echo ---
|
| 164 |
|
| 165 | # a few casts because Id ; is TokenWord.
|
| 166 | egrep --color -w 'cast' {osh,core,frontend}/*.py
|
| 167 |
|
| 168 | echo ---
|
| 169 |
|
| 170 | egrep --color -w 'type: ignore' {osh,core,frontend}/*.py
|
| 171 | }
|
| 172 |
|
| 173 | "$@"
|