Skip to content

Commit e16e43e

Browse files
committed
0.2.1 - fix run time signatures
Fix: `utilities.clone_method` and `utilities.clone_function` does not provide correct signature in run time. Correct the signature manually.
1 parent f0e3668 commit e16e43e

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1. Fix: Remove unwanted printed information.
1212
2. Fix: Fix the typos in the docstrings.
1313
3. Fix: Correct the `usage.py` where the instance path and the engine options are not configured properly.
14+
4. Fix: `utilities.clone_method` and `utilities.clone_function` does not provide correct signature in run time. Correct the signature manually.
1415

1516
### 0.2.0 @ 12/13/2024
1617

flask_sqlalchemy_compat/utilities.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
)
5757

5858
__all__ = (
59+
"DBNotReadyError",
5960
"hook_classmethod",
6061
"hook_base_model",
6162
"apply_to_engines",
@@ -96,6 +97,11 @@ def hook_classmethod(
9697
9798
hook_after: `(type, **P) -> T`
9899
The hook that will be run after this class method.
100+
101+
Returns
102+
-------
103+
#1: `(**P) -> T`:
104+
The hooked class method, where the first argument (class) is hidden.
99105
"""
100106

101107
if hook_before is None and hook_after is None:
@@ -178,6 +184,11 @@ def hook_base_model(
178184
179185
db: `flask_sqlalchemy.SQLAlchemy | flask_sqlalchemy_lite.SQLAlchemy`
180186
The db instance used for providing the session.
187+
188+
Returns
189+
-------
190+
#1: `cls`
191+
The hooked base model class.
181192
"""
182193

183194
try:
@@ -241,7 +252,7 @@ def apply_to_engines(
241252

242253

243254
def clone_method(
244-
method: Callable[Concatenate[P], Any],
255+
func_o: Callable[Concatenate[P], Any],
245256
) -> Callable[[Callable[Concatenate[S, P], T]], Callable[Concatenate[S, P], T]]:
246257
"""A decorator for modifying the static type hint of a method by cloning the
247258
signature of another method.
@@ -251,7 +262,7 @@ def clone_method(
251262
252263
Arguments
253264
---------
254-
method: `(**P) -> Any`
265+
func_o: `(**P) -> Any`
255266
The original function providing the signature to be cloned. Its signature is
256267
expected to be the same as the bounded method.
257268
@@ -268,17 +279,28 @@ def clone_method(
268279
"""
269280

270281
def wrapper(func: Callable[Concatenate[S, P], T]) -> Callable[Concatenate[S, P], T]:
271-
_func = functools.update_wrapper(wrapper=func, wrapped=method)
282+
_func = func
272283
_func.__doc__ = "{0}\n\nOriginal Method\n---------------\n\n{1}".format(
273-
inspect.getdoc(func), inspect.getdoc(_func)
284+
inspect.getdoc(func_o), inspect.getdoc(_func)
274285
)
286+
sig_func = inspect.signature(_func)
287+
sig_o = inspect.signature(func_o)
288+
params_func = tuple(sig_func.parameters.values())
289+
if params_func:
290+
sig_o = sig_o.replace(
291+
parameters=(params_func[0], *sig_o.parameters.values()),
292+
return_annotation=sig_func.return_annotation,
293+
)
294+
else:
295+
sig_o = sig_o.replace(return_annotation=sig_func.return_annotation)
296+
setattr(_func, "__signature__", sig_o)
275297
return _func
276298

277299
return wrapper
278300

279301

280302
def clone_function(
281-
func_o: Callable[..., Any]
303+
func_o: Callable[P, Any]
282304
) -> Callable[[Callable[P, T]], Callable[P, T]]:
283305
"""A decorator for modifying the static type hint of a function by cloning the
284306
signature of another function.
@@ -302,10 +324,14 @@ def clone_function(
302324
"""
303325

304326
def wrapper(func: Callable[P, T]) -> Callable[P, T]:
305-
_func = functools.update_wrapper(wrapper=func, wrapped=func_o)
327+
_func = func
306328
_func.__doc__ = "{0}\n\nOriginal Function\n-----------------n\n{1}".format(
307-
inspect.getdoc(func), inspect.getdoc(_func)
329+
inspect.getdoc(func_o), inspect.getdoc(_func)
330+
)
331+
sig_o = inspect.signature(func_o).replace(
332+
return_annotation=inspect.signature(_func).return_annotation
308333
)
334+
setattr(_func, "__signature__", sig_o)
309335
return _func
310336

311337
return wrapper
@@ -321,7 +347,7 @@ class QueryGetter:
321347
322348
Use this descriptor like this:
323349
``` python
324-
class Base:
350+
class Base(sa_orm.DeclarativeBase):
325351
query = QueryGetter()
326352
327353
class NewModel(Base): ...
@@ -386,7 +412,7 @@ class TableNameGetter:
386412
387413
Use this descriptor like this:
388414
``` python
389-
class Base:
415+
class Base(sa_orm.DeclarativeBase):
390416
__tablename__ = TableNameGetter()
391417
392418
class NewModel(Base): ...

0 commit comments

Comments
 (0)