question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

__iter__ method of a Series should be type annotated

See original GitHub issue

Describe the bug

A type checker can not correctly resolve the return type of the __iter__ method of a pandas series.

To Reproduce

  1. Provide a minimal runnable pandas example that is not properly checked by the stubs.
from typing_extensions import reveal_type
import pandas as pd

series = pd.Series([1, 2, 3], dtype=int)
reveal_type(series) # type = Series[int]
iterable = iter(series)
reveal_type(iterable) # type = Unkown, type should be Iterable[int]
  1. Indicate which type checker you are using (mypy or pyright).

I am using pyright 1.1.257

  1. Pyright message
  /home/pandas_types.py:5:13 - information: Type of "series" is "Series[int]"
  /home/pandas_types.py:7:13 - information: Type of "iterable" is "Unknown"

Please complete the following information:

  • OS: Linux
  • OS Version: Ubuntu 22.04
  • python version: 3.10.4
  • version of type checker 1.1.257
  • version of installed pandas-stubs 1.4.3.220704

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Dr-Irvcommented, Jul 10, 2022

Also, is there any reason for a Series not being a Sequence?

Series does not implement __reversed__

0reactions
bashtagecommented, Jul 12, 2022

@Dr-Irv looks like Series cannot be a Sequence since a Sequence needs to implement:

  • __getitem__
  • __len__
  • __contains__
  • __iter__
  • __reversed__
  • index << Not possible since index returns the index and cannot be index(loc: int).
  • count

This is somewhat unfortunate but forever unchangeable I’m sure.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to tell which type annotations support __iter__ - ...
The simplest way is to resolve the FQN of the given annotation, and then retrieve the stub for the module it belongs to...
Read more >
Understanding type annotation in Python
The Iterable type should be used when the function takes an iterable and iterates over it. An iterable is an object that can...
Read more >
Python | Pandas Series.iteritems()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type.
Read more >
typing — Support for type hints — Python 3.11.1 documentation
At runtime, the statement Derived = NewType('Derived', Base) will make Derived a callable that immediately returns whatever parameter you pass it. That means...
Read more >
Type hints cheat sheet - mypy 0.991 documentation
This is how you declare the type of a variable age: int = 1 # You don't need to initialize a variable to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found