Movatterモバイル変換


[0]ホーム

URL:


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

Answeredbyhauntsaninja
jace asked this question inQ&A
Discussion options

Is there a way to hint that afunction is abstract and doesn't need a body, like with the@overload decorator? Context: making a type-hinted version ofBlinker 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)classTypedSignal(Generic[P,T_co]):def__init__(self,spec:Callable[P,T_co])->None: ...defconnect(self,handler:Callable[P,T_co])->Callable[P,T_co]: ...defsend(self,*args:P.args,**kwargs:P.kwargs)->list[tuple[Callable[P,T_co],T_co]]: ...@TypedSignaldefmy_signal(sender:SomeType,arg:OtherType)->ReturnType: ...@my_signal.connectdefmy_signal_handler(sender:SomeType,arg:OtherType)->ReturnType:# Insert real implementation

At the moment, Mypy complainsmy_signal has no return statement so I have to useraise 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
@jace
Comment options

Ooh, looks like there'ssupport 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 byjace
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@jace@hauntsaninja

[8]ページ先頭

©2009-2025 Movatter.jp