From 9c551c316c643b9466daae5083bd805dd54ab098 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Tue, 27 Jun 2023 21:07:40 +0300 Subject: [PATCH] More proper deprecation warnings (#4694) * Add GradioDeprecationWarning subclass * Add and use find_user_stack_level(); add warn_deprecation() * Deduplicate `.style()` deprecation warning * Deduplicate inputs deprecation warnings * Deduplicate outputs deprecation warnings * Use warn_deprecation for deprecation warnings * Changelog * formatting --------- Co-authored-by: Abubakar Abid --- CHANGELOG.md | 1 + gradio/blocks.py | 31 ++++++------ gradio/components/annotated_image.py | 6 +-- gradio/components/base.py | 19 ++++---- gradio/components/button.py | 13 +++-- gradio/components/chatbot.py | 10 ++-- gradio/components/checkboxgroup.py | 8 ++-- gradio/components/dropdown.py | 5 +- gradio/components/file.py | 3 +- gradio/components/gallery.py | 8 ++-- gradio/components/highlighted_text.py | 6 +-- gradio/components/image.py | 5 +- gradio/components/json_component.py | 6 +-- gradio/components/label.py | 6 +-- gradio/components/plot.py | 6 +-- gradio/components/radio.py | 8 ++-- gradio/components/slider.py | 6 +-- gradio/components/status_tracker.py | 6 +-- gradio/components/textbox.py | 6 +-- gradio/components/upload_button.py | 10 ++-- gradio/components/video.py | 5 +- gradio/deprecation.py | 35 ++++++++++++-- gradio/events.py | 6 +-- gradio/external.py | 6 ++- gradio/flagging.py | 6 +-- gradio/inputs.py | 68 +++++++++------------------ gradio/interface.py | 7 ++- gradio/layouts.py | 10 ++-- gradio/outputs.py | 65 +++++++++---------------- gradio/utils.py | 15 ++++++ 30 files changed, 182 insertions(+), 210 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62a9c7dcf7b00..faa8785250294 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - Ensure that Gradio does not silently fail when running on a port that is occupied by [@abidlabs](https://github.com/abidlabs) in [PR 4624](https://github.com/gradio-app/gradio/pull/4624). - Fix double upload bug that caused lag in file uploads by [@aliabid94](https://github.com/aliabid94) in [PR 4661](https://github.com/gradio-app/gradio/pull/4661) - `Progress` component now appears even when no `iterable` is specified in `tqdm` constructor by [@itrushkin](https://github.com/itrushkin) in [PR 4475](https://github.com/gradio-app/gradio/pull/4475) +- Deprecation warnings now point at the user code using those deprecated features, instead of Gradio internals, by (https://github.com/akx) in [PR 4694](https://github.com/gradio-app/gradio/pull/4694) ## Other Changes: diff --git a/gradio/blocks.py b/gradio/blocks.py index bb101e88ac0e6..ff9e3e5a9e2ca 100644 --- a/gradio/blocks.py +++ b/gradio/blocks.py @@ -35,7 +35,7 @@ wasm_utils, ) from gradio.context import Context -from gradio.deprecation import check_deprecated_parameters +from gradio.deprecation import check_deprecated_parameters, warn_deprecation from gradio.exceptions import DuplicateBlockError, InvalidApiNameError from gradio.helpers import EventData, create_tracker, skip, special_args from gradio.themes import Default as DefaultTheme @@ -101,9 +101,7 @@ def __init__( if render: self.render() - check_deprecated_parameters( - self.__class__.__name__, stacklevel=6, kwargs=kwargs - ) + check_deprecated_parameters(self.__class__.__name__, kwargs=kwargs) def render(self): """ @@ -1501,7 +1499,9 @@ def get_time(): demo.launch() """ if isinstance(self_or_cls, type): - warnings.warn("gr.Blocks.load() will be deprecated. Use gr.load() instead.") + warn_deprecation( + "gr.Blocks.load() will be deprecated. Use gr.load() instead." + ) if name is None: raise ValueError( "Blocks.load() requires passing parameters as keyword arguments" @@ -1570,14 +1570,16 @@ def queue( demo.launch() """ if default_enabled is not None: - warnings.warn( + warn_deprecation( "The default_enabled parameter of queue has no effect and will be removed " "in a future version of gradio." ) self.enable_queue = True self.api_open = api_open if client_position_to_load_data is not None: - warnings.warn("The client_position_to_load_data parameter is deprecated.") + warn_deprecation( + "The client_position_to_load_data parameter is deprecated." + ) self._queue = queueing.Queue( live_updates=status_update_rate == "auto", concurrency_count=concurrency_count, @@ -1724,14 +1726,13 @@ def reverse(text): if enable_queue is not None: self.enable_queue = enable_queue - warnings.warn( - "The `enable_queue` parameter has been deprecated. Please use the `.queue()` method instead.", - DeprecationWarning, + warn_deprecation( + "The `enable_queue` parameter has been deprecated. " + "Please use the `.queue()` method instead.", ) if encrypt is not None: - warnings.warn( + warn_deprecation( "The `encrypt` parameter has been deprecated and has no effect.", - DeprecationWarning, ) if self.space_id: @@ -1743,9 +1744,9 @@ def reverse(text): self.show_api = self.api_open if self.enable_queue else show_api if file_directories is not None: - warnings.warn( - "The `file_directories` parameter has been renamed to `allowed_paths`. Please use that instead.", - DeprecationWarning, + warn_deprecation( + "The `file_directories` parameter has been renamed to `allowed_paths`. " + "Please use that instead.", ) if allowed_paths is None: allowed_paths = file_directories diff --git a/gradio/components/annotated_image.py b/gradio/components/annotated_image.py index fe25921ff9834..cdc6902437817 100644 --- a/gradio/components/annotated_image.py +++ b/gradio/components/annotated_image.py @@ -2,7 +2,6 @@ from __future__ import annotations -import warnings from typing import Literal import numpy as np @@ -12,6 +11,7 @@ from gradio import utils from gradio.components.base import IOComponent, _Keywords +from gradio.deprecation import warn_style_method_deprecation from gradio.events import ( EventListenerMethod, Selectable, @@ -233,9 +233,7 @@ def style( """ This method is deprecated. Please set these arguments in the constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if height is not None: self.height = height if width is not None: diff --git a/gradio/components/base.py b/gradio/components/base.py index 6a83bcf7a364c..153681d934c81 100644 --- a/gradio/components/base.py +++ b/gradio/components/base.py @@ -10,7 +10,6 @@ import shutil import tempfile import urllib.request -import warnings from enum import Enum from pathlib import Path from typing import TYPE_CHECKING, Any, Callable @@ -28,6 +27,7 @@ from gradio import processing_utils, utils from gradio.blocks import Block, BlockContext +from gradio.deprecation import warn_deprecation, warn_style_method_deprecation from gradio.events import ( EventListener, ) @@ -90,32 +90,29 @@ def style(self, *args, **kwargs): """ This method is deprecated. Please set these arguments in the Components constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the Components constructor instead." - ) + warn_style_method_deprecation() put_deprecated_params_in_box = False if "rounded" in kwargs: - warnings.warn( + warn_deprecation( "'rounded' styling is no longer supported. To round adjacent components together, place them in a Column(variant='box')." ) if isinstance(kwargs["rounded"], (list, tuple)): put_deprecated_params_in_box = True kwargs.pop("rounded") if "margin" in kwargs: - warnings.warn( + warn_deprecation( "'margin' styling is no longer supported. To place adjacent components together without margin, place them in a Column(variant='box')." ) if isinstance(kwargs["margin"], (list, tuple)): put_deprecated_params_in_box = True kwargs.pop("margin") if "border" in kwargs: - warnings.warn( + warn_deprecation( "'border' styling is no longer supported. To place adjacent components in a shared border, place them in a Column(variant='box')." ) kwargs.pop("border") - if len(kwargs): - for key in kwargs: - warnings.warn(f"Unknown style parameter: {key}") + for key in kwargs: + warn_deprecation(f"Unknown style parameter: {key}") if ( put_deprecated_params_in_box and isinstance(self.parent, (Row, Column)) @@ -162,7 +159,7 @@ def __init__( self.show_label = show_label self.container = container if scale is not None and scale != round(scale): - warnings.warn( + warn_deprecation( f"'scale' value should be an integer. Using {scale} will cause issues." ) self.scale = scale diff --git a/gradio/components/button.py b/gradio/components/button.py index 39d2c6e93cec6..4d932c75becc1 100644 --- a/gradio/components/button.py +++ b/gradio/components/button.py @@ -2,13 +2,13 @@ from __future__ import annotations -import warnings from typing import Callable, Literal from gradio_client.documentation import document, set_documentation_group from gradio_client.serializing import StringSerializable from gradio.components.base import Component, IOComponent, _Keywords +from gradio.deprecation import warn_deprecation, warn_style_method_deprecation from gradio.events import Clickable set_documentation_group("component") @@ -62,7 +62,7 @@ def __init__( **kwargs, ) if variant == "plain": - warnings.warn("'plain' variant deprecated, using 'secondary' instead.") + warn_deprecation("'plain' variant deprecated, using 'secondary' instead.") variant = "secondary" self.variant = variant self.size = size @@ -109,12 +109,11 @@ def style( """ This method is deprecated. Please set these arguments in the constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if full_width is not None: - warnings.warn( - "Use `scale` in place of full_width in the constructor. scale=1 will make the button expand, whereas 0 will not." + warn_deprecation( + "Use `scale` in place of full_width in the constructor. " + "scale=1 will make the button expand, whereas 0 will not." ) self.scale = 1 if full_width else None if size is not None: diff --git a/gradio/components/chatbot.py b/gradio/components/chatbot.py index c41c70c8cd8d9..40c820bdfe627 100644 --- a/gradio/components/chatbot.py +++ b/gradio/components/chatbot.py @@ -3,7 +3,6 @@ from __future__ import annotations import inspect -import warnings from typing import Callable, Literal from gradio_client import utils as client_utils @@ -12,6 +11,7 @@ from gradio import utils from gradio.components.base import IOComponent, _Keywords +from gradio.deprecation import warn_deprecation, warn_style_method_deprecation from gradio.events import ( Changeable, EventListenerMethod, @@ -68,9 +68,7 @@ def __init__( latex_delimiters: A list of dicts of the form {"left": open delimiter (str), "right": close delimiter (str), "display": whether to display in newline (bool)} that will be used to render LaTeX expressions. If not provided, `latex_delimiters` is set to `[{ "left": "$$", "right": "$$", "display": True }]`, so only expressions enclosed in $$ delimiters will be rendered as LaTeX, and in a new line. Pass in an empty list to disable LaTeX rendering. For more information, see the [KaTeX documentation](https://katex.org/docs/autorender.html). """ if color_map is not None: - warnings.warn( - "The 'color_map' parameter has been deprecated.", - ) + warn_deprecation("The 'color_map' parameter has been deprecated.") self.select: EventListenerMethod """ Event listener for when the user selects message from Chatbot. @@ -225,9 +223,7 @@ def style(self, height: int | None = None, **kwargs): """ This method is deprecated. Please set these arguments in the constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if height is not None: self.height = height return self diff --git a/gradio/components/checkboxgroup.py b/gradio/components/checkboxgroup.py index 3e86b8f15afcc..b38c8d90a398c 100644 --- a/gradio/components/checkboxgroup.py +++ b/gradio/components/checkboxgroup.py @@ -2,13 +2,13 @@ from __future__ import annotations -import warnings from typing import Any, Callable, Literal from gradio_client.documentation import document, set_documentation_group from gradio_client.serializing import ListStringSerializable from gradio.components.base import FormComponent, IOComponent, _Keywords +from gradio.deprecation import warn_deprecation, warn_style_method_deprecation from gradio.events import Changeable, EventListenerMethod, Inputable, Selectable from gradio.interpretation import NeighborInterpretable @@ -203,11 +203,9 @@ def style( """ This method is deprecated. Please set these arguments in the constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if item_container is not None: - warnings.warn("The `item_container` parameter is deprecated.") + warn_deprecation("The `item_container` parameter is deprecated.") if container is not None: self.container = container return self diff --git a/gradio/components/dropdown.py b/gradio/components/dropdown.py index 8bc1f133281dd..1d76f904bcd36 100644 --- a/gradio/components/dropdown.py +++ b/gradio/components/dropdown.py @@ -9,6 +9,7 @@ from gradio_client.serializing import SimpleSerializable from gradio.components.base import FormComponent, IOComponent, _Keywords +from gradio.deprecation import warn_style_method_deprecation from gradio.events import ( Blurrable, Changeable, @@ -233,9 +234,7 @@ def style(self, *, container: bool | None = None, **kwargs): """ This method is deprecated. Please set these arguments in the constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if container is not None: self.container = container return self diff --git a/gradio/components/file.py b/gradio/components/file.py index c1cca56950192..9202dc323bbc2 100644 --- a/gradio/components/file.py +++ b/gradio/components/file.py @@ -13,6 +13,7 @@ from gradio import utils from gradio.components.base import IOComponent, _Keywords +from gradio.deprecation import warn_deprecation from gradio.events import ( Changeable, Clearable, @@ -93,7 +94,7 @@ def __init__( f"Invalid value for parameter `type`: {type}. Please choose from one of: {valid_types}" ) if type == "bytes": - warnings.warn( + warn_deprecation( "The `bytes` type is deprecated and may not work as expected. Please use `binary` instead." ) if file_count == "directory" and file_types is not None: diff --git a/gradio/components/gallery.py b/gradio/components/gallery.py index 84468149ffd56..dc397e61b5a5f 100644 --- a/gradio/components/gallery.py +++ b/gradio/components/gallery.py @@ -2,7 +2,6 @@ from __future__ import annotations -import warnings from typing import Any, Callable, Literal import numpy as np @@ -12,6 +11,7 @@ from gradio import utils from gradio.components.base import IOComponent, _Keywords +from gradio.deprecation import warn_deprecation, warn_style_method_deprecation from gradio.events import ( EventListenerMethod, Selectable, @@ -204,11 +204,9 @@ def style( """ This method is deprecated. Please set these arguments in the constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if grid is not None: - warnings.warn( + warn_deprecation( "The 'grid' parameter will be deprecated. Please use 'grid_cols' in the constructor instead.", ) self.grid_cols = grid diff --git a/gradio/components/highlighted_text.py b/gradio/components/highlighted_text.py index ca099cbf60b01..89b6c0aa27e5c 100644 --- a/gradio/components/highlighted_text.py +++ b/gradio/components/highlighted_text.py @@ -2,7 +2,6 @@ from __future__ import annotations -import warnings from typing import Callable, Literal from gradio_client.documentation import document, set_documentation_group @@ -11,6 +10,7 @@ ) from gradio.components.base import IOComponent, _Keywords +from gradio.deprecation import warn_style_method_deprecation from gradio.events import ( Changeable, EventListenerMethod, @@ -197,9 +197,7 @@ def style( """ This method is deprecated. Please set these arguments in the constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if container is not None: self.container = container if color_map is not None: diff --git a/gradio/components/image.py b/gradio/components/image.py index 15168d6c2c8df..aaf1a68b646f0 100644 --- a/gradio/components/image.py +++ b/gradio/components/image.py @@ -16,6 +16,7 @@ from gradio import processing_utils, utils from gradio.components.base import IOComponent, _Keywords +from gradio.deprecation import warn_style_method_deprecation from gradio.events import ( Changeable, Clearable, @@ -389,9 +390,7 @@ def style(self, *, height: int | None = None, width: int | None = None, **kwargs """ This method is deprecated. Please set these arguments in the constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if height is not None: self.height = height if width is not None: diff --git a/gradio/components/json_component.py b/gradio/components/json_component.py index 805dec51f07ae..937d002d2475c 100644 --- a/gradio/components/json_component.py +++ b/gradio/components/json_component.py @@ -3,13 +3,13 @@ from __future__ import annotations import json -import warnings from typing import Any, Callable, Literal from gradio_client.documentation import document, set_documentation_group from gradio_client.serializing import JSONSerializable from gradio.components.base import IOComponent, _Keywords +from gradio.deprecation import warn_style_method_deprecation from gradio.events import ( Changeable, ) @@ -116,9 +116,7 @@ def style(self, *, container: bool | None = None, **kwargs): """ This method is deprecated. Please set these arguments in the constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if container is not None: self.container = container return self diff --git a/gradio/components/label.py b/gradio/components/label.py index 53a08706da7bd..140b6bb27f764 100644 --- a/gradio/components/label.py +++ b/gradio/components/label.py @@ -3,7 +3,6 @@ from __future__ import annotations import operator -import warnings from pathlib import Path from typing import Callable, Literal @@ -13,6 +12,7 @@ ) from gradio.components.base import IOComponent, _Keywords +from gradio.deprecation import warn_style_method_deprecation from gradio.events import ( Changeable, EventListenerMethod, @@ -176,9 +176,7 @@ def style( """ This method is deprecated. Please set these arguments in the constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if container is not None: self.container = container return self diff --git a/gradio/components/plot.py b/gradio/components/plot.py index a88dabf7d7518..203d933afb8b5 100644 --- a/gradio/components/plot.py +++ b/gradio/components/plot.py @@ -3,7 +3,6 @@ from __future__ import annotations import json -import warnings from types import ModuleType from typing import Any, Callable, Literal @@ -14,6 +13,7 @@ from gradio import processing_utils from gradio.components.base import IOComponent, _Keywords +from gradio.deprecation import warn_style_method_deprecation from gradio.events import Changeable, Clearable set_documentation_group("component") @@ -137,9 +137,7 @@ def style(self, container: bool | None = None): """ This method is deprecated. Please set these arguments in the constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if container is not None: self.container = container return self diff --git a/gradio/components/radio.py b/gradio/components/radio.py index 2e2f6019b688f..1a36393e2f745 100644 --- a/gradio/components/radio.py +++ b/gradio/components/radio.py @@ -2,13 +2,13 @@ from __future__ import annotations -import warnings from typing import Any, Callable, Literal from gradio_client.documentation import document, set_documentation_group from gradio_client.serializing import StringSerializable from gradio.components.base import FormComponent, IOComponent, _Keywords +from gradio.deprecation import warn_deprecation, warn_style_method_deprecation from gradio.events import Changeable, EventListenerMethod, Inputable, Selectable from gradio.interpretation import NeighborInterpretable @@ -183,11 +183,9 @@ def style( """ This method is deprecated. Please set these arguments in the constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if item_container is not None: - warnings.warn("The `item_container` parameter is deprecated.") + warn_deprecation("The `item_container` parameter is deprecated.") if container is not None: self.container = container return self diff --git a/gradio/components/slider.py b/gradio/components/slider.py index 4714b07ac85df..665ca46ab582d 100644 --- a/gradio/components/slider.py +++ b/gradio/components/slider.py @@ -4,7 +4,6 @@ import math import random -import warnings from typing import Any, Callable, Literal import numpy as np @@ -12,6 +11,7 @@ from gradio_client.serializing import NumberSerializable from gradio.components.base import FormComponent, IOComponent, _Keywords +from gradio.deprecation import warn_style_method_deprecation from gradio.events import Changeable, Inputable, Releaseable from gradio.interpretation import NeighborInterpretable @@ -202,9 +202,7 @@ def style( """ This method is deprecated. Please set these arguments in the constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if container is not None: self.container = container return self diff --git a/gradio/components/status_tracker.py b/gradio/components/status_tracker.py index 344a49c4f62a2..a9abec2969d93 100644 --- a/gradio/components/status_tracker.py +++ b/gradio/components/status_tracker.py @@ -1,10 +1,8 @@ """gr.StatusTracker() component.""" - -import warnings - from gradio_client.serializing import SimpleSerializable from gradio.components.base import Component +from gradio.deprecation import warn_deprecation class StatusTracker(Component, SimpleSerializable): @@ -12,4 +10,4 @@ def __init__( self, **kwargs, ): - warnings.warn("The StatusTracker component is deprecated.") + warn_deprecation("The StatusTracker component is deprecated.") diff --git a/gradio/components/textbox.py b/gradio/components/textbox.py index 062489d7e2e79..25682cd1d5388 100644 --- a/gradio/components/textbox.py +++ b/gradio/components/textbox.py @@ -2,7 +2,6 @@ from __future__ import annotations -import warnings from typing import Callable, Literal import numpy as np @@ -14,6 +13,7 @@ IOComponent, _Keywords, ) +from gradio.deprecation import warn_style_method_deprecation from gradio.events import ( Blurrable, Changeable, @@ -255,9 +255,7 @@ def style( """ This method is deprecated. Please set these arguments in the constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if show_copy_button is not None: self.show_copy_button = show_copy_button if container is not None: diff --git a/gradio/components/upload_button.py b/gradio/components/upload_button.py index 68205ed6187fb..fb75d5a3723fa 100644 --- a/gradio/components/upload_button.py +++ b/gradio/components/upload_button.py @@ -12,6 +12,7 @@ from gradio import utils from gradio.components.base import Component, IOComponent, _Keywords +from gradio.deprecation import warn_deprecation, warn_style_method_deprecation from gradio.events import Clickable, Uploadable set_documentation_group("component") @@ -198,12 +199,11 @@ def style( """ This method is deprecated. Please set these arguments in the constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if full_width is not None: - warnings.warn( - "Use `scale` in place of full_width in the constructor. scale=1 will make the button expand, whereas 0 will not." + warn_deprecation( + "Use `scale` in place of full_width in the constructor. " + "scale=1 will make the button expand, whereas 0 will not." ) self.scale = 1 if full_width else None if size is not None: diff --git a/gradio/components/video.py b/gradio/components/video.py index cf1b253594e3f..bba61cfa8121d 100644 --- a/gradio/components/video.py +++ b/gradio/components/video.py @@ -14,6 +14,7 @@ from gradio import processing_utils, utils, wasm_utils from gradio.components.base import IOComponent, _Keywords +from gradio.deprecation import warn_style_method_deprecation from gradio.events import Changeable, Clearable, Playable, Recordable, Uploadable if not wasm_utils.IS_WASM: @@ -385,9 +386,7 @@ def style(self, *, height: int | None = None, width: int | None = None, **kwargs """ This method is deprecated. Please set these arguments in the constructor instead. """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if height is not None: self.height = height if width is not None: diff --git a/gradio/deprecation.py b/gradio/deprecation.py index 15d015679039b..f9283df454d8a 100644 --- a/gradio/deprecation.py +++ b/gradio/deprecation.py @@ -1,5 +1,15 @@ +from __future__ import annotations + import warnings +from gradio import utils + + +class GradioDeprecationWarning(UserWarning): + # This does not subclass DeprecationWarning + # because we want to show the warning by default. + pass + def simple_deprecated_notice(term: str) -> str: return f"`{term}` parameter is deprecated, and it has no effect" @@ -32,15 +42,34 @@ def use_in_launch(term: str) -> str: } -def check_deprecated_parameters(cls: str, *, stacklevel: int = 2, kwargs) -> None: +def check_deprecated_parameters( + cls: str, *, stacklevel: int | None = None, kwargs +) -> None: + if stacklevel is None: + stacklevel = utils.find_user_stack_level() + for key, value in DEPRECATION_MESSAGE.items(): if key in kwargs: kwargs.pop(key) - # Interestingly, using DeprecationWarning causes warning to not appear. - warnings.warn(value, stacklevel=stacklevel) + warnings.warn(value, GradioDeprecationWarning, stacklevel=stacklevel) if kwargs: warnings.warn( f"You have unused kwarg parameters in {cls}, please remove them: {kwargs}", + GradioDeprecationWarning, stacklevel=stacklevel, ) + + +def warn_deprecation(text: str) -> None: + warnings.warn( + text, + GradioDeprecationWarning, + stacklevel=utils.find_user_stack_level(), + ) + + +def warn_style_method_deprecation() -> None: + warn_deprecation( + "The `style` method is deprecated. Please set these arguments in the constructor instead." + ) diff --git a/gradio/events.py b/gradio/events.py index 14237d8ceb6ca..d2c12784be8d9 100644 --- a/gradio/events.py +++ b/gradio/events.py @@ -3,12 +3,12 @@ from __future__ import annotations -import warnings from typing import TYPE_CHECKING, Any, Callable, Literal from gradio_client.documentation import document, set_documentation_group from gradio.blocks import Block +from gradio.deprecation import warn_deprecation from gradio.helpers import EventData from gradio.utils import get_cancel_function @@ -123,11 +123,11 @@ def __call__( every: Run this event 'every' number of seconds while the client connection is open. Interpreted in seconds. Queue must be enabled. """ if status_tracker: - warnings.warn( + warn_deprecation( "The 'status_tracker' parameter has been deprecated and has no effect." ) if self.event_name == "stop": - warnings.warn( + warn_deprecation( "The `stop` event on Video and Audio has been deprecated and will be remove in a future version. Use `ended` instead." ) diff --git a/gradio/external.py b/gradio/external.py index 0e753f22e515a..29ad28384cbe1 100644 --- a/gradio/external.py +++ b/gradio/external.py @@ -15,6 +15,7 @@ import gradio from gradio import components, utils from gradio.context import Context +from gradio.deprecation import warn_deprecation from gradio.exceptions import Error, TooManyRequestsError from gradio.external_utils import ( cols_to_rows, @@ -61,8 +62,9 @@ def load( demo.launch() """ if hf_token is None and api_key: - warnings.warn( - "The `api_key` parameter will be deprecated. Please use the `hf_token` parameter going forward." + warn_deprecation( + "The `api_key` parameter will be deprecated. " + "Please use the `hf_token` parameter going forward." ) hf_token = api_key return load_blocks_from_repo( diff --git a/gradio/flagging.py b/gradio/flagging.py index 32467a77259ff..c98828825e74e 100644 --- a/gradio/flagging.py +++ b/gradio/flagging.py @@ -6,7 +6,6 @@ import os import time import uuid -import warnings from abc import ABC, abstractmethod from collections import OrderedDict from distutils.version import StrictVersion @@ -21,6 +20,7 @@ import gradio as gr from gradio import utils +from gradio.deprecation import warn_deprecation if TYPE_CHECKING: from gradio.components import IOComponent @@ -227,7 +227,7 @@ def __init__( separate_dirs: If True, each flagged item will be saved in a separate directory. This makes the flagging more robust to concurrent editing, but may be less convenient to use. """ if organization is not None: - warnings.warn( + warn_deprecation( "Parameter `organization` is not used anymore. Please pass a full dataset id (e.g. 'username/dataset_name') to `dataset_name` instead." ) self.hf_token = hf_token @@ -467,7 +467,7 @@ def __init__( info_filename: str = "dataset_info.json", verbose: bool = True, # silently ignored. TODO: remove it? ): - warnings.warn( + warn_deprecation( "Callback `HuggingFaceDatasetJSONSaver` is deprecated in favor of using" " `HuggingFaceDatasetSaver` and passing `separate_dirs=True` as parameter." ) diff --git a/gradio/inputs.py b/gradio/inputs.py index 35545e3540ba1..9345530649a0b 100644 --- a/gradio/inputs.py +++ b/gradio/inputs.py @@ -7,10 +7,16 @@ from __future__ import annotations -import warnings from typing import Any, Optional from gradio import components +from gradio.deprecation import warn_deprecation + + +def warn_inputs_deprecation(): + warn_deprecation( + "Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components", + ) class Textbox(components.Textbox): @@ -24,9 +30,7 @@ def __init__( label: Optional[str] = None, optional: bool = False, ): - warnings.warn( - "Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components", - ) + warn_inputs_deprecation() super().__init__( value=default, lines=lines, @@ -56,9 +60,7 @@ def __init__( label (str): component name in interface. optional (bool): If True, the interface can be submitted with no value for this component. """ - warnings.warn( - "Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components", - ) + warn_inputs_deprecation() super().__init__(value=default, label=label, optional=optional) @@ -86,9 +88,7 @@ def __init__( label (str): component name in interface. optional (bool): this parameter is ignored. """ - warnings.warn( - "Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components", - ) + warn_inputs_deprecation() super().__init__( value=default, @@ -118,9 +118,7 @@ def __init__( default (bool): if True, checked by default. optional (bool): this parameter is ignored. """ - warnings.warn( - "Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components", - ) + warn_inputs_deprecation() super().__init__(value=default, label=label, optional=optional) @@ -148,9 +146,7 @@ def __init__( """ if default is None: default = [] - warnings.warn( - "Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components", - ) + warn_inputs_deprecation() super().__init__( value=default, choices=choices, @@ -182,9 +178,7 @@ def __init__( label (str): component name in interface. optional (bool): this parameter is ignored. """ - warnings.warn( - "Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components", - ) + warn_inputs_deprecation() super().__init__( choices=choices, type=type, @@ -216,9 +210,7 @@ def __init__( label (str): component name in interface. optional (bool): this parameter is ignored. """ - warnings.warn( - "Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components", - ) + warn_inputs_deprecation() super().__init__( choices=choices, type=type, @@ -256,9 +248,7 @@ def __init__( label (str): component name in interface. optional (bool): If True, the interface can be submitted with no uploaded image, in which case the input value is None. """ - warnings.warn( - "Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components", - ) + warn_inputs_deprecation() super().__init__( shape=shape, image_mode=image_mode, @@ -292,9 +282,7 @@ def __init__( label (str): component name in interface. optional (bool): If True, the interface can be submitted with no uploaded video, in which case the input value is None. """ - warnings.warn( - "Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_inputs_deprecation() super().__init__(format=type, source=source, label=label, optional=optional) @@ -318,9 +306,7 @@ def __init__( label (str): component name in interface. optional (bool): If True, the interface can be submitted with no uploaded audio, in which case the input value is None. """ - warnings.warn( - "Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_inputs_deprecation() super().__init__(source=source, type=type, label=label, optional=optional) @@ -346,9 +332,7 @@ def __init__( keep_filename (bool): DEPRECATED. Original filename always kept. optional (bool): If True, the interface can be submitted with no uploaded image, in which case the input value is None. """ - warnings.warn( - "Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_inputs_deprecation() super().__init__( file_count=file_count, type=type, @@ -388,9 +372,7 @@ def __init__( label (str): component name in interface. optional (bool): this parameter is ignored. """ - warnings.warn( - "Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_inputs_deprecation() super().__init__( value=default, headers=headers, @@ -424,9 +406,7 @@ def __init__( label (str): component name in interface. optional (bool): If True, the interface can be submitted with no uploaded csv file, in which case the input value is None. """ - warnings.warn( - "Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_inputs_deprecation() super().__init__(x=x, y=y, label=label, optional=optional) @@ -447,9 +427,7 @@ def __init__( default (Any): the initial value of the state. optional (bool): this parameter is ignored. """ - warnings.warn( - "Usage of gradio.inputs is deprecated, and will not be supported in the future, please import this component as gr.State() from gradio.components", - ) + warn_inputs_deprecation() super().__init__(value=default, label=label) @@ -469,7 +447,5 @@ def __init__( label (str): component name in interface. optional (bool): If True, the interface can be submitted with no uploaded image, in which case the input value is None. """ - warnings.warn( - "Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_inputs_deprecation() super().__init__(label=label, optional=optional) diff --git a/gradio/interface.py b/gradio/interface.py index e1fa889d1c9fc..9f3207af50350 100644 --- a/gradio/interface.py +++ b/gradio/interface.py @@ -27,6 +27,7 @@ get_component_instance, ) from gradio.data_classes import InterfaceTypes +from gradio.deprecation import warn_deprecation from gradio.events import Changeable, Streamable, Submittable from gradio.flagging import CSVLogger, FlaggingCallback, FlagMethod from gradio.layouts import Column, Row, Tab, Tabs @@ -92,7 +93,9 @@ def load( Returns: a Gradio Interface object for the given model """ - warnings.warn("gr.Interface.load() will be deprecated. Use gr.load() instead.") + warn_deprecation( + "gr.Interface.load() will be deprecated. Use gr.load() instead." + ) return external.load( name=name, src=src, hf_token=api_key, alias=alias, **kwargs ) @@ -878,7 +881,7 @@ def test_launch(self) -> None: """ Deprecated. """ - warnings.warn("The Interface.test_launch() function is deprecated.") + warn_deprecation("The Interface.test_launch() function is deprecated.") @document() diff --git a/gradio/layouts.py b/gradio/layouts.py index b5d06969a6c8e..41af443dccadd 100644 --- a/gradio/layouts.py +++ b/gradio/layouts.py @@ -1,11 +1,11 @@ from __future__ import annotations -import warnings from typing import TYPE_CHECKING, Literal from gradio_client.documentation import document, set_documentation_group from gradio.blocks import BlockContext +from gradio.deprecation import warn_style_method_deprecation from gradio.events import Changeable, Selectable if TYPE_CHECKING: @@ -77,9 +77,7 @@ def style( Parameters: equal_height: If True, makes every child element have equal height """ - warnings.warn( - "The `style` method is deprecated. Please set these arguments in the constructor instead." - ) + warn_style_method_deprecation() if equal_height is not None: self.equal_height = equal_height return self @@ -312,7 +310,7 @@ def update( } def style(self, **kwargs): - warnings.warn("The `style` method is deprecated.") + warn_style_method_deprecation() return self @@ -331,7 +329,7 @@ def add_child(self, child: Block): if isinstance(self.parent, Row): scale = getattr(child, "scale", None) self.scale += 1 if scale is None else scale - self.min_width += getattr(child, "min_width", 0) + self.min_width += getattr(child, "min_width", 0) or 0 super().add_child(child) def get_config(self): diff --git a/gradio/outputs.py b/gradio/outputs.py index c647c07cc4108..b6d2d20c8f5ec 100644 --- a/gradio/outputs.py +++ b/gradio/outputs.py @@ -7,10 +7,17 @@ from __future__ import annotations -import warnings from typing import Optional from gradio import components +from gradio.deprecation import warn_deprecation + + +def warn_outputs_deprecation(): + warn_deprecation( + "Usage of gradio.outputs is deprecated, and will not be supported in the future, " + "please import your components from gradio.components", + ) class Textbox(components.Textbox): @@ -19,9 +26,7 @@ def __init__( type: str = "text", label: Optional[str] = None, ): - warnings.warn( - "Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_outputs_deprecation() super().__init__(label=label, type=type) @@ -40,9 +45,7 @@ def __init__( plot (bool): DEPRECATED. Whether to expect a plot to be returned by the function. label (str): component name in interface. """ - warnings.warn( - "Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_outputs_deprecation() if plot: type = "plot" super().__init__(type=type, label=label) @@ -60,9 +63,7 @@ def __init__(self, type: Optional[str] = None, label: Optional[str] = None): type (str): Type of video format to be passed to component, such as 'avi' or 'mp4'. Use 'mp4' to ensure browser playability. If set to None, video will keep returned format. label (str): component name in interface. """ - warnings.warn( - "Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_outputs_deprecation() super().__init__(format=type, label=label) @@ -78,9 +79,7 @@ def __init__(self, type: str = "auto", label: Optional[str] = None): type (str): Type of value to be passed to component. "numpy" returns a 2-set tuple with an integer sample_rate and the data as 16-bit int numpy.array of shape (samples, 2), "file" returns a temporary file path to the saved wav audio file, "auto" detects return type. label (str): component name in interface. """ - warnings.warn( - "Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_outputs_deprecation() super().__init__(type=type, label=label) @@ -95,9 +94,7 @@ def __init__(self, label: Optional[str] = None): Parameters: label (str): component name in interface. """ - warnings.warn( - "Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_outputs_deprecation() super().__init__(label=label) @@ -125,9 +122,7 @@ def __init__( type (str): Type of value to be passed to component. "pandas" for pandas dataframe, "numpy" for numpy array, or "array" for Python array, "auto" detects return type. label (str): component name in interface. """ - warnings.warn( - "Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_outputs_deprecation() super().__init__( headers=headers, type=type, @@ -153,9 +148,7 @@ def __init__( y (Union[str, List[str]]): Column name of y series, or list of column names if multiple series. None if csv has no headers, in which case every column after first is a y series. label (str): component name in interface. """ - warnings.warn( - "Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_outputs_deprecation() super().__init__(x=x, y=y, label=label) @@ -170,9 +163,7 @@ def __init__(self, label: Optional[str] = None): Parameters: label (str): component name in interface (not used). """ - warnings.warn( - "Usage of gradio.outputs is deprecated, and will not be supported in the future, please import this component as gr.State() from gradio.components", - ) + warn_outputs_deprecation() super().__init__(label=label) @@ -194,9 +185,7 @@ def __init__( type (str): Type of value to be passed to component. "value" expects a single out label, "confidences" expects a dictionary mapping labels to confidence scores, "auto" detects return type. label (str): component name in interface. """ - warnings.warn( - "Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_outputs_deprecation() super().__init__(num_top_classes=num_top_classes, type=type, label=label) @@ -237,9 +226,7 @@ def __init__( label (str): component name in interface. show_legend (bool): whether to show span categories in a separate legend or inline. """ - warnings.warn( - "Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_outputs_deprecation() super().__init__(color_map=color_map, label=label, show_legend=show_legend) @@ -254,9 +241,7 @@ def __init__(self, label: Optional[str] = None): Parameters: label (str): component name in interface. """ - warnings.warn( - "Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_outputs_deprecation() super().__init__(label=label) @@ -289,9 +274,7 @@ def __init__( components (Union[List[Component], Component]): Classes of component(s) that will be scrolled through. label (str): component name in interface. """ - warnings.warn( - "Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_outputs_deprecation() super().__init__(components=components, label=label) @@ -306,9 +289,7 @@ def __init__(self, label: Optional[str] = None): Parameters: label (str): component name in interface (not used). """ - warnings.warn( - "Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_outputs_deprecation() super().__init__(label=label) @@ -328,7 +309,5 @@ def __init__( label (str): component name in interface. optional (bool): If True, the interface can be submitted with no uploaded image, in which case the input value is None. """ - warnings.warn( - "Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components", - ) + warn_outputs_deprecation() super().__init__(clear_color=clear_color, label=label) diff --git a/gradio/utils.py b/gradio/utils.py index 1aeba3ebf11a5..7a0e6f1f108a9 100644 --- a/gradio/utils.py +++ b/gradio/utils.py @@ -932,3 +932,18 @@ def render_blank_link(self, tokens, idx, options, env): def remove_html_tags(raw_html: str | None) -> str: return re.sub(HTML_TAG_RE, "", raw_html or "") + + +def find_user_stack_level() -> int: + """ + Find the first stack frame not inside Gradio. + """ + frame = inspect.currentframe() + n = 0 + while frame: + fname = inspect.getfile(frame) + if "/gradio/" not in fname.replace(os.sep, "/"): + break + frame = frame.f_back + n += 1 + return n