You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functools.singledispatch’s register decorator can take a function with the first argument being type-annotated. That type annotation will then be used to dispatch.
The following code should therefore be correct with all rules active, however it claims rule violations on the annotated lines.
"""Test module."""from __future__ importannotationsfromfunctoolsimportsingledispatchfromtypingimportTYPE_CHECKINGfromnumpyimportasarray# TCH002: Move third-party import `numpy.typing.ArrayLike` into a type-checking blockfromnumpy.typingimportArrayLike# TCH002: Move third-party import `scipy.sparse.spmatrix` into a type-checking blockfromscipy.sparseimportspmatrixifTYPE_CHECKING:
fromnumpyimportndarray@singledispatchdefto_array_or_mat(a: ArrayLike|spmatrix) ->ndarray|spmatrix:
"""Convert arg to array or leaves it as sparse matrix."""msg=f"Unhandled type {type(a)}"raiseNotImplementedError(msg)
@to_array_or_mat.registerdef_(a: ArrayLike) ->ndarray:
returnasarray(a)
@to_array_or_mat.registerdef_(a: spmatrix) ->spmatrix:
returna
The text was updated successfully, but these errors were encountered:
## Summary
When a function uses `@functools.singledispatch`, we need to treat the
first argument of any implementations as runtime-required.
Closes#6849.
Playground link
functools.singledispatch
’sregister
decorator can take a function with the first argument being type-annotated. That type annotation will then be used to dispatch.The following code should therefore be correct with all rules active, however it claims rule violations on the annotated lines.
The text was updated successfully, but these errors were encountered: