Skip to content

Commit b686b61

Browse files
committed
Escalated mypy to use --strict option.
Use --strict in mypy to perform maximum type checking. Fixed the remaining errors.
1 parent 8963ef9 commit b686b61

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

Diff for: demo/python/astronomy.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@
3939
from typing import Any, List, Tuple, Optional, Union, Callable, Dict
4040

4141
def _cbrt(x: float) -> float:
42-
if x < 0.0:
43-
return -((-x) ** (1.0 / 3.0))
44-
return x ** (1.0 / 3.0)
42+
'''Returns the cube root of x.'''
43+
y = (x ** (1.0 / 3.0)) if (x >= 0.0) else -((-x) ** (1.0 / 3.0))
44+
# mypy knows that the exponentiation operator '**' can return
45+
# complex values in some cases. It doesn't realize that can't
46+
# happen here. To prevent type errors, explicitly check the type.
47+
if isinstance(y, float):
48+
return y
49+
raise InternalError()
4550

4651
KM_PER_AU = 1.4959787069098932e+8 #<const> The number of kilometers per astronomical unit.
4752
C_AUDAY = 173.1446326846693 #<const> The speed of light expressed in astronomical units per day.
@@ -10164,7 +10169,7 @@ def __init__(self, originBody: Body, time: Time, bodyStates: List[StateVector])
1016410169

1016510170
# Create a stub list of small body states that we will append to later.
1016610171
# We just need the stub to put into `self.curr`
10167-
smallBodyList: List = []
10172+
smallBodyList: List[_body_grav_calc_t] = []
1016810173

1016910174
# Calculate the states of the Sun and planets at the initial time.
1017010175
largeBodyDict = _CalcSolarSystem(time)

Diff for: generate/template/astronomy.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@
3939
from typing import Any, List, Tuple, Optional, Union, Callable, Dict
4040

4141
def _cbrt(x: float) -> float:
42-
if x < 0.0:
43-
return -((-x) ** (1.0 / 3.0))
44-
return x ** (1.0 / 3.0)
42+
'''Returns the cube root of x.'''
43+
y = (x ** (1.0 / 3.0)) if (x >= 0.0) else -((-x) ** (1.0 / 3.0))
44+
# mypy knows that the exponentiation operator '**' can return
45+
# complex values in some cases. It doesn't realize that can't
46+
# happen here. To prevent type errors, explicitly check the type.
47+
if isinstance(y, float):
48+
return y
49+
raise InternalError()
4550

4651
KM_PER_AU = 1.4959787069098932e+8 #<const> The number of kilometers per astronomical unit.
4752
C_AUDAY = 173.1446326846693 #<const> The speed of light expressed in astronomical units per day.
@@ -8207,7 +8212,7 @@ def __init__(self, originBody: Body, time: Time, bodyStates: List[StateVector])
82078212

82088213
# Create a stub list of small body states that we will append to later.
82098214
# We just need the stub to put into `self.curr`
8210-
smallBodyList: List = []
8215+
smallBodyList: List[_body_grav_calc_t] = []
82118216

82128217
# Calculate the states of the Sun and planets at the initial time.
82138218
largeBodyDict = _CalcSolarSystem(time)

Diff for: generate/unit_test_python

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ python3 -m pylint --init-hook="import sys; sys.setrecursionlimit(2000)" ../sour
1414

1515
echo "$0: running mypy"
1616
cd ../source/python/astronomy || Fail "error changing to Python source directory"
17-
mypy --disallow-untyped-defs --module astronomy || Fail "error checking types using mypy"
17+
mypy --strict --module astronomy || Fail "error checking types using mypy"
1818
cd ../../../generate || Fail "error changing back to generate directory"
1919
echo ""
2020

Diff for: source/python/astronomy/astronomy.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@
3939
from typing import Any, List, Tuple, Optional, Union, Callable, Dict
4040

4141
def _cbrt(x: float) -> float:
42-
if x < 0.0:
43-
return -((-x) ** (1.0 / 3.0))
44-
return x ** (1.0 / 3.0)
42+
'''Returns the cube root of x.'''
43+
y = (x ** (1.0 / 3.0)) if (x >= 0.0) else -((-x) ** (1.0 / 3.0))
44+
# mypy knows that the exponentiation operator '**' can return
45+
# complex values in some cases. It doesn't realize that can't
46+
# happen here. To prevent type errors, explicitly check the type.
47+
if isinstance(y, float):
48+
return y
49+
raise InternalError()
4550

4651
KM_PER_AU = 1.4959787069098932e+8 #<const> The number of kilometers per astronomical unit.
4752
C_AUDAY = 173.1446326846693 #<const> The speed of light expressed in astronomical units per day.
@@ -10164,7 +10169,7 @@ def __init__(self, originBody: Body, time: Time, bodyStates: List[StateVector])
1016410169

1016510170
# Create a stub list of small body states that we will append to later.
1016610171
# We just need the stub to put into `self.curr`
10167-
smallBodyList: List = []
10172+
smallBodyList: List[_body_grav_calc_t] = []
1016810173

1016910174
# Calculate the states of the Sun and planets at the initial time.
1017010175
largeBodyDict = _CalcSolarSystem(time)

0 commit comments

Comments
 (0)