Skip to content

Commit 2577560

Browse files
FHU-yeziwang0618
andauthored
Add Type Hints to all public funcs (#501)
* style: fix some trailing space and over-indented * add type hints to `input.py` * add type hints to `output.py` * add type hints to `pin.py` * fixed unsorted imports * add type hitns to func `start_server()` * remove some comment which prevent type check * fix wrong type hints for position argument * fix some type check error of internal modules * fix wrong format of import * add Type Hint to `container_scope` arg Co-authored-by: WangWeimin <[email protected]> * Update pywebio/output.py Co-authored-by: WangWeimin <[email protected]> * Update pywebio/output.py Co-authored-by: WangWeimin <[email protected]> * Update pywebio/output.py Co-authored-by: WangWeimin <[email protected]> * Update pywebio/output.py Co-authored-by: WangWeimin <[email protected]> * Update pywebio/output.py Co-authored-by: WangWeimin <[email protected]> * Update pywebio/pin.py Co-authored-by: WangWeimin <[email protected]> * Update pywebio/input.py Co-authored-by: WangWeimin <[email protected]> * fix wrong type of Type Hints Co-authored-by: WangWeimin <[email protected]> * fix typo Co-authored-by: WangWeimin <[email protected]> * Update pywebio/output.py Co-authored-by: WangWeimin <[email protected]> * fix wrong type of `content` arg Co-authored-by: WangWeimin <[email protected]> * fix wrong type of Type Hints Co-authored-by: WangWeimin <[email protected]> * Update pywebio/output.py Co-authored-by: WangWeimin <[email protected]> * Update pywebio/output.py Co-authored-by: WangWeimin <[email protected]> * Update pywebio/output.py Co-authored-by: WangWeimin <[email protected]> * Update pywebio/output.py Co-authored-by: WangWeimin <[email protected]> Co-authored-by: WangWeimin <[email protected]>
1 parent 5242db7 commit 2577560

File tree

11 files changed

+159
-135
lines changed

11 files changed

+159
-135
lines changed

pywebio/input.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@
7676
import logging
7777
import os.path
7878
from collections.abc import Mapping
79+
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
7980

80-
from .io_ctrl import single_input, input_control, output_register_callback, send_msg, single_input_kwargs
81+
from .io_ctrl import input_control, output_register_callback, send_msg, single_input, single_input_kwargs
8182
from .platform import page as platform_setting
8283
from .session import get_current_session, get_current_task_id
83-
from .utils import Setter, parse_file_size, check_dom_name_value
84+
from .utils import Setter, check_dom_name_value, parse_file_size
8485

8586
logger = logging.getLogger(__name__)
8687

@@ -99,9 +100,10 @@
99100
SELECT = 'select'
100101
TEXTAREA = 'textarea'
101102

102-
__all__ = ['TEXT', 'NUMBER', 'FLOAT', 'PASSWORD', 'URL', 'DATE', 'TIME', 'COLOR', 'DATETIME_LOCAL', 'input', 'textarea',
103-
'select',
104-
'checkbox', 'radio', 'actions', 'file_upload', 'slider', 'input_group', 'input_update']
103+
__all__ = ['TEXT', 'NUMBER', 'FLOAT', 'PASSWORD', 'URL', 'DATE',
104+
'TIME', 'COLOR', 'DATETIME_LOCAL', 'input', 'textarea',
105+
'select', 'checkbox', 'radio', 'actions', 'file_upload',
106+
'slider', 'input_group', 'input_update']
105107

106108

107109
def _parse_args(kwargs, excludes=()):
@@ -131,8 +133,9 @@ def _parse_args(kwargs, excludes=()):
131133
return kwargs, valid_func, onchange_func
132134

133135

134-
def input(label='', type=TEXT, *, validate=None, name=None, value=None, action=None, onchange=None, placeholder=None,
135-
required=None, readonly=None, datalist=None, help_text=None, **other_html_attrs):
136+
def input(label: str = '', type: str = TEXT, *, validate: Callable[[Any], Optional[str]] = None, name: str = None, value: str = None,
137+
action: Tuple[str, Callable[[Callable], None]] = None, onchange: Callable[[Any], None] = None, placeholder: str = None, required: bool = None,
138+
readonly: bool = None, datalist: List[str] = None, help_text: str = None, **other_html_attrs):
136139
r"""Text input
137140
138141
:param str label: Label of input field.
@@ -260,8 +263,9 @@ def preprocess_func(d): # Convert the original data submitted by the user
260263
return single_input(item_spec, valid_func, preprocess_func, onchange_func)
261264

262265

263-
def textarea(label='', *, rows=6, code=None, maxlength=None, minlength=None, validate=None, name=None, value=None,
264-
onchange=None, placeholder=None, required=None, readonly=None, help_text=None, **other_html_attrs):
266+
def textarea(label: str = '', *, rows: int = 6, code: Union[bool, Dict] = None, maxlength: int = None, minlength: int = None,
267+
validate: Callable[[Any], Optional[str]] = None, name: str = None, value: str = None, onchange: Callable[[Any], None] = None,
268+
placeholder: str = None, required: bool = None, readonly: bool = None, help_text: str = None, **other_html_attrs):
265269
r"""Text input area (multi-line text input)
266270
267271
:param int rows: The number of visible text lines for the input area. Scroll bar will be used when content exceeds.
@@ -326,8 +330,9 @@ def _set_options_selected(options, value):
326330
return options
327331

328332

329-
def select(label='', options=None, *, multiple=None, validate=None, name=None, value=None, onchange=None, required=None,
330-
help_text=None, **other_html_attrs):
333+
def select(label: str = '', options: List[Union[Dict[str, Any], Tuple, List, str]] = None, *, multiple: bool = None, validate: Callable[[Any], Optional[str]] = None,
334+
name: str = None, value: Union[List, str] = None, onchange: Callable[[Any], None] = None, required: bool = None,
335+
help_text: str = None, **other_html_attrs):
331336
r"""Drop-down selection
332337
333338
By default, only one option can be selected at a time, you can set ``multiple`` parameter to enable multiple selection.
@@ -371,8 +376,9 @@ def select(label='', options=None, *, multiple=None, validate=None, name=None, v
371376
return single_input(item_spec, valid_func=valid_func, preprocess_func=lambda d: d, onchange_func=onchange_func)
372377

373378

374-
def checkbox(label='', options=None, *, inline=None, validate=None, name=None, value=None, onchange=None,
375-
help_text=None, **other_html_attrs):
379+
def checkbox(label: str = '', options: List[Union[Dict[str, Any], Tuple, List, str]] = None, *, inline: bool = None, validate: Callable[[Any], Optional[str]] = None,
380+
name: str = None, value: List = None, onchange: Callable[[Any], None] = None, help_text: str = None,
381+
**other_html_attrs):
376382
r"""A group of check box that allowing single values to be selected/deselected.
377383
378384
:param list options: List of options. The format is the same as the ``options`` parameter of the `select()` function
@@ -393,8 +399,9 @@ def checkbox(label='', options=None, *, inline=None, validate=None, name=None, v
393399
return single_input(item_spec, valid_func, lambda d: d, onchange_func)
394400

395401

396-
def radio(label='', options=None, *, inline=None, validate=None, name=None, value=None, onchange=None, required=None,
397-
help_text=None, **other_html_attrs):
402+
def radio(label: str = '', options: List[Union[Dict[str, Any], Tuple, List, str]] = None, *, inline: bool = None, validate: Callable[[Any], Optional[str]] = None,
403+
name: str = None, value: str = None, onchange: Callable[[Any], None] = None, required: bool = None,
404+
help_text: str = None, **other_html_attrs):
398405
r"""A group of radio button. Only a single button can be selected.
399406
400407
:param list options: List of options. The format is the same as the ``options`` parameter of the `select()` function
@@ -457,7 +464,7 @@ def _parse_action_buttons(buttons):
457464
return act_res
458465

459466

460-
def actions(label='', buttons=None, name=None, help_text=None):
467+
def actions(label: str = '', buttons: List[Union[Dict[str, Any], Tuple, List, str]] = None, name: str = None, help_text: str = None):
461468
r"""Actions selection
462469
463470
It is displayed as a group of buttons on the page. After the user clicks the button of it,
@@ -557,8 +564,9 @@ def actions(label='', buttons=None, name=None, help_text=None):
557564
return single_input(item_spec, valid_func, lambda d: d, onchange_func)
558565

559566

560-
def file_upload(label='', accept=None, name=None, placeholder='Choose file', multiple=False, max_size=0,
561-
max_total_size=0, required=None, help_text=None, **other_html_attrs):
567+
def file_upload(label: str = '', accept: Union[List, str] = None, name: str = None, placeholder: str = 'Choose file',
568+
multiple: bool = False, max_size: Union[int, str] = 0, max_total_size: Union[int, str] = 0,
569+
required: bool = None, help_text: str = None, **other_html_attrs):
562570
r"""File uploading
563571
564572
:param accept: Single value or list, indicating acceptable file types. The available formats of file types are:
@@ -590,14 +598,14 @@ def file_upload(label='', accept=None, name=None, placeholder='Choose file', mul
590598
'mime_type': MIME type of the file,
591599
'last_modified': Last modified time (timestamp) of the file
592600
}
593-
601+
594602
If there is no file uploaded, return ``None``.
595603
596604
When ``multiple=True``, a list is returned. The format of the list item is the same as the return value when ``multiple=False`` above.
597605
If the user does not upload a file, an empty list is returned.
598606
599607
.. note::
600-
608+
601609
If uploading large files, please pay attention to the file upload size limit setting of the web framework.
602610
When using :func:`start_server() <pywebio.platform.tornado.start_server>` or
603611
:func:`path_deploy() <pywebio.platform.path_deploy>` to start the PyWebIO application,
@@ -639,8 +647,9 @@ def read_file(data):
639647
return single_input(item_spec, valid_func, read_file, onchange_func)
640648

641649

642-
def slider(label='', *, name=None, value=0, min_value=0, max_value=100, step=1, validate=None, onchange=None,
643-
required=None, help_text=None, **other_html_attrs):
650+
def slider(label: str = '', *, name: str = None, value: Union[int, float] = 0, min_value: Union[int, float] = 0,
651+
max_value: Union[int, float] = 100, step: int = 1, validate: Callable[[Any], Optional[str]] = None,
652+
onchange: Callable[[Any], None] = None, required: bool = None, help_text: str = None, **other_html_attrs):
644653
r"""Range input.
645654
646655
:param int/float value: The initial value of the slider.
@@ -661,7 +670,7 @@ def slider(label='', *, name=None, value=0, min_value=0, max_value=100, step=1,
661670
return single_input(item_spec, valid_func, lambda d: d, onchange_func)
662671

663672

664-
def input_group(label='', inputs=None, validate=None, cancelable=False):
673+
def input_group(label: str = '', inputs: List = None, validate: Callable[[Dict], Optional[Tuple[str, str]]] = None, cancelable: bool = False):
665674
r"""Input group. Request a set of inputs from the user at once.
666675
667676
:param str label: Label of input group.
@@ -747,7 +756,7 @@ def parse_input_update_spec(spec):
747756
return attributes
748757

749758

750-
def input_update(name=None, **spec):
759+
def input_update(name: str = None, **spec):
751760
"""Update attributes of input field.
752761
This function can only be called in ``onchange`` callback of input functions.
753762

0 commit comments

Comments
 (0)