Skip to content

TYP: fix mypy ignored error in pandas/io/formats/latex.py #37738

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 2 commits into from
Nov 10, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions pandas/io/formats/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Module for formatting output data in Latex.
"""
from abc import ABC, abstractmethod
from typing import Iterator, List, Optional, Tuple, Type, Union
from typing import Iterator, List, Optional, Sequence, Tuple, Type, Union

import numpy as np

Expand Down Expand Up @@ -73,10 +73,8 @@ def __init__(
self.multicolumn_format = multicolumn_format
self.multirow = multirow
self.clinebuf: List[List[int]] = []
self.strcols = self._get_strcols()
self.strrows: List[List[str]] = list(
zip(*self.strcols) # type: ignore[arg-type]
)
self.strcols: Sequence[Sequence[str]] = self._get_strcols()
self.strrows: Sequence[Sequence[str]] = list(zip(*self.strcols))
Copy link
Member

Choose a reason for hiding this comment

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

The variable type annotations can be removed. My preference is not to add variable types annotations unless absolutely necessary.

the variable types for self.strcols and self.strrows can (should) be inferred from the rhs expression.

self.strcols: Revealed type is 'builtins.list[builtins.list[builtins.str]]'
self.strrows: Revealed type is 'builtins.list[builtins.tuple*[Any]]'

I'm not sure why mypy can't infer more precise for self.strrows

mypy is green, but if wanted to be more precise for self.strrows could use List[Tuple[str, ...]]

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for the suggestions!
Updated accordingly.


def get_strrow(self, row_num: int) -> str:
"""Get string representation of the row."""
Expand Down Expand Up @@ -179,7 +177,7 @@ def _empty_info_line(self):
f"Index: {self.frame.index}"
)

def _preprocess_row(self, row: List[str]) -> List[str]:
def _preprocess_row(self, row: Sequence[str]) -> List[str]:
"""Preprocess elements of the row."""
if self.fmt.escape:
crow = _escape_symbols(row)
Expand Down Expand Up @@ -781,7 +779,7 @@ def _get_index_format(self) -> str:
return "l" * self.frame.index.nlevels if self.fmt.index else ""


def _escape_symbols(row: List[str]) -> List[str]:
def _escape_symbols(row: Sequence[str]) -> List[str]:
"""Carry out string replacements for special symbols.

Parameters
Expand Down Expand Up @@ -813,7 +811,7 @@ def _escape_symbols(row: List[str]) -> List[str]:
]


def _convert_to_bold(crow: List[str], ilevels: int) -> List[str]:
def _convert_to_bold(crow: Sequence[str], ilevels: int) -> List[str]:
"""Convert elements in ``crow`` to bold."""
return [
f"\\textbf{{{x}}}" if j < ilevels and x.strip() not in ["", "{}"] else x
Expand Down