From 1bd13d847ce39fc59208988999c7521e046ed734 Mon Sep 17 00:00:00 2001 From: Avasam Date: Wed, 11 Jan 2023 21:07:54 -0500 Subject: [PATCH 1/4] Fix Any subclassing in tqdm --- stubs/tqdm/@tests/stubtest_allowlist.txt | 5 +- stubs/tqdm/tqdm/_rich_shims.pyi | 141 +++++++++++++++++++++++ stubs/tqdm/tqdm/dask.pyi | 22 +++- stubs/tqdm/tqdm/keras.pyi | 27 ++++- stubs/tqdm/tqdm/rich.pyi | 10 +- 5 files changed, 189 insertions(+), 16 deletions(-) create mode 100644 stubs/tqdm/tqdm/_rich_shims.pyi diff --git a/stubs/tqdm/@tests/stubtest_allowlist.txt b/stubs/tqdm/@tests/stubtest_allowlist.txt index 5405b8923536..85a7d4bf7d27 100644 --- a/stubs/tqdm/@tests/stubtest_allowlist.txt +++ b/stubs/tqdm/@tests/stubtest_allowlist.txt @@ -1,5 +1,4 @@ tqdm.contrib.discord -# Metaclass differs: -tqdm.rich.FractionColumn -tqdm.rich.RateColumn +# Shims +tqdm._rich_shims diff --git a/stubs/tqdm/tqdm/_rich_shims.pyi b/stubs/tqdm/tqdm/_rich_shims.pyi new file mode 100644 index 000000000000..7419dfa09e88 --- /dev/null +++ b/stubs/tqdm/tqdm/_rich_shims.pyi @@ -0,0 +1,141 @@ +from _typeshed import Incomplete +from abc import ABC, abstractmethod +from collections.abc import Callable, Iterable +from dataclasses import dataclass +from typing import NamedTuple, Protocol, runtime_checkable +from typing_extensions import Literal, TypeAlias + +# rich.text +Text: TypeAlias = str | Incomplete + +# rich.align +VerticalAlignMethod: TypeAlias = Literal["top", "middle", "bottom"] + +# rich.style +StyleType: TypeAlias = str | Incomplete + +# rich.table + +@dataclass +class Column(Protocol): + header: RenderableType + footer: RenderableType + header_style: StyleType + footer_style: StyleType + style: StyleType + justify: JustifyMethod + vertical: VerticalAlignMethod + overflow: OverflowMethod + width: int | None + min_width: int | None + max_width: int | None + ratio: int | None + no_wrap: bool = False + def copy(self) -> Column: ... + @property + def cells(self) -> Iterable[RenderableType]: ... + @property + def flexible(self) -> bool: ... + +# rich.segment +Segment: TypeAlias = Incomplete + +# region rich.console +HighlighterType: TypeAlias = Callable[[str | Text], Text] +JustifyMethod: TypeAlias = Literal["default", "left", "center", "right", "full"] +OverflowMethod: TypeAlias = Literal["fold", "crop", "ellipsis", "ignore"] +RenderResult: TypeAlias = Iterable[RenderableType | Segment] +Console: TypeAlias = Incomplete + +class NoChange: ... + +class ConsoleDimensions(NamedTuple): + width: int + height: int + +@dataclass +class ConsoleOptions: + size: ConsoleDimensions + legacy_windows: bool + min_width: int + max_width: int + is_terminal: bool + encoding: str + max_height: int + justify: JustifyMethod | None + overflow: OverflowMethod | None + no_wrap: bool | None + highlight: bool | None + markup: bool | None + height: int | None + @property + def ascii_only(self) -> bool: ... + def copy(self) -> ConsoleOptions: ... + def update( + self, + *, + width: int | NoChange, + min_width: int | NoChange, + max_width: int | NoChange, + justify: JustifyMethod | None | NoChange, + overflow: OverflowMethod | None | NoChange, + no_wrap: bool | None | NoChange, + highlight: bool | None | NoChange, + markup: bool | None | NoChange, + height: int | None | NoChange, + ) -> ConsoleOptions: ... + def update_width(self, width: int) -> ConsoleOptions: ... + def update_height(self, height: int) -> ConsoleOptions: ... + def reset_height(self) -> ConsoleOptions: ... + def update_dimensions(self, width: int, height: int) -> ConsoleOptions: ... + +@runtime_checkable +class RichCast(Protocol): + def __rich__(self) -> ConsoleRenderable | RichCast | str: ... + +@runtime_checkable +class ConsoleRenderable(Protocol): + def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult: ... + +RenderableType: TypeAlias = ConsoleRenderable | RichCast | str + +# endregion + +# region rich.progress.Task +@dataclass +class Task(Protocol): + id: int + description: str + total: float | None + completed: float + finished_time: float | None + visible: bool + fields: dict[str, Incomplete] + start_time: float | None + stop_time: float | None + finished_speed: float | None + def get_time(self) -> float: ... + @property + def started(self) -> bool: ... + @property + def remaining(self) -> float | None: ... + @property + def elapsed(self) -> float | None: ... + @property + def finished(self) -> bool: ... + @property + def percentage(self) -> float: ... + @property + def speed(self) -> float | None: ... + @property + def time_remaining(self) -> float | None: ... + +class ProgressColumn(ABC): + max_refresh: float | None + def __init__(self, table_column: Column | None = ...) -> None: ... + def get_table_column(self) -> Column: ... + def __call__(self, task: Task) -> RenderableType: ... + @abstractmethod + def render(self, task: Task) -> RenderableType: ... + +# endregion diff --git a/stubs/tqdm/tqdm/dask.pyi b/stubs/tqdm/tqdm/dask.pyi index 2509750615dd..dbd7e6b246b7 100644 --- a/stubs/tqdm/tqdm/dask.pyi +++ b/stubs/tqdm/tqdm/dask.pyi @@ -1,10 +1,24 @@ -from _typeshed import Incomplete -from typing import Any -from typing_extensions import TypeAlias +from _typeshed import Incomplete, Self +from collections.abc import Callable +from typing import ClassVar __all__ = ["TqdmCallback"] -_Callback: TypeAlias = Any # Actually dask.callbacks.Callback +# dask.callbacks.Callback +class _Callback: + active: ClassVar[set[tuple[Callable[..., Incomplete] | None, ...]]] + def __init__( + self, + start: Incomplete | None, + start_state: Incomplete | None, + pretask: Incomplete | None, + posttask: Incomplete | None, + finish: Incomplete | None, + ) -> None: ... + def __enter__(self: Self) -> Self: ... + def __exit__(self, *args) -> None: ... + def register(self) -> None: ... + def unregister(self) -> None: ... class TqdmCallback(_Callback): tqdm_class: type[Incomplete] diff --git a/stubs/tqdm/tqdm/keras.pyi b/stubs/tqdm/tqdm/keras.pyi index 0a5f5c7bdcf9..75e86e92a4a4 100644 --- a/stubs/tqdm/tqdm/keras.pyi +++ b/stubs/tqdm/tqdm/keras.pyi @@ -1,10 +1,31 @@ from _typeshed import Incomplete -from typing import Any -from typing_extensions import TypeAlias __all__ = ["TqdmCallback"] -_Callback: TypeAlias = Any # Actually tensorflow.keras.callbacks.Callback +# keras.callbacks.Callback +class _Callback: + validation_data: Incomplete | None + model: Incomplete | None + params: Incomplete + def __init__(self) -> None: ... + def set_params(self, params) -> None: ... + def set_model(self, model) -> None: ... + def on_batch_begin(self, batch, logs: Incomplete | None = ...) -> None: ... + def on_batch_end(self, batch, logs: Incomplete | None = ...) -> None: ... + def on_epoch_begin(self, epoch, logs: Incomplete | None = ...) -> None: ... + def on_epoch_end(self, epoch, logs: Incomplete | None = ...) -> None: ... + def on_train_batch_begin(self, batch, logs: Incomplete | None = ...) -> None: ... + def on_train_batch_end(self, batch, logs: Incomplete | None = ...) -> None: ... + def on_test_batch_begin(self, batch, logs: Incomplete | None = ...) -> None: ... + def on_test_batch_end(self, batch, logs: Incomplete | None = ...) -> None: ... + def on_predict_batch_begin(self, batch, logs: Incomplete | None = ...) -> None: ... + def on_predict_batch_end(self, batch, logs: Incomplete | None = ...) -> None: ... + def on_train_begin(self, logs: Incomplete | None = ...) -> None: ... + def on_train_end(self, logs: Incomplete | None = ...) -> None: ... + def on_test_begin(self, logs: Incomplete | None = ...) -> None: ... + def on_test_end(self, logs: Incomplete | None = ...) -> None: ... + def on_predict_begin(self, logs: Incomplete | None = ...) -> None: ... + def on_predict_end(self, logs: Incomplete | None = ...) -> None: ... class TqdmCallback(_Callback): @staticmethod diff --git a/stubs/tqdm/tqdm/rich.pyi b/stubs/tqdm/tqdm/rich.pyi index c06c63eeb0be..41c51d155c34 100644 --- a/stubs/tqdm/tqdm/rich.pyi +++ b/stubs/tqdm/tqdm/rich.pyi @@ -1,22 +1,20 @@ from _typeshed import Incomplete, SupportsWrite from collections.abc import Iterable, Mapping -from typing import Any, Generic, NoReturn, TypeVar, overload -from typing_extensions import TypeAlias +from typing import Generic, NoReturn, TypeVar, overload +from ._rich_shims import ProgressColumn from .std import tqdm as std_tqdm __all__ = ["tqdm_rich", "trrange", "tqdm", "trange"] -_ProgressColumn: TypeAlias = Any # Actually rich.progress.ProgressColumn - -class FractionColumn(_ProgressColumn): +class FractionColumn(ProgressColumn): unit_scale: bool unit_divisor: int def __init__(self, unit_scale: bool = ..., unit_divisor: int = ...) -> None: ... def render(self, task): ... -class RateColumn(_ProgressColumn): +class RateColumn(ProgressColumn): unit: str unit_scale: bool unit_divisor: int From eb752f4fa32cecfb790840b8de50eea0239813a0 Mon Sep 17 00:00:00 2001 From: Avasam Date: Wed, 11 Jan 2023 21:18:16 -0500 Subject: [PATCH 2/4] Fix for pytype --- stubs/tqdm/tqdm/_rich_shims.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/tqdm/tqdm/_rich_shims.pyi b/stubs/tqdm/tqdm/_rich_shims.pyi index 7419dfa09e88..7c73edf7af4a 100644 --- a/stubs/tqdm/tqdm/_rich_shims.pyi +++ b/stubs/tqdm/tqdm/_rich_shims.pyi @@ -30,7 +30,7 @@ class Column(Protocol): min_width: int | None max_width: int | None ratio: int | None - no_wrap: bool = False + no_wrap: bool def copy(self) -> Column: ... @property def cells(self) -> Iterable[RenderableType]: ... From 05f214b947969b1c8c245cf55e0c5ec942b81d59 Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 12 Jan 2023 21:50:41 -0500 Subject: [PATCH 3/4] Remove rich shims --- stubs/tqdm/@tests/stubtest_allowlist.txt | 3 - stubs/tqdm/tqdm/_rich_shims.pyi | 141 ----------------------- stubs/tqdm/tqdm/rich.pyi | 10 +- 3 files changed, 6 insertions(+), 148 deletions(-) delete mode 100644 stubs/tqdm/tqdm/_rich_shims.pyi diff --git a/stubs/tqdm/@tests/stubtest_allowlist.txt b/stubs/tqdm/@tests/stubtest_allowlist.txt index 85a7d4bf7d27..aff04d2aecaf 100644 --- a/stubs/tqdm/@tests/stubtest_allowlist.txt +++ b/stubs/tqdm/@tests/stubtest_allowlist.txt @@ -1,4 +1 @@ tqdm.contrib.discord - -# Shims -tqdm._rich_shims diff --git a/stubs/tqdm/tqdm/_rich_shims.pyi b/stubs/tqdm/tqdm/_rich_shims.pyi deleted file mode 100644 index 7c73edf7af4a..000000000000 --- a/stubs/tqdm/tqdm/_rich_shims.pyi +++ /dev/null @@ -1,141 +0,0 @@ -from _typeshed import Incomplete -from abc import ABC, abstractmethod -from collections.abc import Callable, Iterable -from dataclasses import dataclass -from typing import NamedTuple, Protocol, runtime_checkable -from typing_extensions import Literal, TypeAlias - -# rich.text -Text: TypeAlias = str | Incomplete - -# rich.align -VerticalAlignMethod: TypeAlias = Literal["top", "middle", "bottom"] - -# rich.style -StyleType: TypeAlias = str | Incomplete - -# rich.table - -@dataclass -class Column(Protocol): - header: RenderableType - footer: RenderableType - header_style: StyleType - footer_style: StyleType - style: StyleType - justify: JustifyMethod - vertical: VerticalAlignMethod - overflow: OverflowMethod - width: int | None - min_width: int | None - max_width: int | None - ratio: int | None - no_wrap: bool - def copy(self) -> Column: ... - @property - def cells(self) -> Iterable[RenderableType]: ... - @property - def flexible(self) -> bool: ... - -# rich.segment -Segment: TypeAlias = Incomplete - -# region rich.console -HighlighterType: TypeAlias = Callable[[str | Text], Text] -JustifyMethod: TypeAlias = Literal["default", "left", "center", "right", "full"] -OverflowMethod: TypeAlias = Literal["fold", "crop", "ellipsis", "ignore"] -RenderResult: TypeAlias = Iterable[RenderableType | Segment] -Console: TypeAlias = Incomplete - -class NoChange: ... - -class ConsoleDimensions(NamedTuple): - width: int - height: int - -@dataclass -class ConsoleOptions: - size: ConsoleDimensions - legacy_windows: bool - min_width: int - max_width: int - is_terminal: bool - encoding: str - max_height: int - justify: JustifyMethod | None - overflow: OverflowMethod | None - no_wrap: bool | None - highlight: bool | None - markup: bool | None - height: int | None - @property - def ascii_only(self) -> bool: ... - def copy(self) -> ConsoleOptions: ... - def update( - self, - *, - width: int | NoChange, - min_width: int | NoChange, - max_width: int | NoChange, - justify: JustifyMethod | None | NoChange, - overflow: OverflowMethod | None | NoChange, - no_wrap: bool | None | NoChange, - highlight: bool | None | NoChange, - markup: bool | None | NoChange, - height: int | None | NoChange, - ) -> ConsoleOptions: ... - def update_width(self, width: int) -> ConsoleOptions: ... - def update_height(self, height: int) -> ConsoleOptions: ... - def reset_height(self) -> ConsoleOptions: ... - def update_dimensions(self, width: int, height: int) -> ConsoleOptions: ... - -@runtime_checkable -class RichCast(Protocol): - def __rich__(self) -> ConsoleRenderable | RichCast | str: ... - -@runtime_checkable -class ConsoleRenderable(Protocol): - def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult: ... - -RenderableType: TypeAlias = ConsoleRenderable | RichCast | str - -# endregion - -# region rich.progress.Task -@dataclass -class Task(Protocol): - id: int - description: str - total: float | None - completed: float - finished_time: float | None - visible: bool - fields: dict[str, Incomplete] - start_time: float | None - stop_time: float | None - finished_speed: float | None - def get_time(self) -> float: ... - @property - def started(self) -> bool: ... - @property - def remaining(self) -> float | None: ... - @property - def elapsed(self) -> float | None: ... - @property - def finished(self) -> bool: ... - @property - def percentage(self) -> float: ... - @property - def speed(self) -> float | None: ... - @property - def time_remaining(self) -> float | None: ... - -class ProgressColumn(ABC): - max_refresh: float | None - def __init__(self, table_column: Column | None = ...) -> None: ... - def get_table_column(self) -> Column: ... - def __call__(self, task: Task) -> RenderableType: ... - @abstractmethod - def render(self, task: Task) -> RenderableType: ... - -# endregion diff --git a/stubs/tqdm/tqdm/rich.pyi b/stubs/tqdm/tqdm/rich.pyi index 41c51d155c34..c06c63eeb0be 100644 --- a/stubs/tqdm/tqdm/rich.pyi +++ b/stubs/tqdm/tqdm/rich.pyi @@ -1,20 +1,22 @@ from _typeshed import Incomplete, SupportsWrite from collections.abc import Iterable, Mapping -from typing import Generic, NoReturn, TypeVar, overload +from typing import Any, Generic, NoReturn, TypeVar, overload +from typing_extensions import TypeAlias -from ._rich_shims import ProgressColumn from .std import tqdm as std_tqdm __all__ = ["tqdm_rich", "trrange", "tqdm", "trange"] -class FractionColumn(ProgressColumn): +_ProgressColumn: TypeAlias = Any # Actually rich.progress.ProgressColumn + +class FractionColumn(_ProgressColumn): unit_scale: bool unit_divisor: int def __init__(self, unit_scale: bool = ..., unit_divisor: int = ...) -> None: ... def render(self, task): ... -class RateColumn(ProgressColumn): +class RateColumn(_ProgressColumn): unit: str unit_scale: bool unit_divisor: int From 65896f3fcfbfb1a5157df056c293ea261d2c3797 Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 12 Jan 2023 21:51:21 -0500 Subject: [PATCH 4/4] forgot to add bakc allowlist entries --- stubs/tqdm/@tests/stubtest_allowlist.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/stubs/tqdm/@tests/stubtest_allowlist.txt b/stubs/tqdm/@tests/stubtest_allowlist.txt index aff04d2aecaf..5405b8923536 100644 --- a/stubs/tqdm/@tests/stubtest_allowlist.txt +++ b/stubs/tqdm/@tests/stubtest_allowlist.txt @@ -1 +1,5 @@ tqdm.contrib.discord + +# Metaclass differs: +tqdm.rich.FractionColumn +tqdm.rich.RateColumn