From b686b6185cea1aa100726c1f64d2bebd56ff4c7d Mon Sep 17 00:00:00 2001 From: Don Cross Date: Tue, 21 Feb 2023 15:34:52 -0500 Subject: [PATCH] Escalated mypy to use --strict option. Use --strict in mypy to perform maximum type checking. Fixed the remaining errors. --- demo/python/astronomy.py | 13 +++++++++---- generate/template/astronomy.py | 13 +++++++++---- generate/unit_test_python | 2 +- source/python/astronomy/astronomy.py | 13 +++++++++---- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/demo/python/astronomy.py b/demo/python/astronomy.py index 1b4b7599..5542ddfe 100644 --- a/demo/python/astronomy.py +++ b/demo/python/astronomy.py @@ -39,9 +39,14 @@ import abc from typing import Any, List, Tuple, Optional, Union, Callable, Dict def _cbrt(x: float) -> float: - if x < 0.0: - return -((-x) ** (1.0 / 3.0)) - return x ** (1.0 / 3.0) + '''Returns the cube root of x.''' + y = (x ** (1.0 / 3.0)) if (x >= 0.0) else -((-x) ** (1.0 / 3.0)) + # mypy knows that the exponentiation operator '**' can return + # complex values in some cases. It doesn't realize that can't + # happen here. To prevent type errors, explicitly check the type. + if isinstance(y, float): + return y + raise InternalError() KM_PER_AU = 1.4959787069098932e+8 # The number of kilometers per astronomical unit. C_AUDAY = 173.1446326846693 # The speed of light expressed in astronomical units per day. @@ -10164,7 +10169,7 @@ class GravitySimulator: # Create a stub list of small body states that we will append to later. # We just need the stub to put into `self.curr` - smallBodyList: List = [] + smallBodyList: List[_body_grav_calc_t] = [] # Calculate the states of the Sun and planets at the initial time. largeBodyDict = _CalcSolarSystem(time) diff --git a/generate/template/astronomy.py b/generate/template/astronomy.py index 83e9ef79..1a2b719e 100644 --- a/generate/template/astronomy.py +++ b/generate/template/astronomy.py @@ -39,9 +39,14 @@ import abc from typing import Any, List, Tuple, Optional, Union, Callable, Dict def _cbrt(x: float) -> float: - if x < 0.0: - return -((-x) ** (1.0 / 3.0)) - return x ** (1.0 / 3.0) + '''Returns the cube root of x.''' + y = (x ** (1.0 / 3.0)) if (x >= 0.0) else -((-x) ** (1.0 / 3.0)) + # mypy knows that the exponentiation operator '**' can return + # complex values in some cases. It doesn't realize that can't + # happen here. To prevent type errors, explicitly check the type. + if isinstance(y, float): + return y + raise InternalError() KM_PER_AU = 1.4959787069098932e+8 # The number of kilometers per astronomical unit. C_AUDAY = 173.1446326846693 # The speed of light expressed in astronomical units per day. @@ -8207,7 +8212,7 @@ class GravitySimulator: # Create a stub list of small body states that we will append to later. # We just need the stub to put into `self.curr` - smallBodyList: List = [] + smallBodyList: List[_body_grav_calc_t] = [] # Calculate the states of the Sun and planets at the initial time. largeBodyDict = _CalcSolarSystem(time) diff --git a/generate/unit_test_python b/generate/unit_test_python index 961c8e34..64733f79 100755 --- a/generate/unit_test_python +++ b/generate/unit_test_python @@ -14,7 +14,7 @@ python3 -m pylint --init-hook="import sys; sys.setrecursionlimit(2000)" ../sour echo "$0: running mypy" cd ../source/python/astronomy || Fail "error changing to Python source directory" -mypy --disallow-untyped-defs --module astronomy || Fail "error checking types using mypy" +mypy --strict --module astronomy || Fail "error checking types using mypy" cd ../../../generate || Fail "error changing back to generate directory" echo "" diff --git a/source/python/astronomy/astronomy.py b/source/python/astronomy/astronomy.py index 1b4b7599..5542ddfe 100644 --- a/source/python/astronomy/astronomy.py +++ b/source/python/astronomy/astronomy.py @@ -39,9 +39,14 @@ import abc from typing import Any, List, Tuple, Optional, Union, Callable, Dict def _cbrt(x: float) -> float: - if x < 0.0: - return -((-x) ** (1.0 / 3.0)) - return x ** (1.0 / 3.0) + '''Returns the cube root of x.''' + y = (x ** (1.0 / 3.0)) if (x >= 0.0) else -((-x) ** (1.0 / 3.0)) + # mypy knows that the exponentiation operator '**' can return + # complex values in some cases. It doesn't realize that can't + # happen here. To prevent type errors, explicitly check the type. + if isinstance(y, float): + return y + raise InternalError() KM_PER_AU = 1.4959787069098932e+8 # The number of kilometers per astronomical unit. C_AUDAY = 173.1446326846693 # The speed of light expressed in astronomical units per day. @@ -10164,7 +10169,7 @@ class GravitySimulator: # Create a stub list of small body states that we will append to later. # We just need the stub to put into `self.curr` - smallBodyList: List = [] + smallBodyList: List[_body_grav_calc_t] = [] # Calculate the states of the Sun and planets at the initial time. largeBodyDict = _CalcSolarSystem(time)