romainsacchi / carculator

Prospective environmental and economic life cycle assessment of vehicles made blazing fast.
http://carculator.psi.ch
BSD 3-Clause "New" or "Revised" License
48 stars 14 forks source link

Return type hint in EnergyConsumptionModel.motive_energy_per_km breaks python < 3.9 #26

Closed tngTUDOR closed 2 years ago

tngTUDOR commented 2 years ago

Current

Tests with python 3.7 and 3.8 don't pass because of:

...
../../../../miniconda3/envs/premise-dev/lib/python3.8/site-packages/carculator/energy_consumption.py:135: in EnergyConsumptionModel
) -> tuple[Union[float, Any], Any, Union[float, Any]]:
E   TypeError: 'type' object is not subscriptable
/opt/conda/envs/py37/lib/python3.7/site-packages/carculator/energy_consumption.py:135: in EnergyConsumptionModel
) -> tuple[Union[float, Any], Any, Union[float, Any]]:
E   TypeError: 'type' object is not subscriptable

Expected

The readme of the repo says carculator is compatible with python 3.7 and onwards. If this is still the case, a fix for this bug is necessary.

tngTUDOR commented 2 years ago

Using the Tuple class instead of the basic type tuple should solve the issue:

diff --git a/carculator/energy_consumption.py b/carculator/energy_consumption.py
index b121caa..dfbc23e 100644
--- a/carculator/energy_consumption.py
+++ b/carculator/energy_consumption.py
@@ -4,7 +4,7 @@ one for calculating the auxiliary energy needs, and another one for calculating
 energy needs.
 """

-from typing import Any, Union
+from typing import Any, Union, Tuple

 import numexpr as ne
 import numpy as np
@@ -132,7 +132,7 @@ class EnergyConsumptionModel:
         frontal_area: Union[float, np.ndarray, xr.DataArray],
         sizes: Union[str, np.ndarray, xr.DataArray],
         motor_power: Union[float, np.ndarray, xr.DataArray] = 0,
-    ) -> tuple[Union[float, Any], Any, Union[float, Any]]:
+    ) -> Tuple[Union[float, Any], Any, Union[float, Any]]:
         """
         Calculate energy used and recuperated for a given vehicle per km driven.
romainsacchi commented 2 years ago

Thanks. Should be fixed now. I also setup github action to run pytest on 3.7, 3.8 and 3.9 https://github.com/romainsacchi/carculator/runs/5467871531?check_suite_focus=true