Replies: 3 comments 4 replies
-
I think |
Beta Was this translation helpful? Give feedback.
-
@erictraut Mark isn't actively working on Pyre anymore, so I can step in for PEP 612 related questions.
This implies that we need to leave out the class _lru_cache_wrapper(Generic[_P, _T]):
def __call__(*args: _P.args, **kwargs: _P.kwargs) -> _T: ... The following code works in Pyre (you'd have to change
If that sounds reasonable, we can probably update the PEP (along with a couple of clarifications that others had brought up). |
Beta Was this translation helpful? Give feedback.
-
There is a similar issue that it appears involves the same problem, in this PR: python/typeshed#6670 The thing is, removing It seems like it'd be cool to have some kind of a descriptor type, that'd handle inserting the |
Beta Was this translation helpful? Give feedback.
-
I'm a contributor to pyright, and I'm hoping to get some clarification about a particular use case involving ParamSpec (introduced in PEP 612) and
__call__
methods.Consider the following code, which executes with no runtime exceptions.
The
functools.cache
decorator is defined in typeshed'sfunctools.pyi
as follows:And
_lru_cache_wrapper
is defined as:When the
@cache
decorator is applied to the methodcached
, the ParamSpec_P
captures the input parameters ofcached
. In other words, it captures the parameter list(self)
. The type of parameterself
in this case is implied to beFoo
or some subclass thereof (or if we adopt the terminology in draft PEP 637, its type isSelf
orSelf@Foo
).The decorated type of
cached
is now_lru_cache_wrapper[(self: Self@Foo), int]
. The__call__
method for this specialized class is effectively now typed as:For the call expression
foo.cached()
in the sample above, it appears that there is a missing argument (the one corresponding to the secondSelf@Foo
parameter). Pyright therefore generates an error in this case, but clearly this isn't an error at runtime.How do we avoid a false positive error in this case?
Have the authors of PEP 612 have considered this situation? If so, what is the solution? I haven't found anything in the PEP that describes the intended behavior.
Perhaps this is just the consequence of the
functools.cache
stub definition, which is attempting to model the runtime behavior but is in effect "lying" about some of the details? In that case, is there a better way to definefunctools.cache
and similar decorators? Maybe through a use of descriptor?Beta Was this translation helpful? Give feedback.
All reactions