GreenDelta / olca-ipc.py

Mozilla Public License 2.0
29 stars 17 forks source link

TypeError with Python annotations #18

Closed dt-woods closed 6 months ago

dt-woods commented 2 years ago

When using USEPA's electricityLCI Python package, olca is imported via fedelemflowlist. When running the script through main.py, no errors, but when individual modules are loaded, the following TypeError was encountered. A little research turned up that the annotation style used in ipc.py is supported in Python 3.9+; however, not for Python 3.8 (which is being used here).

A simple fix would be to add a Python version check (e.g., sys.version) and for versions that don't support these annotations, add a future import, from __future__ import annotations, which solved the problem on my end.

from electricitylci.combinator import concat_map_upstream_databases
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/python3.8/site-packages/electricitylci/combinator.py", line 4, in <module>
    import electricitylci.generation as gen
  File "~/python3.8/site-packages/electricitylci/generation.py", line 8, in <module>
    from electricitylci.elementaryflows import map_emissions_to_fedelemflows
  File "~/python3.8/site-packages/electricitylci/elementaryflows.py", line 2, in <module>
    import fedelemflowlist
  File "~/python3.8/site-packages/fedelemflowlist/__init__.py", line 10, in <module>
    import fedelemflowlist.jsonld as jsonld
  File "~/python3.8/site-packages/fedelemflowlist/jsonld.py", line 10, in <module>
    import olca
  File "~/python3.8/site-packages/olca/__init__.py", line 4, in <module>
    from .ipc import *
  File "~/python3.8/site-packages/olca/ipc.py", line 146, in <module>
    class Client(object):
  File "~/python3.8/site-packages/olca/ipc.py", line 646, in Client
    def lci_inputs(self, result: schema.SimpleResult) -> list[schema.FlowResult]:
TypeError: 'type' object is not subscriptable

*this is the first of many TypeErrors encountered

More information available on StackOverflow <-- This is not the exact site I found earlier, but close enough.

Miguel-g-c commented 2 years ago

Hi @dt-woods,

We have just included some changes that should solve that. Let us know if this works after updating olca-ipc.py: pip install -U git+https://github.com/GreenDelta/olca-ipc.py.git/@master

selimyoussry commented 2 years ago

Hey @Miguel-g-c , thanks. I believe you missed another one, below the diff I needed to get import olca to work. Thanks for the hard work!

You simply need to replace tuple[...] with Tuple[...] and of course import Tuple from typing at the top.

/work/openlca/olca-ipc.py$ git diff
diff --git a/olca/ipc.py b/olca/ipc.py
index 5d831fc..ae400aa 100644
--- a/olca/ipc.py
+++ b/olca/ipc.py
@@ -7,7 +7,7 @@ import olca.upstream_tree as utree

 from dataclasses import dataclass

-from typing import Any, Iterator, List, Optional, Type, TypeVar, Union
+from typing import Any, Iterator, List, Optional, Tuple, Type, TypeVar, Union

 E = TypeVar('E', bound=schema.RootEntity)
 ModelType = Union[Type[E], str]
@@ -1003,7 +1003,7 @@ class Client(object):
             return None
         return utree.UpstreamTree.from_json(raw)

-    def __post(self, method: str, params) -> tuple[Any, Optional[str]]:
+    def __post(self, method: str, params) -> Tuple[Any, Optional[str]]:
         """
         Performs a request with the given parameters.
Miguel-g-c commented 2 years ago

Hey @selimyoussry, thanks for pointing that out! I've just created a PR which should be merged soon 😁