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.
-
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.Callable
and another is viatyping.Protocol
. According to PEP-544:There are two ways of writing
Protocol
for a decorator:PDecorator
which 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
Callable
itself is already very different from any other generic type, say:Related question
It seems pretty counterintuitive that
Callable
type alias is handled like any other generic type alias, having their type vars replaced withUnknown
/Any
. This behavior is not applied toCallable
itself (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
Callable
orCallable
-based type alias used in type annotation has an unbound type variable (maybe it should be used only in thisCallable
or alias) - assume that this type variable is call-scoped instead of beingUnknown
/Any
.This will make
TDecorator
equivalent toPDecorator
.Related discussions:
#1134
microsoft/pyright#3803
Beta Was this translation helpful? Give feedback.
All reactions