Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

var-annotated error for Series and Index after upgrading to 2.0.3+ #826

Open
amgcc opened this issue Dec 14, 2023 · 5 comments
Open

var-annotated error for Series and Index after upgrading to 2.0.3+ #826

amgcc opened this issue Dec 14, 2023 · 5 comments

Comments

@amgcc
Copy link
Contributor

amgcc commented Dec 14, 2023

Describe the bug
Upgrading from 2.0.1 to 2.1.1 and now I get errors about Series and Index that aren't helpful. It should not be necessary to duplicate type hints on a local variable.

To Reproduce

  1. Provide a minimal runnable pandas example that is not properly checked by the stubs.
    idx0 = pd.Index()
    idx1: pd.Index = pd.Index()
    sr0 = pd.Series()
    sr1: pd.Series = pd.Series()
  1. Indicate which type checker you are using (mypy or pyright).
    mypy

  2. Show the error message received from that type checker while checking your example.

error: Need type annotation for "idx0"  [var-annotated]
error: Need type annotation for "sr0"  [var-annotated]

Please complete the following information:

  • OS: [e.g. Windows, Linux, MacOS]
  • Linux
  • OS Version [e.g. 22]
  • python version
  • 3.8
  • version of type checker
  • 1.7.1
  • version of installed pandas-stubs
  • 2.1.1 and 2.0.3 failing, 2.0.1 no error
@twoertwein
Copy link
Member

I can re-produce the var-annotated error for pd.Index but not for pd.Series.

Revealed type is "pandas.core.indexes.base.Index[]"
Revealed type is "pandas.core.series.Series[Any]"

Probably need to slightly adjust the Index overloads to return Index[Any] (or Index[S1]) in the last case?

Quick note: pandas 2.1.* requires python 3.9 or newer! This also means you are not getting the latest pandas-stubs releases (they also require 3.9)!

@amgcc
Copy link
Contributor Author

amgcc commented Dec 15, 2023

I can re-produce the var-annotated error for pd.Index but not for pd.Series.

Revealed type is "pandas.core.indexes.base.Index[]"
Revealed type is "pandas.core.series.Series[Any]"

Probably need to slightly adjust the Index overloads to return Index[Any] (or Index[S1]) in the last case?

Quick note: pandas 2.1.* requires python 3.9 or newer! This also means you are not getting the latest pandas-stubs releases (they also require 3.9)!

I am able to resolve 2.1.1 with Python 3.8 and while I plan to upgrade soon, it wouldn't help unless this gets fixed in the stubs library first. This issue affects pd.Series and not just pd.Index. It shouldn't matter but my version of pandas I am testing with is 2.0.3.

Additional migration issues for the pd.Series constructor involve passing a dictionary. It fails with a Timestamp or multi-index key whereas before it did not:

sr1 = pd.Series(
        {
            pd.Timestamp(2023, 1, 2): "b",
        }
    )

    sr2 = pd.Series(
        {
            ("a", "b"): "c",
        }
    )
test.py:172: error: Need type annotation for "sr1"  [var-annotated]
test.py:174: error: Dict entry 0 has incompatible type "Timestamp": "str"; expected "str": "ndarray[Any, Any]"  [dict-item]
test.py:178: error: Need type annotation for "sr2"  [var-annotated]
test.py:180: error: Dict entry 0 has incompatible type "Tuple[str, str]": "str"; expected "str": "ndarray[Any, Any]"  [dict-item]

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Dec 18, 2023

Additional migration issues for the pd.Series constructor involve passing a dictionary. It fails with a Timestamp or multi-index key whereas before it did not:

I created a separate issue for this. #831

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Dec 18, 2023

I am able to resolve 2.1.1 with Python 3.8 and while I plan to upgrade soon, it wouldn't help unless this gets fixed in the stubs library first. This issue affects pd.Series and not just pd.Index.

I just tried this with the latest release of the stubs (2.1.4.231218) and mypy 1.7.1, with python 3.9, and the Series lines pass fine, but there is a problem with Index that we need to fix up.

We won't update the stubs to support older versions of python, mypy, pyright, or pandas.

@amgcc
Copy link
Contributor Author

amgcc commented Dec 19, 2023

We won't update the stubs to support older versions of python, mypy, pyright, or pandas.

Thats understandable. I appreciate you taking the time to fix this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants