From 1bd13d847ce39fc59208988999c7521e046ed734 Mon Sep 17 00:00:00 2001 From: Avasam Date: Wed, 11 Jan 2023 21:07:54 -0500 Subject: [PATCH 1/6] 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/6] 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 efc1939a540635f244fc24d0d0551febdbf297db Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 14 Jan 2023 06:10:16 -0500 Subject: [PATCH 3/6] Simpler shim --- stubs/tqdm/tqdm/_rich_shims.pyi | 51 ++------------------------------- 1 file changed, 3 insertions(+), 48 deletions(-) diff --git a/stubs/tqdm/tqdm/_rich_shims.pyi b/stubs/tqdm/tqdm/_rich_shims.pyi index 7c73edf7af4a..f5704d3a5714 100644 --- a/stubs/tqdm/tqdm/_rich_shims.pyi +++ b/stubs/tqdm/tqdm/_rich_shims.pyi @@ -1,13 +1,10 @@ from _typeshed import Incomplete from abc import ABC, abstractmethod -from collections.abc import Callable, Iterable +from collections.abc import Iterable from dataclasses import dataclass -from typing import NamedTuple, Protocol, runtime_checkable +from typing import Protocol, runtime_checkable from typing_extensions import Literal, TypeAlias -# rich.text -Text: TypeAlias = str | Incomplete - # rich.align VerticalAlignMethod: TypeAlias = Literal["top", "middle", "bottom"] @@ -41,53 +38,11 @@ class Column(Protocol): 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: ... +ConsoleOptions: TypeAlias = Incomplete @runtime_checkable class RichCast(Protocol): From 7e45e5d3509ae561e12f1bf2b892cdb2675f5673 Mon Sep 17 00:00:00 2001 From: Avasam Date: Mon, 6 Feb 2023 18:29:04 -0500 Subject: [PATCH 4/6] Keeping it simple --- stubs/tqdm/@tests/stubtest_allowlist.txt | 6 +- stubs/tqdm/METADATA.toml | 1 + stubs/tqdm/tqdm/_rich_shims.pyi | 96 ------------------------ stubs/tqdm/tqdm/rich.pyi | 15 +++- 4 files changed, 16 insertions(+), 102 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..b142ecf1ff65 100644 --- a/stubs/tqdm/@tests/stubtest_allowlist.txt +++ b/stubs/tqdm/@tests/stubtest_allowlist.txt @@ -1,4 +1,4 @@ +# Cannot import in stubtest +tqdm.__main__ +# disco-py fails to install through pip and is an archived project tqdm.contrib.discord - -# Shims -tqdm._rich_shims diff --git a/stubs/tqdm/METADATA.toml b/stubs/tqdm/METADATA.toml index 8b856db6e193..c9febe7ea83d 100644 --- a/stubs/tqdm/METADATA.toml +++ b/stubs/tqdm/METADATA.toml @@ -1,4 +1,5 @@ version = "4.64.*" [tool.stubtest] +ignore_missing_stub = false extras = ["slack", "telegram"] diff --git a/stubs/tqdm/tqdm/_rich_shims.pyi b/stubs/tqdm/tqdm/_rich_shims.pyi deleted file mode 100644 index f5704d3a5714..000000000000 --- a/stubs/tqdm/tqdm/_rich_shims.pyi +++ /dev/null @@ -1,96 +0,0 @@ -from _typeshed import Incomplete -from abc import ABC, abstractmethod -from collections.abc import Iterable -from dataclasses import dataclass -from typing import Protocol, runtime_checkable -from typing_extensions import Literal, TypeAlias - -# 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 -JustifyMethod: TypeAlias = Literal["default", "left", "center", "right", "full"] -OverflowMethod: TypeAlias = Literal["fold", "crop", "ellipsis", "ignore"] -RenderResult: TypeAlias = Iterable[RenderableType | Segment] -Console: TypeAlias = Incomplete -ConsoleOptions: TypeAlias = Incomplete - -@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..2a81cc21fe1c 100644 --- a/stubs/tqdm/tqdm/rich.pyi +++ b/stubs/tqdm/tqdm/rich.pyi @@ -1,20 +1,29 @@ from _typeshed import Incomplete, SupportsWrite +from abc import ABC, abstractmethod from collections.abc import Iterable, Mapping 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"] -class FractionColumn(ProgressColumn): +# Actually rich.progress.ProgressColumn +class _ProgressColumn(ABC): + max_refresh: float | None + def __init__(self, table_column: Incomplete | None = ...) -> None: ... + def get_table_column(self) -> Incomplete: ... + def __call__(self, task: Incomplete) -> Incomplete: ... + @abstractmethod + def render(self, task: Incomplete) -> Incomplete: ... + +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 f1eb6c763f3ee13b883c1104365eab3e96f9cef1 Mon Sep 17 00:00:00 2001 From: Avasam Date: Mon, 6 Feb 2023 18:35:49 -0500 Subject: [PATCH 5/6] colorama re-export on linux --- stubs/tqdm/@tests/stubtest_allowlist_linux.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 stubs/tqdm/@tests/stubtest_allowlist_linux.txt diff --git a/stubs/tqdm/@tests/stubtest_allowlist_linux.txt b/stubs/tqdm/@tests/stubtest_allowlist_linux.txt new file mode 100644 index 000000000000..6bf0661efa68 --- /dev/null +++ b/stubs/tqdm/@tests/stubtest_allowlist_linux.txt @@ -0,0 +1,2 @@ +#Unintended re-export of the colorama module +tqdm\._?utils\.colorama From ed184b844b01c913e3e0ddfbb6d694051e6e6d91 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 7 Feb 2023 00:05:02 +0000 Subject: [PATCH 6/6] Remove unused allowlist entry --- stubs/tqdm/@tests/stubtest_allowlist_linux.txt | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 stubs/tqdm/@tests/stubtest_allowlist_linux.txt diff --git a/stubs/tqdm/@tests/stubtest_allowlist_linux.txt b/stubs/tqdm/@tests/stubtest_allowlist_linux.txt deleted file mode 100644 index 6bf0661efa68..000000000000 --- a/stubs/tqdm/@tests/stubtest_allowlist_linux.txt +++ /dev/null @@ -1,2 +0,0 @@ -#Unintended re-export of the colorama module -tqdm\._?utils\.colorama