Skip to content

Commit e748370

Browse files
committed
REF: replace col_space setter with initialization
1 parent 52d6a16 commit e748370

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

pandas/io/formats/format.py

+11-18
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,7 @@ def __init__(
581581
self.formatters = self._initialize_formatters(formatters)
582582
self.na_rep = na_rep
583583
self.decimal = decimal
584-
585-
# Ignoring error
586-
# expression has type "Union[str, int, Sequence[Union[str, int]],
587-
# Mapping[Optional[Hashable], Union[str, int]], None]",
588-
# variable has type "Mapping[Optional[Hashable], Union[str, int]]"
589-
self.col_space = col_space # type: ignore[assignment]
584+
self.col_space = self._initialize_colspace(col_space)
590585

591586
self.header = header
592587
self.index = index
@@ -650,33 +645,31 @@ def columns(self, columns: Optional[Sequence[str]]) -> None:
650645
self._columns = self.frame.columns
651646
assert self._columns is not None
652647

653-
@property
654-
def col_space(self) -> ColspaceType:
655-
return self._col_space
648+
def _initialize_colspace(
649+
self, col_space: Optional[ColspaceArgType]
650+
) -> ColspaceType:
651+
result: ColspaceType
656652

657-
@col_space.setter
658-
def col_space(self, col_space: Optional[ColspaceArgType]) -> None:
659-
self._col_space: ColspaceType
660653
if col_space is None:
661-
self._col_space = {}
654+
result = {}
662655
elif isinstance(col_space, (int, str)):
663-
self._col_space = {"": col_space}
664-
self._col_space.update({column: col_space for column in self.frame.columns})
656+
result = {"": col_space}
657+
result.update({column: col_space for column in self.frame.columns})
665658
elif isinstance(col_space, Mapping):
666659
for column in col_space.keys():
667660
if column not in self.frame.columns and column != "":
668661
raise ValueError(
669662
f"Col_space is defined for an unknown column: {column}"
670663
)
671-
self._col_space = col_space
664+
result = col_space
672665
else:
673666
if len(self.frame.columns) != len(col_space):
674667
raise ValueError(
675668
f"Col_space length({len(col_space)}) should match "
676669
f"DataFrame number of columns({len(self.frame.columns)})"
677670
)
678-
self._col_space = dict(zip(self.frame.columns, col_space))
679-
assert self._col_space is not None
671+
result = dict(zip(self.frame.columns, col_space))
672+
return result
680673

681674
@property
682675
def max_rows_displayed(self) -> int:

0 commit comments

Comments
 (0)