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

BUG: columns on existing DataFrame does not accept "List[str]" #73

Open
bashtage opened this issue Jul 2, 2022 · 1 comment
Open

BUG: columns on existing DataFrame does not accept "List[str]" #73

bashtage opened this issue Jul 2, 2022 · 1 comment
Labels
mypy bug Requires mypy to fix a bug

Comments

@bashtage
Copy link
Contributor

bashtage commented Jul 2, 2022

import pandas as pd
df = pd.DataFrame({"a":[1,2,3],"b":[0.0,1,1]})

df.columns= ["c", "d"]  # mypy error, columns is `Index` so cannot take List[str}
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jul 4, 2022

This is a bug in mypy: python/mypy#3004

Here's a simple example:

from typing import Union


class Foobar:
    def __init__(self, x: Union[int, str]):
        self._x = self.makestr(x)

    def makestr(self, x: Union[int, str]) -> str:
        if isinstance(x, int):
            return str(x)
        elif isinstance(x, str):
            return x
        else:
            raise Exception("invalid type")

    @property
    def x(self) -> str:
        return self._x

    @x.setter
    def x(self, x: Union[int, str]) -> None:
        self._x = self.makestr(x)


fb = Foobar("abc")
fb.x = 3

mypy will then report:

mypyasym.py:26: error: Incompatible types in assignment (expression has type "int", variable has type "str")

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

No branches or pull requests

2 participants