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

docs(python): Annotate Config kwarg options #18988

Merged
merged 6 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
64 changes: 60 additions & 4 deletions py-polars/polars/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import contextlib
import os
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal, get_args
from typing import TYPE_CHECKING, Literal, TypedDict, get_args

from polars._utils.various import normalize_filepath
from polars.dependencies import json
Expand All @@ -20,9 +20,9 @@
from typing_extensions import TypeAlias

if sys.version_info >= (3, 11):
from typing import Self
from typing import Self, Unpack
else:
from typing_extensions import Self
from typing_extensions import Self, Unpack


__all__ = ["Config"]
Expand Down Expand Up @@ -87,6 +87,60 @@
}


class ConfigParameters(TypedDict, total=False):
"""Parameters supported by the polars Config."""

ascii_tables: bool | None
auto_structify: bool | None
decimal_separator: str | None
thousands_separator: str | bool | None
float_precision: int | None
fmt_float: FloatFmt | None
fmt_str_lengths: int | None
fmt_table_cell_list_len: int | None
streaming_chunk_size: int | None
tbl_cell_alignment: Literal["LEFT", "CENTER", "RIGHT"] | None
tbl_cell_numeric_alignment: Literal["LEFT", "CENTER", "RIGHT"] | None
tbl_cols: int | None
tbl_column_data_type_inline: bool | None
tbl_dataframe_shape_below: bool | None
tbl_formatting: TableFormatNames | None
tbl_hide_column_data_types: bool | None
tbl_hide_column_names: bool | None
tbl_hide_dtype_separator: bool | None
tbl_hide_dataframe_shape: bool | None
tbl_rows: int | None
tbl_width_chars: int | None
trim_decimal_zeros: bool | None
verbose: bool | None
expr_depth_warning: int

set_ascii_tables: bool | None
set_auto_structify: bool | None
set_decimal_separator: str | None
set_thousands_separator: str | bool | None
set_float_precision: int | None
set_fmt_float: FloatFmt | None
set_fmt_str_lengths: int | None
set_fmt_table_cell_list_len: int | None
set_streaming_chunk_size: int | None
set_tbl_cell_alignment: Literal["LEFT", "CENTER", "RIGHT"] | None
set_tbl_cell_numeric_alignment: Literal["LEFT", "CENTER", "RIGHT"] | None
set_tbl_cols: int | None
set_tbl_column_data_type_inline: bool | None
set_tbl_dataframe_shape_below: bool | None
set_tbl_formatting: TableFormatNames | None
set_tbl_hide_column_data_types: bool | None
set_tbl_hide_column_names: bool | None
set_tbl_hide_dtype_separator: bool | None
set_tbl_hide_dataframe_shape: bool | None
set_tbl_rows: int | None
set_tbl_width_chars: int | None
set_trim_decimal_zeros: bool | None
set_verbose: bool | None
set_expr_depth_warning: int


class Config(contextlib.ContextDecorator):
"""
Configure polars; offers options for table formatting and more.
Expand Down Expand Up @@ -118,7 +172,9 @@ class Config(contextlib.ContextDecorator):

_original_state: str = ""

def __init__(self, *, restore_defaults: bool = False, **options: Any) -> None:
def __init__(
self, *, restore_defaults: bool = False, **options: Unpack[ConfigParameters]
) -> None:
"""
Initialise a Config object instance for context manager usage.

Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ def test_config_scope() -> None:


def test_config_raise_error_if_not_exist() -> None:
with pytest.raises(AttributeError), pl.Config(i_do_not_exist=True):
with pytest.raises(AttributeError), pl.Config(i_do_not_exist=True): # type: ignore[call-arg]
pass


Expand Down