Replies: 1 comment 2 replies
-
|
Sorry about having to wait for an answer so long :) You seem to be confused about what This means that
The tl;dr of this is:
I would argue that this is not a bug in either type checker. They both have a well-defined approach and they work consistently in their own ways. Both approaches usually solve |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The context
I wanted to type hint a lot of decorator factories. So, I've started with writing a type for a decorator.
There are two approaches to do so - one is via
typing.Callableand another is viatyping.Protocol. According to PEP-544:There are two ways of writing
Protocolfor a decorator:PDecoratorwhich has type variables in its__call__methodPGenericDecorator[TCallable]which has a class-scope type variableThere is a code snippet with all the approaches (in mypy 0.971 and pyright running via Pylance v2022.8.10):
So it seems that the only working both in mypy and pyright solution is non-generic protocol.
I'd also like to note that
Callableitself is already very different from any other generic type, say:Related question
It seems pretty counterintuitive that
Callabletype alias is handled like any other generic type alias, having their type vars replaced withUnknown/Any. This behavior is not applied toCallableitself (as in snippet with-> Callable[[T], T]) and seem to be almost never desired.Is this a bug or this behavior is actually intended?
One possible solution
If
CallableorCallable-based type alias used in type annotation has an unbound type variable (maybe it should be used only in thisCallableor alias) - assume that this type variable is call-scoped instead of beingUnknown/Any.This will make
TDecoratorequivalent toPDecorator.Related discussions:
#1134
microsoft/pyright#3803
Beta Was this translation helpful? Give feedback.
All reactions