Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Type hint a function as abstract, not needing a body #2030

Answered by hauntsaninja
jace asked this question in Q&A
Discussion options

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?

You must be logged in to vote

raise NotImplementedError / --disable-error-code empty-body / # type: ignore[empty-body] are your best bet

Replies: 1 comment 1 reply

Comment options

raise NotImplementedError / --disable-error-code empty-body / # type: ignore[empty-body] are your best bet

You must be logged in to vote
1 reply
Comment options

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!)

Answer selected by jace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /