Skip to content

Commit 245249a

Browse files
authored
Merge pull request #3254 from plotly/pyright-typing-fixes
feat: adding more type annotations
2 parents 700b706 + 6b75843 commit 245249a

29 files changed

+277
-229
lines changed

components/dash-core-components/dash_core_components_base/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
send_string,
1414
)
1515

16-
__all__ = _components + [
16+
__all__ = _components + [ # type: ignore[reportUnsupportedDunderAll]
1717
"send_bytes",
1818
"send_data_frame",
1919
"send_file",

components/dash-table/dash_table_base/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# type: ignore
2+
13
import os as _os
24
import sys as _sys
35
import json

components/dash-table/src/dash-table/dash/DataTable.js

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -472,25 +472,14 @@ export const propTypes = {
472472
* View the documentation examples to learn more.
473473
*
474474
*/
475-
fixed_columns: PropTypes.oneOfType([
476-
PropTypes.exact({
477-
/**
478-
* Example `{'headers':False, 'data':0}` No columns are fixed (the default)
479-
*/
480-
481-
data: PropTypes.oneOf([0]),
482-
headers: PropTypes.oneOf([false])
483-
}),
484-
485-
PropTypes.exact({
486-
/**
487-
* Example `{'headers':True, 'data':1}` one column is fixed.
488-
*/
475+
fixed_columns: PropTypes.exact({
476+
/**
477+
* Example `{'headers':False, 'data':0}` No columns are fixed (the default)
478+
*/
489479

490-
data: PropTypes.number,
491-
headers: PropTypes.oneOf([true]).isRequired
492-
})
493-
]),
480+
data: PropTypes.number,
481+
headers: PropTypes.bool
482+
}),
494483

495484
/**
496485
* `fixed_rows` will "fix" the set of rows so that
@@ -505,24 +494,14 @@ export const propTypes = {
505494
* way that your columns are rendered or sized.
506495
* View the documentation examples to learn more.
507496
*/
508-
fixed_rows: PropTypes.oneOfType([
509-
PropTypes.exact({
510-
/**
511-
* Example `{'headers':False, 'data':0}` No rows are fixed (the default)
512-
*/
513-
514-
data: PropTypes.oneOf([0]),
515-
headers: PropTypes.oneOf([false])
516-
}),
517-
PropTypes.exact({
518-
/**
519-
* Example `{'headers':True, 'data':1}` one row is fixed.
520-
*/
497+
fixed_rows: PropTypes.exact({
498+
/**
499+
* Example `{'headers':False, 'data':0}` No rows are fixed (the default)
500+
*/
521501

522-
data: PropTypes.number,
523-
headers: PropTypes.oneOf([true]).isRequired
524-
})
525-
]),
502+
data: PropTypes.number,
503+
headers: PropTypes.bool
504+
}),
526505

527506
/**
528507
* If `single`, then the user can select a single column or group

dash/_callback.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,9 @@ def register_callback(
365365
# pylint: disable=too-many-locals
366366
def wrap_func(func):
367367

368-
if background is not None:
368+
if background is None:
369+
background_key = None
370+
else:
369371
background_key = BaseBackgroundCallbackManager.register_func(
370372
func,
371373
background.get("progress") is not None,
@@ -520,7 +522,7 @@ def add_context(*args, **kwargs):
520522
return to_json(response)
521523
else:
522524
try:
523-
output_value = _invoke_callback(func, *func_args, **func_kwargs)
525+
output_value = _invoke_callback(func, *func_args, **func_kwargs) # type: ignore[reportArgumentType]
524526
except PreventUpdate as err:
525527
raise err
526528
except Exception as err: # pylint: disable=broad-exception-caught
@@ -560,7 +562,7 @@ def add_context(*args, **kwargs):
560562
if NoUpdate.is_no_update(val):
561563
continue
562564
for vali, speci in (
563-
zip(val, spec) if isinstance(spec, list) else [[val, spec]]
565+
zip(val, spec) if isinstance(spec, list) else [[val, spec]] # type: ignore[reportArgumentType]]
564566
):
565567
if not NoUpdate.is_no_update(vali):
566568
has_update = True
@@ -595,6 +597,7 @@ def add_context(*args, **kwargs):
595597
dist = app.get_dist(diff_packages)
596598
response["dist"] = dist
597599

600+
jsonResponse = None
598601
try:
599602
jsonResponse = to_json(response)
600603
except TypeError:

dash/_callback_context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,15 @@ def response(self):
207207

208208
@staticmethod
209209
@has_context
210-
def record_timing(name, duration=None, description=None):
210+
def record_timing(name, duration, description=None):
211211
"""Records timing information for a server resource.
212212
213213
:param name: The name of the resource.
214214
:type name: string
215215
216216
:param duration: The time in seconds to report. Internally, this
217217
is rounded to the nearest millisecond.
218-
:type duration: float or None
218+
:type duration: float
219219
220220
:param description: A description of the resource.
221221
:type description: string or None

dash/_grouping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
structure
1414
1515
"""
16-
from dash.exceptions import InvalidCallbackReturnValue
16+
from .exceptions import InvalidCallbackReturnValue
1717
from ._utils import AttributeDict, stringify_id
1818

1919

dash/_hooks.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def add_hook(
6161
hook: str,
6262
func: _t.Callable,
6363
priority: _t.Optional[int] = None,
64-
final=False,
65-
data=None,
64+
final: bool = False,
65+
data: _t.Optional[HookDataType] = None,
6666
):
6767
if final:
6868
existing = self._finals.get(hook)
@@ -117,7 +117,7 @@ def route(
117117
name: _t.Optional[str] = None,
118118
methods: _t.Sequence[str] = ("GET",),
119119
priority: _t.Optional[int] = None,
120-
final=False,
120+
final: bool = False,
121121
):
122122
"""
123123
Add a route to the Dash server.
@@ -136,7 +136,7 @@ def wrap(func: _t.Callable[[], _f.Response]):
136136

137137
return wrap
138138

139-
def error(self, priority: _t.Optional[int] = None, final=False):
139+
def error(self, priority: _t.Optional[int] = None, final: bool = False):
140140
"""Automatically add an error handler to the dash app."""
141141

142142
def _error(func: _t.Callable[[Exception], _t.Any]):
@@ -145,7 +145,9 @@ def _error(func: _t.Callable[[Exception], _t.Any]):
145145

146146
return _error
147147

148-
def callback(self, *args, priority: _t.Optional[int] = None, final=False, **kwargs):
148+
def callback(
149+
self, *args, priority: _t.Optional[int] = None, final: bool = False, **kwargs
150+
):
149151
"""
150152
Add a callback to all the apps with the hook installed.
151153
"""

dash/_jupyter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# type: ignore
12
import asyncio
23
import io
34
import inspect

dash/_pages.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import collections
22
import importlib
3+
import importlib.util # to make the type checker happy
34
import os
45
import re
56
import sys
@@ -85,9 +86,11 @@ def _infer_path(module_name, template):
8586

8687

8788
def _module_name_is_package(module_name):
89+
file_path = sys.modules[module_name].__file__
8890
return (
89-
module_name in sys.modules
90-
and Path(sys.modules[module_name].__file__).name == "__init__.py"
91+
file_path
92+
and module_name in sys.modules
93+
and Path(file_path).name == "__init__.py"
9194
)
9295

9396

@@ -441,8 +444,8 @@ def _import_layouts_from_pages(pages_folder):
441444

442445
module_name = _infer_module_name(page_path)
443446
spec = importlib.util.spec_from_file_location(module_name, page_path)
444-
page_module = importlib.util.module_from_spec(spec)
445-
spec.loader.exec_module(page_module)
447+
page_module = importlib.util.module_from_spec(spec) # type: ignore[reportArgumentType]
448+
spec.loader.exec_module(page_module) # type: ignore[reportOptionalMemberAccess]
446449
sys.modules[module_name] = page_module
447450

448451
if (

dash/_patch.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
from typing import List, Union, Optional, Any
2+
3+
14
def _operation(name, location, **kwargs):
25
return {"operation": name, "location": location, "params": dict(**kwargs)}
36

47

58
_noop = object()
69

10+
_KeyType = Union[str, int]
11+
712

8-
def validate_slice(obj):
13+
def validate_slice(obj: Any):
914
if isinstance(obj, slice):
1015
raise TypeError("a slice is not a valid index for patch")
1116

@@ -19,7 +24,11 @@ class Patch:
1924
Supported prop types: Dictionaries and lists.
2025
"""
2126

22-
def __init__(self, location=None, parent=None):
27+
def __init__(
28+
self,
29+
location: Optional[List[_KeyType]] = None,
30+
parent: Optional["Patch"] = None,
31+
):
2332
if location is not None:
2433
self._location = location
2534
else:
@@ -36,11 +45,11 @@ def __getstate__(self):
3645
def __setstate__(self, state):
3746
vars(self).update(state)
3847

39-
def __getitem__(self, item) -> "Patch":
48+
def __getitem__(self, item: _KeyType) -> "Patch":
4049
validate_slice(item)
4150
return Patch(location=self._location + [item], parent=self)
4251

43-
def __getattr__(self, item) -> "Patch":
52+
def __getattr__(self, item: _KeyType) -> "Patch":
4453
if item == "tolist":
4554
# to_json fix
4655
raise AttributeError
@@ -50,16 +59,16 @@ def __getattr__(self, item) -> "Patch":
5059
return self._operations # type: ignore
5160
return self.__getitem__(item)
5261

53-
def __setattr__(self, key, value):
62+
def __setattr__(self, key: _KeyType, value: Any):
5463
if key in ("_location", "_operations"):
5564
self.__dict__[key] = value
5665
else:
5766
self.__setitem__(key, value)
5867

59-
def __delattr__(self, item):
68+
def __delattr__(self, item: _KeyType):
6069
self.__delitem__(item)
6170

62-
def __setitem__(self, key, value):
71+
def __setitem__(self, key: _KeyType, value: Any):
6372
validate_slice(key)
6473
if value is _noop:
6574
# The += set themselves.
@@ -72,11 +81,11 @@ def __setitem__(self, key, value):
7281
)
7382
)
7483

75-
def __delitem__(self, key):
84+
def __delitem__(self, key: _KeyType):
7685
validate_slice(key)
7786
self._operations.append(_operation("Delete", self._location + [key]))
7887

79-
def __iadd__(self, other):
88+
def __iadd__(self, other: Any):
8089
if isinstance(other, (list, tuple)):
8190
self.extend(other)
8291
else:
@@ -85,25 +94,25 @@ def __iadd__(self, other):
8594
return self
8695
return _noop
8796

88-
def __isub__(self, other):
97+
def __isub__(self, other: Any):
8998
self._operations.append(_operation("Sub", self._location, value=other))
9099
if not self._location:
91100
return self
92101
return _noop
93102

94-
def __imul__(self, other):
103+
def __imul__(self, other: Any) -> "Patch":
95104
self._operations.append(_operation("Mul", self._location, value=other))
96105
if not self._location:
97106
return self
98107
return _noop
99108

100-
def __itruediv__(self, other):
109+
def __itruediv__(self, other: Any):
101110
self._operations.append(_operation("Div", self._location, value=other))
102111
if not self._location:
103112
return self
104113
return _noop
105114

106-
def __ior__(self, other):
115+
def __ior__(self, other: Any):
107116
self.update(E=other)
108117
if not self._location:
109118
return self
@@ -115,39 +124,39 @@ def __iter__(self):
115124
def __repr__(self):
116125
return f"<write-only dash.Patch object at {self._location}>"
117126

118-
def append(self, item):
127+
def append(self, item: Any) -> None:
119128
"""Add the item to the end of a list"""
120129
self._operations.append(_operation("Append", self._location, value=item))
121130

122-
def prepend(self, item):
131+
def prepend(self, item: Any) -> None:
123132
"""Add the item to the start of a list"""
124133
self._operations.append(_operation("Prepend", self._location, value=item))
125134

126-
def insert(self, index, item):
135+
def insert(self, index: int, item: Any) -> None:
127136
"""Add the item at the index of a list"""
128137
self._operations.append(
129138
_operation("Insert", self._location, value=item, index=index)
130139
)
131140

132-
def clear(self):
141+
def clear(self) -> None:
133142
"""Remove all items in a list"""
134143
self._operations.append(_operation("Clear", self._location))
135144

136-
def reverse(self):
145+
def reverse(self) -> None:
137146
"""Reversal of the order of items in a list"""
138147
self._operations.append(_operation("Reverse", self._location))
139148

140-
def extend(self, item):
149+
def extend(self, item: Union[list, tuple]) -> None:
141150
"""Add all the items to the end of a list"""
142151
if not isinstance(item, (list, tuple)):
143152
raise TypeError(f"{item} should be a list or tuple")
144153
self._operations.append(_operation("Extend", self._location, value=item))
145154

146-
def remove(self, item):
155+
def remove(self, item: Any) -> None:
147156
"""filter the item out of a list on the frontend"""
148157
self._operations.append(_operation("Remove", self._location, value=item))
149158

150-
def update(self, E=None, **F):
159+
def update(self, E: Any = None, **F) -> None:
151160
"""Merge a dict or keyword arguments with another dictionary"""
152161
value = E or {}
153162
value.update(F)
@@ -159,7 +168,7 @@ def sort(self):
159168
"sort is reserved for future use, use brackets to access this key on your object"
160169
)
161170

162-
def to_plotly_json(self):
171+
def to_plotly_json(self) -> Any:
163172
return {
164173
"__dash_patch_update": "__dash_patch_update",
165174
"operations": self._operations,

0 commit comments

Comments
 (0)