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

chore(python): let read_csv take Sequence as columns, remove several type: ignore #7028

Merged
merged 5 commits into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions py-polars/polars/_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import os
from textwrap import dedent
from types import TracebackType
from typing import Iterable
from typing import TYPE_CHECKING, Iterable

if TYPE_CHECKING:
from polars.internals import DataFrame


class Tag:
Expand Down Expand Up @@ -43,7 +46,7 @@ def __exit__(
class HTMLFormatter:
def __init__(
self,
df: DataFrame, # type: ignore[name-defined] # noqa: F821
df: DataFrame,
max_cols: int = 75,
max_rows: int = 40,
from_series: bool = False,
Expand Down Expand Up @@ -75,7 +78,7 @@ def __init__(

def write_header(self) -> None:
"""Write the header of an HTML table."""
shape = self.df.shape
shape: tuple[int, ...] = self.df.shape
stinodego marked this conversation as resolved.
Show resolved Hide resolved
if self.series:
shape = shape[:1]

Expand Down
7 changes: 3 additions & 4 deletions py-polars/polars/internals/series/datetime.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from datetime import date, datetime, time, timedelta
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, cast

import polars.internals as pli
from polars.internals.series.utils import expr_dispatch
Expand Down Expand Up @@ -52,8 +52,7 @@ def min(self) -> date | datetime | timedelta:
datetime.datetime(2001, 1, 1, 0, 0)

"""
# we can ignore types because we are certain we get a logical type
return pli.wrap_s(self._s).min() # type: ignore[return-value]
return cast("date | datetime | timedelta", pli.wrap_s(self._s).min())
stinodego marked this conversation as resolved.
Show resolved Hide resolved

def max(self) -> date | datetime | timedelta:
"""
Expand All @@ -75,7 +74,7 @@ def max(self) -> date | datetime | timedelta:
datetime.datetime(2001, 1, 3, 0, 0)

"""
return pli.wrap_s(self._s).max() # type: ignore[return-value]
return cast("date | datetime | timedelta", pli.wrap_s(self._s).max())

def median(self) -> date | datetime | timedelta | None:
"""
Expand Down
11 changes: 6 additions & 5 deletions py-polars/polars/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
BinaryIO,
Callable,
Mapping,
Sequence,
TextIO,
cast,
overload,
Expand Down Expand Up @@ -58,7 +59,7 @@ def _check_arg_is_1byte(
def read_csv(
file: str | TextIO | BytesIO | Path | BinaryIO | bytes,
has_header: bool = True,
columns: list[int] | list[str] | None = None,
columns: Sequence[int] | Sequence[str] | None = None,
new_columns: list[str] | None = None,
sep: str = ",",
comment_char: str | None = None,
Expand Down Expand Up @@ -230,7 +231,7 @@ def read_csv(
and not low_memory
and null_values is None
):
include_columns = None
include_columns: Sequence[str] | None = None

if columns:
if not has_header:
Expand Down Expand Up @@ -1235,7 +1236,7 @@ def _read_excel_sheet(
parser: Any,
sheet_id: int | None,
sheet_name: str | None,
read_csv_options: dict[str, Any] | None,
read_csv_options: dict[str, Any],
) -> DataFrame:
csv_buffer = StringIO()

Expand All @@ -1246,7 +1247,7 @@ def _read_excel_sheet(
csv_buffer.seek(0)

# Parse CSV output.
return read_csv(csv_buffer, **read_csv_options) # type: ignore[arg-type]
return read_csv(csv_buffer, **read_csv_options)


def _get_delta_lake_table(
Expand Down Expand Up @@ -1660,7 +1661,7 @@ def scan_ds(ds: pa.dataset.dataset, allow_pyarrow_filter: bool = True) -> LazyFr
def read_csv_batched(
file: str | Path,
has_header: bool = True,
columns: list[int] | list[str] | None = None,
columns: Sequence[int] | Sequence[str] | None = None,
new_columns: list[str] | None = None,
sep: str = ",",
comment_char: str | None = None,
Expand Down
24 changes: 14 additions & 10 deletions py-polars/polars/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import sys
import warnings
from collections.abc import MappingView, Reversible, Sized
from datetime import date, datetime, time, timedelta, timezone, tzinfo
from datetime import date, datetime, time, timedelta, timezone
from datetime import tzinfo as tzinfo_t
stinodego marked this conversation as resolved.
Show resolved Hide resolved
from pathlib import Path
from typing import (
TYPE_CHECKING,
Expand All @@ -18,6 +19,7 @@
Iterable,
Sequence,
TypeVar,
cast,
overload,
)

Expand Down Expand Up @@ -201,20 +203,22 @@ def range_to_slice(rng: range) -> slice:

def handle_projection_columns(
columns: Sequence[str] | Sequence[int] | str | None,
) -> tuple[list[int] | None, list[str] | None]:
) -> tuple[list[int] | None, Sequence[str] | None]:
"""Disambiguates between columns specified as integers vs. strings."""
projection: list[int] | None = None
if columns:
new_columns: Sequence[str] | None = None
if columns is not None:
if isinstance(columns, str):
columns = [columns]
new_columns = [columns]
elif is_int_sequence(columns):
projection = list(columns)
columns = None
elif not is_str_sequence(columns):
raise ValueError(
"'columns' arg should contain a list of all integers or all strings"
" values."
)
else:
new_columns = columns
if columns and len(set(columns)) != len(columns):
raise ValueError(
f"'columns' arg should only have unique values. Got '{columns}'."
Expand All @@ -223,7 +227,7 @@ def handle_projection_columns(
raise ValueError(
f"'columns' arg should only have unique values. Got '{projection}'."
)
return projection, columns # type: ignore[return-value]
return projection, new_columns


def _to_python_time(value: int) -> time:
Expand Down Expand Up @@ -317,7 +321,7 @@ def _to_python_datetime(
# cache here as we have a single tz per column
# and this function will be called on every conversion
@functools.lru_cache(16)
def _parse_fixed_tz_offset(offset: str) -> tzinfo:
def _parse_fixed_tz_offset(offset: str) -> tzinfo_t:
try:
# use fromisoformat to parse the offset
dt_offset = datetime.fromisoformat("2000-01-01T00:00:00" + offset)
Expand All @@ -328,16 +332,16 @@ def _parse_fixed_tz_offset(offset: str) -> tzinfo:
except ValueError:
raise ValueError(f"Offset: {offset} not understood.") from None

return dt_offset.tzinfo # type: ignore[return-value]
return cast(tzinfo_t, dt_offset.tzinfo)
MarcoGorelli marked this conversation as resolved.
Show resolved Hide resolved


def _localize(dt: datetime, tz: str) -> datetime:
# zone info installation should already be checked
try:
tzinfo = ZoneInfo(tz)
tzinfo: ZoneInfo | tzinfo_t = ZoneInfo(tz)
stinodego marked this conversation as resolved.
Show resolved Hide resolved
except zoneinfo.ZoneInfoNotFoundError:
# try fixed offset, which is not supported by ZoneInfo
tzinfo = _parse_fixed_tz_offset(tz) # type: ignore[assignment]
tzinfo = _parse_fixed_tz_offset(tz)

return dt.astimezone(tzinfo)

Expand Down