seandstewart / typical

Typical: Fast, simple, & correct data-validation using Python 3 typing.
https://python-typical.org
MIT License
183 stars 9 forks source link

Couldn't evaluate type ForwardRef pandas DataFrame #172

Closed floriandorre closed 3 years ago

floriandorre commented 3 years ago

Description

Trying to use a package (swmmtoolbox) that has a tstoolbox dependy that has itself a typical depency, thep ackage is applying as typic.al decorator to a method returning a pandas.DataFrame.

After trying to use typic.al on a dummy method as below i get the following result :

What I Did

import pandas as pd
import typic

@typic.al
def dummy() -> pd.DataFrame:
    return pd.DataFrame()

if __name__ == '__main__':
    dummy()

Output :

lib\site-packages\typic\util.py:479: UserWarning: Couldn't evaluate type ForwardRef('typing.Union[weakref.ReferenceType[NDFrame],None]'): 'type' object is not subscriptable
  warnings.warn(f"Couldn't evaluate type {value!r}: {e}")

Pip freeze output :

future-typing==0.4.1
inflection==0.5.1
numpy==1.21.2
pandas==1.3.3
pendulum==2.1.2
python-dateutil==2.8.2
pytz==2021.1
pytzdata==2020.1
six==1.16.0
typical==2.6.4
typing-extensions==3.10.0.2
seandstewart commented 3 years ago

Hey @floriandorre -

This warning is to let the user know that the type annotation couldn't be inspected at runtime. It's not an error with typical, but rather the notation itself doesn't support runtime inspection.

In this case, typical will ignore the annotation, so it's considered safe.

There's nothing typical can do to resolve this issue within its own logic, the owner of the annotation would need to ensure that the annotation is valid at runtime, not just as a static string.

floriandorre commented 3 years ago

Thanks for the details, I'm closing this issue then