-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Regression in type inference with constructors with type arguments #5369
Comments
I was too hasty in following the bug. This is a minimization of my actual problem, which involves a decorator that expects a factory method to which I had been passing class names: def publishes_events(
event_factory: Callable[
Concatenate[MethodCallEventType, Publisher, Output | None, Exception | None, Params],
MethodCallEventT,
],
) -> SyncOrAsyncMethod[Publisher, Params, Output]:
...
class SyncOrAsyncMethod(Protocol[Slf, Params, Return]):
@overload
def __call__(self, func: Callable[Concatenate[Slf, Params], Return]) -> Callable[Concatenate[Slf, Params], Return]:
...
@overload
def __call__(
self,
func: Callable[Concatenate[Slf, Params], Awaitable[Return]],
) -> Callable[Concatenate[Slf, Params], Awaitable[Return]]:
... As of 1.1.313, passing a bare classname (like However, it appears that the code I have above never type-checked, at least not for 1.1.312 <= <= 1.1.315. Not sure what's going on here. |
Could you please provide a minimal, self-contained code sample? The sample above references a bunch of types that are not defined within the sample. I could guess at how they are defined, but it would be better if you could provide them. I presume that some of the identifiers refer to type variables, others refer to classes. |
As for your top example, I can't explain why the runtime is generating an exception here. This strikes me as a runtime bug. If I remove the from dataclasses import dataclass
from typing import Generic, TypeVar
T = TypeVar("T")
@dataclass(frozen=True, slots=True)
class Test(Generic[T]):
x: T
Test[int](3) Here's the output:
|
I will try (again) to minimize. Do you believe the very first (self-contained) example should fail to typecheck, even though the type can be inferred? |
Yes, I agree that the top example should type check. It does with other generic functions, like this: def func(x: G) -> Test[G]: ...
y: Callable[[int], Test[int]] = func So it should work consistently when applied to a constructor call. I'll investigate further. Is your second sample the same problem? If so, then it's unnecessary for you to provide an updated version. If you think it's different from the first problem, then I'd appreciate a complete sample so I can repro it. |
… assign a class to a `Callable` type when the class is generic and the constructor includes class-scoped type variables in its parameter annotations. This addresses #5369.
… assign a class to a `Callable` type when the class is generic and the constructor includes class-scoped type variables in its parameter annotations. This addresses #5369. (#5372) Co-authored-by: Eric Traut <[email protected]>
For the runtime error, I've encountered a similar error with python 3.11 a few months ago, and there is a related issue in the bug tracker python/cpython#90562 |
I'm not sure if the original issue and my issue are the same. There must be something else going on since the regression happened with a change in behavior for the issue you fixed, but it may be that some other change I have not identified unmasked this issue. I will investigate more after the next release and file a new issue if it still exists and I can minimize. As always, thank you for the quick responses. |
This is addressed in pyright 1.1.316, which I just published. It will also be included in this week's insiders build of pylance. |
Describe the bug
The following code no longer typechecks as of 1.1.315:
The error is
The code runs fine. The following code typechecks:
but fails at runtime with
Expected behavior
I believe the old pyright behavior was correct given the runtime behavior.
VS Code extension or command-line
Command-line version 1.1.315.
The text was updated successfully, but these errors were encountered: