Skip to content

Commit c073712

Browse files
committed
Switch to pyright
mypy isn't able to infer types as well as pyright, and disallows some useful capabilites such as using type variables in non-static contexts (e.g. avoids https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases). This adds a node dependency to the project, which is a downside, but I believe is the right way to go for more advanced type usage than mypy will allow
1 parent 71cba8a commit c073712

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

scripts/pyright

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/bin/bash
2+
PATH_TO_PYRIGHT=`which pyright`
3+
4+
vercomp () {
5+
if [[ $1 == $2 ]]
6+
then
7+
return 0
8+
fi
9+
local IFS=.
10+
local i ver1=($1) ver2=($2)
11+
# fill empty fields in ver1 with zeros
12+
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
13+
do
14+
ver1[i]=0
15+
done
16+
for ((i=0; i<${#ver1[@]}; i++))
17+
do
18+
if [[ -z ${ver2[i]} ]]
19+
then
20+
# fill empty fields in ver2 with zeros
21+
ver2[i]=0
22+
fi
23+
if ((10#${ver1[i]} > 10#${ver2[i]}))
24+
then
25+
return 1
26+
fi
27+
if ((10#${ver1[i]} < 10#${ver2[i]}))
28+
then
29+
return 2
30+
fi
31+
done
32+
return 0
33+
}
34+
35+
# Node version check
36+
echo "Checking node version..."
37+
NODE_VERSION=`node -v | cut -d'v' -f2`
38+
MIN_NODE_VERSION="10.15.2"
39+
vercomp $MIN_NODE_VERSION $NODE_VERSION
40+
# 1 == gt
41+
if [[ $? -eq 1 ]]; then
42+
echo "Node version ${NODE_VERSION} too old, min expected is ${MIN_NODE_VERSION}, run:"
43+
echo " npm -g upgrade node"
44+
exit -1
45+
fi
46+
47+
# Do we need to sudo?
48+
echo "Checking node_modules dir..."
49+
NODE_MODULES=`npm -g root`
50+
SUDO="sudo"
51+
if [ -w "$NODE_MODULES" ]; then
52+
SUDO="" #nop
53+
fi
54+
55+
# If we can't find pyright, install it.
56+
echo "Checking pyright exists..."
57+
if [ -z "$PATH_TO_PYRIGHT" ]; then
58+
echo "...installing pyright"
59+
${SUDO} npm install -g pyright
60+
else
61+
# already installed, upgrade to make sure it's current
62+
# this avoids a sudo on launch if we're already current
63+
echo "Checking pyright version..."
64+
CURRENT=`pyright --version | cut -d' ' -f2`
65+
REMOTE=`npm info pyright version`
66+
if [ "$CURRENT" != "$REMOTE" ]; then
67+
echo "...new version of pyright found, upgrading."
68+
${SUDO} npm upgrade -g pyright
69+
fi
70+
fi
71+
72+
echo "done."
73+
pyright "$@"

scripts/test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
1818
usage
1919
else
2020
# Types
21-
mypy pystac
21+
scripts/pyright pystac tests
2222

2323
# Lint
2424
flake8 pystac tests

tests/extensions/test_sar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ def test_polarization_must_be_list(self):
104104
polarizations = sar.Polarization.HV
105105
product_type: str = 'Some product'
106106
with self.assertRaises(ps.STACError):
107-
SarExtension.ext(self.item).apply(mode, frequency_band, polarizations,
108-
product_type) # type:ignore
107+
SarExtension.ext(self.item).apply(mode, frequency_band, polarizations, # type:ignore
108+
product_type)
109109

110110

111111
if __name__ == '__main__':

0 commit comments

Comments
 (0)