-1

Mypy is correctly telling me that the following is missing the generic arg for pd.Series

def foo(x : pd.Series) -> None:
     pass

When I add the arg like so

from typing import Any

def foo(x : pd.Series[Any]) -> None:
     pass

and I try to run my code using Python 3.10.10 and using python myfile.py to run it I get the following

def foo(x: pd.Series[Any]) -> None:
TypeError: 'type' object is not subscriptable

I'm also using pandas-stubs==1.5.3.230203 for pandas type stubs.

Related question here indicates this type hinting should be possible. I've also found code examples here

MilesConn
  • 301
  • 2
  • 13
  • What python version are you using, typing API has been evolving very quickly ? Also please [edit] to add the full traceback leading to the `TypeError`. – ljmc Feb 15 '23 at 08:09
  • 2
    `TypeError: 'type' object is not subscriptable` indicates that at runtime, `pandas.Series` is not a generic class, but it's modelled as a generic class in the stubs. You can fix this by adding `from __future__ import annotations` at the top of your module. – dROOOze Feb 15 '23 at 10:26
  • The linked dupe mentions `annotations` future import in one of answers about `pd.Series`, which is exactly your case. Also you can use string annotations directly (`def foo(x: 'pd.Series[Any]') -> None: ...`) – STerliakov Feb 15 '23 at 13:08
  • `from __future__ import annotations` solved it. I initially didn't try it because it was for Python 3.8 and Python 3.9 and I'm on Python 3.10. Should've tried sooner thank you! – MilesConn Feb 15 '23 at 17:53

1 Answers1

3

from __future__ import annotations seemed to solve it.

MilesConn
  • 301
  • 2
  • 13