Skip to content

TYP: remove inappropriate use of cast #34990

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

Merged
merged 1 commit into from
Jun 25, 2020

Conversation

simonjayhawkins
Copy link
Member

the cast was silencing error: Argument 2 to "zip" has incompatible type "Union[Sequence[Union[str, int]], Mapping[Optional[Hashable], Union[str, int]]]"; expected "Iterable[Union[str, int]]"

This error is because col_space is defined as Union[str, int, Sequence[Union[str, int]], Mapping[Label, Union[str, int]]] and the elif isinstance(col_space, dict): would not catch non-dict mappings, which would go though the else.

so the else is non-dict mappings and sequences and the cast to sequence is not safe.

>>> df = pd.DataFrame(np.random.random(size=(3, 3)), columns=["a", "b", "c"])
>>>
>>> print(df.to_string(col_space={"b": 20}))
          a                    b         c
0  0.382920             0.057121  0.862742
1  0.579339             0.391014  0.907678
2  0.340584             0.889387  0.922690
>>>
>>> from collections import UserDict
>>>
>>> print(df.to_string(col_space=UserDict(b=20)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\simon\pandas\pandas\core\frame.py", line 838, in to_string
    formatter = fmt.DataFrameFormatter(
  File "C:\Users\simon\pandas\pandas\io\formats\format.py", line 601, in __init__
    raise ValueError(
ValueError: Col_space length(1) should match DataFrame number of columns(3)
>>>

@simonjayhawkins simonjayhawkins added the Typing type annotations, mypy/pyright type checking label Jun 25, 2020
Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@WillAyd WillAyd added this to the 1.1 milestone Jun 25, 2020
@WillAyd WillAyd merged commit 2b25640 into pandas-dev:master Jun 25, 2020
@WillAyd
Copy link
Member

WillAyd commented Jun 25, 2020

Thanks @simonjayhawkins

@simonjayhawkins simonjayhawkins deleted the remove-cast branch June 25, 2020 18:40
fangchenli pushed a commit to fangchenli/pandas that referenced this pull request Jun 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants