8
8
import asyncio
9
9
from logging import getLogger
10
10
from threading import get_ident as get_thread_id
11
+ from types import FunctionType
11
12
from typing import (
12
13
Any ,
13
14
Awaitable ,
@@ -101,21 +102,23 @@ def dispatch(
101
102
102
103
@overload
103
104
def use_effect (
104
- function : None = None , args : Optional [Sequence [Any ]] = None
105
+ function : None = None ,
106
+ args : Sequence [Any ] | "ellipsis" | None = ...,
105
107
) -> Callable [[_EffectApplyFunc ], None ]:
106
108
...
107
109
108
110
109
111
@overload
110
112
def use_effect (
111
- function : _EffectApplyFunc , args : Optional [Sequence [Any ]] = None
113
+ function : _EffectApplyFunc ,
114
+ args : Sequence [Any ] | "ellipsis" | None = ...,
112
115
) -> None :
113
116
...
114
117
115
118
116
119
def use_effect (
117
120
function : Optional [_EffectApplyFunc ] = None ,
118
- args : Optional [ Sequence [Any ]] = None ,
121
+ args : Sequence [Any ] | "ellipsis" | None = ... ,
119
122
) -> Optional [Callable [[_EffectApplyFunc ], None ]]:
120
123
"""See the full :ref:`Use Effect` docs for details
121
124
@@ -130,6 +133,8 @@ def use_effect(
130
133
If not function is provided, a decorator. Otherwise ``None``.
131
134
"""
132
135
hook = current_hook ()
136
+
137
+ args = _try_to_infer_closure_args (function , args )
133
138
memoize = use_memo (args = args )
134
139
last_clean_callback : Ref [Optional [_EffectCleanFunc ]] = use_ref (None )
135
140
@@ -209,21 +214,23 @@ def dispatch(action: _ActionType) -> None:
209
214
210
215
@overload
211
216
def use_callback (
212
- function : None = None , args : Optional [Sequence [Any ]] = None
217
+ function : None = None ,
218
+ args : Sequence [Any ] | "ellipsis" | None = ...,
213
219
) -> Callable [[_CallbackFunc ], _CallbackFunc ]:
214
220
...
215
221
216
222
217
223
@overload
218
224
def use_callback (
219
- function : _CallbackFunc , args : Optional [Sequence [Any ]] = None
225
+ function : _CallbackFunc ,
226
+ args : Sequence [Any ] | "ellipsis" | None = ...,
220
227
) -> _CallbackFunc :
221
228
...
222
229
223
230
224
231
def use_callback (
225
232
function : Optional [_CallbackFunc ] = None ,
226
- args : Optional [ Sequence [Any ]] = () ,
233
+ args : Sequence [Any ] | "ellipsis" | None = ... ,
227
234
) -> Union [_CallbackFunc , Callable [[_CallbackFunc ], _CallbackFunc ]]:
228
235
"""See the full :ref:`Use Callback` docs for details
229
236
@@ -234,6 +241,7 @@ def use_callback(
234
241
Returns:
235
242
The current function
236
243
"""
244
+ args = _try_to_infer_closure_args (function , args )
237
245
memoize = use_memo (args = args )
238
246
239
247
def setup (function : _CallbackFunc ) -> _CallbackFunc :
@@ -254,21 +262,23 @@ def __call__(self, func: Callable[[], _StateType]) -> _StateType:
254
262
255
263
@overload
256
264
def use_memo (
257
- function : None = None , args : Optional [Sequence [Any ]] = None
265
+ function : None = None ,
266
+ args : Sequence [Any ] | "ellipsis" | None = ...,
258
267
) -> _LambdaCaller :
259
268
...
260
269
261
270
262
271
@overload
263
272
def use_memo (
264
- function : Callable [[], _StateType ], args : Optional [Sequence [Any ]] = None
273
+ function : Callable [[], _StateType ],
274
+ args : Sequence [Any ] | "ellipsis" | None = ...,
265
275
) -> _StateType :
266
276
...
267
277
268
278
269
279
def use_memo (
270
280
function : Optional [Callable [[], _StateType ]] = None ,
271
- args : Optional [ Sequence [Any ]] = None ,
281
+ args : Sequence [Any ] | "ellipsis" | None = ... ,
272
282
) -> Union [_StateType , Callable [[Callable [[], _StateType ]], _StateType ]]:
273
283
"""See the full :ref:`Use Memo` docs for details
274
284
@@ -279,6 +289,8 @@ def use_memo(
279
289
Returns:
280
290
The current state
281
291
"""
292
+ args = _try_to_infer_closure_args (function , args )
293
+
282
294
memo : _Memo [_StateType ] = _use_const (_Memo )
283
295
284
296
if memo .empty ():
@@ -350,6 +362,23 @@ def _use_const(function: Callable[[], _StateType]) -> _StateType:
350
362
return current_hook ().use_state (function )
351
363
352
364
365
+ def _try_to_infer_closure_args (
366
+ func : Callable [..., Any ] | None ,
367
+ args : Sequence [Any ] | "ellipsis" | None ,
368
+ ) -> Sequence [Any ] | None :
369
+ if args is ...:
370
+ if isinstance (func , FunctionType ):
371
+ return (
372
+ [cell .cell_contents for cell in func .__closure__ ]
373
+ if func .__closure__
374
+ else []
375
+ )
376
+ else :
377
+ return None
378
+ else :
379
+ return cast ("Sequence[Any] | None" , args )
380
+
381
+
353
382
_current_life_cycle_hook : Dict [int , "LifeCycleHook" ] = {}
354
383
355
384
0 commit comments