Added typing to more files (#2896)

* started pathlib

* blocks.py

* more changes

* fixes

* typing

* formatting

* typing

* renaming files

* changelog

* script

* changelog

* lint

* routes

* renamed

* state

* formatting

* state

* type check script

* remove strictness

* switched to pyright

* switched to pyright

* fixed flaky tests

* fixed test xray

* fixed load test

* fixed blocks tests

* formatting

* fixed components test

* uncomment tests

* fixed interpretation tests

* formatting

* last tests hopefully

* argh lint

* component

* fixed based on review

* refactor

* components.py t yping

* components.py

* formatting

* lint script

* merge

* merge

* lint

* pathlib

* lint

* events too

* lint script

* fixing tests

* lint
This commit is contained in:
Abubakar Abid 2022-12-29 12:33:12 -05:00 committed by GitHub
parent 5c108458aa
commit a3d0fcfab1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 712 additions and 706 deletions

File diff suppressed because it is too large Load Diff

View File

@ -41,7 +41,7 @@ def document_fn(fn: Callable) -> Tuple[str, List[Dict], Dict, Optional[str]]:
return: A dict storing data for the returned annotation and doc
example: Code for an example use of the fn
"""
doc_str = inspect.getdoc(fn)
doc_str = inspect.getdoc(fn) or ""
doc_lines = doc_str.split("\n")
signature = inspect.signature(fn)
description, parameters, returns, examples = [], {}, [], []

View File

@ -4,7 +4,7 @@ of the on-page-load event, which is defined in gr.Blocks().load()."""
from __future__ import annotations
import warnings
from typing import TYPE_CHECKING, Any, AnyStr, Callable, Dict, List, Optional, Set
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Set
from gradio.blocks import Block
from gradio.utils import get_cancel_function
@ -37,18 +37,18 @@ class Changeable(Block):
fn: Callable,
inputs: Component | List[Component] | Set[Component] | None = None,
outputs: Component | List[Component] | None = None,
api_name: AnyStr = None,
status_tracker: Optional[StatusTracker] = None,
api_name: str | None = None,
status_tracker: StatusTracker | None = None,
scroll_to_output: bool = False,
show_progress: bool = True,
queue: Optional[bool] = None,
queue: bool | None = None,
batch: bool = False,
max_batch_size: int = 4,
preprocess: bool = True,
postprocess: bool = True,
cancels: Dict[str, Any] | List[Dict[str, Any]] | None = None,
every: float | None = None,
_js: Optional[str] = None,
_js: str | None = None,
):
"""
This event is triggered when the component's input value changes (e.g. when the user types in a textbox
@ -100,8 +100,8 @@ class Clickable(Block):
fn: Callable,
inputs: Component | List[Component] | Set[Component] | None = None,
outputs: Component | List[Component] | None = None,
api_name: AnyStr = None,
status_tracker: Optional[StatusTracker] = None,
api_name: str | None = None,
status_tracker: StatusTracker | None = None,
scroll_to_output: bool = False,
show_progress: bool = True,
queue=None,
@ -111,7 +111,7 @@ class Clickable(Block):
postprocess: bool = True,
cancels: Dict[str, Any] | List[Dict[str, Any]] | None = None,
every: float | None = None,
_js: Optional[str] = None,
_js: str | None = None,
):
"""
This event is triggered when the component (e.g. a button) is clicked.
@ -164,18 +164,18 @@ class Submittable(Block):
fn: Callable,
inputs: Component | List[Component] | Set[Component] | None = None,
outputs: Component | List[Component] | None = None,
api_name: AnyStr = None,
status_tracker: Optional[StatusTracker] = None,
api_name: str | None = None,
status_tracker: StatusTracker | None = None,
scroll_to_output: bool = False,
show_progress: bool = True,
queue: Optional[bool] = None,
queue: bool | None = None,
batch: bool = False,
max_batch_size: int = 4,
preprocess: bool = True,
postprocess: bool = True,
cancels: Dict[str, Any] | List[Dict[str, Any]] | None = None,
every: float | None = None,
_js: Optional[str] = None,
_js: str | None = None,
):
"""
This event is triggered when the user presses the Enter key while the component (e.g. a textbox) is focused.
@ -229,18 +229,18 @@ class Editable(Block):
fn: Callable,
inputs: Component | List[Component] | Set[Component] | None = None,
outputs: Component | List[Component] | None = None,
api_name: AnyStr = None,
status_tracker: Optional[StatusTracker] = None,
api_name: str | None = None,
status_tracker: StatusTracker | None = None,
scroll_to_output: bool = False,
show_progress: bool = True,
queue: Optional[bool] = None,
queue: bool | None = None,
batch: bool = False,
max_batch_size: int = 4,
preprocess: bool = True,
postprocess: bool = True,
cancels: Dict[str, Any] | List[Dict[str, Any]] | None = None,
every: float | None = None,
_js: Optional[str] = None,
_js: str | None = None,
):
"""
This event is triggered when the user edits the component (e.g. image) using the
@ -293,18 +293,18 @@ class Clearable(Block):
fn: Callable,
inputs: Component | List[Component] | Set[Component] | None = None,
outputs: Component | List[Component] | None = None,
api_name: AnyStr = None,
status_tracker: Optional[StatusTracker] = None,
api_name: str | None = None,
status_tracker: StatusTracker | None = None,
scroll_to_output: bool = False,
show_progress: bool = True,
queue: Optional[bool] = None,
queue: bool | None = None,
batch: bool = False,
max_batch_size: int = 4,
preprocess: bool = True,
postprocess: bool = True,
cancels: Dict[str, Any] | List[Dict[str, Any]] | None = None,
every: float | None = None,
_js: Optional[str] = None,
_js: str | None = None,
):
"""
This event is triggered when the user clears the component (e.g. image or audio)
@ -357,18 +357,18 @@ class Playable(Block):
fn: Callable,
inputs: Component | List[Component] | Set[Component] | None = None,
outputs: Component | List[Component] | None = None,
api_name: AnyStr = None,
status_tracker: Optional[StatusTracker] = None,
api_name: str | None = None,
status_tracker: StatusTracker | None = None,
scroll_to_output: bool = False,
show_progress: bool = True,
queue: Optional[bool] = None,
queue: bool | None = None,
batch: bool = False,
max_batch_size: int = 4,
preprocess: bool = True,
postprocess: bool = True,
cancels: Dict[str, Any] | List[Dict[str, Any]] | None = None,
every: float | None = None,
_js: Optional[str] = None,
_js: str | None = None,
):
"""
This event is triggered when the user plays the component (e.g. audio or video).
@ -419,18 +419,18 @@ class Playable(Block):
fn: Callable,
inputs: Component | List[Component] | Set[Component] | None = None,
outputs: Component | List[Component] | None = None,
api_name: Optional[AnyStr] = None,
status_tracker: Optional[StatusTracker] = None,
api_name: str | None = None,
status_tracker: StatusTracker | None = None,
scroll_to_output: bool = False,
show_progress: bool = True,
queue: Optional[bool] = None,
queue: bool | None = None,
batch: bool = False,
max_batch_size: int = 4,
preprocess: bool = True,
postprocess: bool = True,
cancels: Dict[str, Any] | List[Dict[str, Any]] | None = None,
every: float | None = None,
_js: Optional[str] = None,
_js: str | None = None,
):
"""
This event is triggered when the user pauses the component (e.g. audio or video).
@ -481,18 +481,18 @@ class Playable(Block):
fn: Callable,
inputs: Component | List[Component] | Set[Component] | None = None,
outputs: Component | List[Component] | None = None,
api_name: AnyStr = None,
status_tracker: Optional[StatusTracker] = None,
api_name: str | None = None,
status_tracker: StatusTracker | None = None,
scroll_to_output: bool = False,
show_progress: bool = True,
queue: Optional[bool] = None,
queue: bool | None = None,
batch: bool = False,
max_batch_size: int = 4,
preprocess: bool = True,
postprocess: bool = True,
cancels: Dict[str, Any] | List[Dict[str, Any]] | None = None,
every: float | None = None,
_js: Optional[str] = None,
_js: str | None = None,
):
"""
This event is triggered when the user stops the component (e.g. audio or video).
@ -545,18 +545,18 @@ class Streamable(Block):
fn: Callable,
inputs: Component | List[Component] | Set[Component] | None = None,
outputs: Component | List[Component] | None = None,
api_name: AnyStr = None,
status_tracker: Optional[StatusTracker] = None,
api_name: str | None = None,
status_tracker: StatusTracker | None = None,
scroll_to_output: bool = False,
show_progress: bool = False,
queue: Optional[bool] = None,
queue: bool | None = None,
batch: bool = False,
max_batch_size: int = 4,
preprocess: bool = True,
postprocess: bool = True,
cancels: Dict[str, Any] | List[Dict[str, Any]] | None = None,
every: float | None = None,
_js: Optional[str] = None,
_js: str | None = None,
):
"""
This event is triggered when the user streams the component (e.g. a live webcam
@ -611,17 +611,17 @@ class Blurrable(Block):
fn: Callable,
inputs: Component | List[Component] | Set[Component] | None = None,
outputs: Component | List[Component] | None = None,
api_name: AnyStr = None,
api_name: str | None = None,
scroll_to_output: bool = False,
show_progress: bool = True,
queue: Optional[bool] = None,
queue: bool | None = None,
batch: bool = False,
max_batch_size: int = 4,
preprocess: bool = True,
postprocess: bool = True,
cancels: Dict[str, Any] | List[Dict[str, Any]] | None = None,
every: float | None = None,
_js: Optional[str] = None,
_js: str | None = None,
):
"""
This event is triggered when the component's is unfocused/blurred (e.g. when the user clicks outside of a textbox). This method can be used when this component is in a Gradio Blocks.
@ -668,17 +668,17 @@ class Uploadable(Block):
fn: Callable,
inputs: List[Component],
outputs: Component | List[Component] | None = None,
api_name: AnyStr = None,
api_name: str | None = None,
scroll_to_output: bool = False,
show_progress: bool = True,
queue: Optional[bool] = None,
queue: bool | None = None,
batch: bool = False,
max_batch_size: int = 4,
preprocess: bool = True,
postprocess: bool = True,
cancels: List[Dict[str, Any]] | None = None,
every: float | None = None,
_js: Optional[str] = None,
_js: str | None = None,
):
"""
This event is triggered when the user uploads a file into the component (e.g. when the user uploads a video into a video component). This method can be used when this component is in a Gradio Blocks.

View File

@ -248,7 +248,7 @@ def convert_to_16_bit_wav(data):
##################
def decode_base64_to_binary(encoding):
def decode_base64_to_binary(encoding) -> Tuple[bytes, str | None]:
extension = get_extension(encoding)
data = encoding.split(",")[1]
return base64.b64decode(data), extension
@ -300,7 +300,7 @@ def dict_or_str_to_json_file(jsn, dir=None):
return file_obj
def file_to_json(file_path):
def file_to_json(file_path: str) -> Dict:
return json.load(open(file_path))

View File

@ -159,7 +159,7 @@ class FileSerializable(Serializable):
class JSONSerializable(Serializable):
def serialize(
self, x: str, load_dir: str = "", encryption_key: bytes | None = None
) -> str:
) -> Dict | None:
"""
Convert from a a human-friendly version (string path to json file) to a
serialized representation (json string)

View File

@ -5,4 +5,5 @@ pip_required
pip install --upgrade pip
pip install pyright
pyright gradio/context.py gradio/blocks.py
cd gradio
pyright blocks.py components.py context.py data_classes.py deprecation.py documentation.py encryptor.py events.py

View File

@ -288,7 +288,7 @@ class TestNumber:
(1.94, -0.23640000000000017),
(1.96, -0.15840000000000032),
(1.98, -0.07960000000000012),
[2, None],
(2, None),
(2.02, 0.08040000000000003),
(2.04, 0.16159999999999997),
(2.06, 0.24359999999999982),
@ -310,7 +310,7 @@ class TestNumber:
(1.94, 0.0),
(1.96, 0.0),
(1.98, 0.0),
[2, None],
(2, None),
(2.02, 0.0),
(2.04, 0.0),
(2.06, 0.0),
@ -331,7 +331,7 @@ class TestNumber:
(1.94, -0.23640000000000017),
(1.96, -0.15840000000000032),
(1.98, -0.07960000000000012),
[2, None],
(2, None),
(2.02, 0.08040000000000003),
(2.04, 0.16159999999999997),
(2.06, 0.24359999999999982),
@ -1331,7 +1331,7 @@ class TestTimeseries:
assert timeseries_output.get_config() == {
"x": None,
"y": None,
"y": [None],
"name": "timeseries",
"show_label": True,
"label": "Disease",