Skip to content

MyPy disallow untyped decorators #5823

@f-schnabel

Description

@f-schnabel

Is your feature request related to a problem? Please describe.
There are some decorators that are untyped e.g. deprecated and deprecated_arg.
See also here: https://github.com/Project-MONAI/MONAI/blob/dev/monai/utils/deprecate_utils.py
These untyped decorators break the type checking of MyPy, so any function annotated with these decorators is not type checked at all.

Describe the solution you'd like
Add a type signature to these decorators, so the decorated functions are type checked.
First Proposal:

T = TypeVar('T', type, Callable)
def deprecated(
    since: Optional[str] = None,
    removed: Optional[str] = None,
    msg_suffix: str = "",
    version_val: str = __version__,
    warning_category=FutureWarning,
) -> Callable[[T], T]:
...

Another idea would be to set the return type to Callable[[T], Union[T, NoReturn]] since in some circumstances the decorator throws an exception and doesn't even reach the decorated code.
Also, enable disallow_untyped_decorators = True inside setup.cfg so we can be certain that all decorators are typed.

Additional context
MyPy documentation: https://mypy.readthedocs.io/en/stable/generics.html?highlight=decorator#declaring-decorators

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions