-
Notifications
You must be signed in to change notification settings - Fork 287
Type hint a function as abstract, not needing a body #2030
-
Is there a way to hint that a function is abstract and doesn't need a body, like with the @overload decorator? Context: making a type-hinted version of Blinker that looks like this (I'm still on old syntax to support Python 3.9+):
P = ParamSpec('P') T_co = TypeVar('T_co', covariant=True) class TypedSignal(Generic[P, T_co]): def __init__(self, spec: Callable[P, T_co]) -> None: ... def connect(self, handler: Callable[P, T_co]) -> Callable[P, T_co]: ... def send(self, *args: P.args, **kwargs: P.kwargs) -> list[tuple[Callable[P, T_co], T_co]]: ... @TypedSignal def my_signal(sender: SomeType, arg: OtherType) -> ReturnType: ... @my_signal.connect def my_signal_handler(sender: SomeType, arg: OtherType) -> ReturnType: # Insert real implementation
At the moment, Mypy complains my_signal has no return statement so I have to use raise NotImplementedError. I'd like to avoid this boilerplate. Is there a way?
Beta Was this translation helpful? Give feedback.
All reactions
raise NotImplementedError / --disable-error-code empty-body / # type: ignore[empty-body] are your best bet
Replies: 1 comment 1 reply
-
raise NotImplementedError / --disable-error-code empty-body / # type: ignore[empty-body] are your best bet
Beta Was this translation helpful? Give feedback.
All reactions
-
Ooh, looks like there's support for module-level rules now. I guess this means I can drop # mypy: disable-error-code="empty-body" at the top of the file? (Confirmed, yes!)
Beta Was this translation helpful? Give feedback.
All reactions
-
🎉 1