-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Update dependency pyright to v1.1.359 #11780
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
Hum, I guess this new rule / spec change makes sense on the surface, probably simplifies type-checkers' job. We were unfortunately using TypeVars in |
I'm not sure how we are supposed to type the following case now: def __init__(self: dict[str, _VT], **kwargs: _VT) -> None: ... This looks to be a regression. |
I suppose pyright might like us to do this instead, so that the TypeVar in the constructor method is unambiguously scoped to the function rather than the class: _T = TypeVar("_T")
_KT = TypeVar("_KT")
_VT = TypeVar("_VT")
class dict(Generic[_KT, _VT]):
def __new__(cls, **kwargs: _T) -> dict[str, _T]: ... Unfortunately, however, there's quite a few generic classes that have |
It appears that the new pyright check was added in microsoft/pyright#7691, which was added in response to a change to the typing spec in python/typing#1667. The change to the typing spec was signed off by the typing council in python/typing-council#25. |
Considering that this typing-council change was extensive, and this point in particular did not seem to have been discussed, I'd hold off on regressing on our stubs for now. This seems like an oversight in the new spec. |
Maybe we could just use a different from __future__ import annotations
from typing import Generic, TypeVar
_KT = TypeVar("_KT")
_VT = TypeVar("_VT")
_T = TypeVar("_T")
class Foo(Generic[_KT, _VT]):
def __init__(self: Foo[str, _T], **kwargs: _T) -> None: ...
reveal_type(Foo(x=1, y=2)) # information: Type of "Foo(x=1, y=2)" is "Foo[str, int]" |
In this case, you should use a function-scoped TypeVar rather than a class-scoped TypeVar, as shown in @Akuli's example. Using a class-scoped TypeVar here is problematic in the general case (leads to ambiguities in meaning), which is why it's now disallowed by the typing spec. Mypy doesn't tend to run into these ambiguities because its handling of constructor calls is quite naive; for example, it completely ignores the return type of a The use of a class-scoped TypeVar in a Unfortunately, mypy doesn't currently handle the case where a function-scoped TypeVar is used here. Until it does, I think it makes sense to leave the typeshed stubs unmodified. Once mypy is updated to conform with the typing spec in this area, then I recommend that you switch all of these cases to use function-scoped TypeVars. |
Thanks @erictraut -- I now agree that pyright's behaviour is correct here (and better than mypy's). Unfortunately, as you say, switching to TypeVars that are unambiguously function-scoped won't be possible for us until mypy becomes conformant with the spec. #11802 switched only a few TypeVars in constructor methods to be unambiguously function-scoped, and the mypy_primer output is pretty catastrophic. Would you consider making this new check a dedicated pyright diagnostic? That would enable us to easily bump pyright to the latest version and -- while we wait for mypy to become conformant with the new spec -- switch off this new check in CI for now. Otherwise we may be unable to upgrade to the latest version of pyright in CI for a while. There are so many CI errors that adding |
By the way, one edge case (and it is an edge case -- I don't think it brings into doubt the validity of the new pyright check) that I came up against while working on #11802 was this overload for the constructor of typeshed/stdlib/collections/__init__.pyi Lines 393 to 394 in b3bfbef
I don't know how to type this overload according to the new rules. I think what I'd want is the following behaviour: d = defaultdict(list[int]) # `d` inferred as `defaultdict[Unknown, list[int]]`
d: defaultdict[str, list[int]] = defaultdict(list) # `d` inferred as `defaultdict[str, list[int]]` But I don't know how to get type checkers to infer that if I can only use function-scoped TypeVars. |
Shouldn't we be able to # |
We can, but it's just a lot of |
Sorry, but I can't justify making a separate diagnostic rule just for this error. The bar is high for adding a new diagnostic rule. It's something we need to document and maintain forever. I could move it from Out of curiosity, in how many locations does this appear in typeshed currently? Is it just a few or hundreds? If it's just a few, then |
I think that would be better, yes.
There are 43. |
Understandable.
That makes sense to me, even if we end up going the |
I'm not sure how many it is (I'm trying to find out now), but it's more than 43, because the pyright 3.8 job only reports type errors on branches that exist on Python 3.8, and the pyright 3.10 job only reports type errors on branches that exist on Python 3.10, etc. For typeshed/stdlib/contextlib.pyi Lines 180 to 200 in 7858e60
|
…nnotation in `__init__`. It was previously reported under `reportGeneralTypeIssues`, but it's now moved to `reportInvalidTypeVarUse`. This was done to help typeshed maintainers migrate away from this pattern. python/typeshed#11780 (comment)
OK, I've moved this check from |
Thanks very much!
We use the "strict" change for most of our stdlib stubs and many of our third-party stubs. #11810 is what it looks like if we just |
This PR contains the following updates:
==1.1.358
->==1.1.359
Release Notes
RobertCraigie/pyright-python (pyright)
v1.1.359
Compare Source
Configuration
📅 Schedule: Branch creation - "before 4am" (UTC), Automerge - At any time (no schedule defined).
🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.
♻ Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.
🔕 Ignore: Close this PR and you won't be reminded about this update again.
This PR has been generated by Mend Renovate. View repository job log here.