-
Notifications
You must be signed in to change notification settings - Fork 768
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
False Positive for covariant
when using T | None
#4613
Comments
Could a pylance team member please transfer to the pyright project? This is a core type checker issue. |
Here's a simplified repro: from typing import TypeVar, Protocol
T = TypeVar("T")
T_co = TypeVar("T_co", covariant=True)
class FalsePositive(Protocol[T]):
var: T | None
class Positive(Protocol[T_co]):
var: T_co | None And here's an even simpler repro that demonstrates the underlying problem: from typing import TypeVar
T = TypeVar("T")
def func(x: list[T | None]) -> T | None:
y: list[object | None] = x # This should generate an error but does not
return x[0] |
…one` type to `object | None` in an invariant context. This same bug led to issues with the validation of TypeVar variance within a Protocol. This addresses microsoft/pylance-release#4613.
…one` type to `object | None` in an invariant context. This same bug led to issues with the validation of TypeVar variance within a Protocol. This addresses microsoft/pylance-release#4613. (#5518) Co-authored-by: Eric Traut <erictr@microsoft.com>
Thanks for the bug report. This will be fixed in the next release of pyright and a future release of pylance. |
Thank you, do I close it now? |
Please leave it open. The pylance team will close it once it's in a released version of pylance. |
Environment data
Code Snippet
Expected behavior
Only the class
Positive
to give a warning thatType variable "T_co" used in generic protocol "Positive" should be invariant
, just like mypy.Actual behavior
Opposite is true, class
Positive
does not give a warning. ClassFalsePositive
gives a warning thatType variable "T" used in generic protocol "FalsePositive" should be covariant
.Logs
The text was updated successfully, but these errors were encountered: