Skip to content

Commit

Permalink
More proper deprecation warnings (#4694)
Browse files Browse the repository at this point in the history
* 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 <abubakar@huggingface.co>
  • Loading branch information
akx and abidlabs authored Jun 27, 2023
1 parent a287ebe commit 9c551c3
Show file tree
Hide file tree
Showing 30 changed files with 182 additions and 210 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
31 changes: 16 additions & 15 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions gradio/components/annotated_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import warnings
from typing import Literal

import numpy as np
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 8 additions & 11 deletions gradio/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions gradio/components/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 3 additions & 7 deletions gradio/components/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
8 changes: 3 additions & 5 deletions gradio/components/checkboxgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
5 changes: 2 additions & 3 deletions gradio/components/dropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion gradio/components/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 3 additions & 5 deletions gradio/components/gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import warnings
from typing import Any, Callable, Literal

import numpy as np
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions gradio/components/highlighted_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import warnings
from typing import Callable, Literal

from gradio_client.documentation import document, set_documentation_group
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
Loading

0 comments on commit 9c551c3

Please sign in to comment.