Skip to content

Commit

Permalink
Fix variance inference when using real typeshed stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
JukkaL committed May 16, 2024
1 parent 196b2a3 commit 369a4af
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 3 additions & 3 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1988,9 +1988,9 @@ def is_more_precise(left: Type, right: Type, *, ignore_promotions: bool = False)
return is_proper_subtype(left, right, ignore_promotions=ignore_promotions)


def all_members(info: TypeInfo) -> set[str]:
def all_non_object_members(info: TypeInfo) -> set[str]:
members = set(info.names)
for base in info.mro[1:]:
for base in info.mro[1:-1]:
members.update(base.names)
return members

Expand All @@ -2012,7 +2012,7 @@ def infer_variance(info: TypeInfo, i: int) -> bool:
contra = True
tvar = info.defn.type_vars[i]
self_type = fill_typevars(info)
for member in all_members(info):
for member in all_non_object_members(info):
if member in ("__init__", "__new__"):
continue
node = info[member].node
Expand Down
21 changes: 21 additions & 0 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -2091,3 +2091,24 @@ def f(d: Description) -> None:
reveal_type(d.name_fn)
[out]
_testDataclassStrictOptionalAlwaysSet.py:9: note: Revealed type is "def (Union[builtins.int, None]) -> Union[builtins.str, None]"

[case testPEP695VarianceInference]
# flags: --python-version=3.12 --enable-incomplete-feature=NewGenericSyntax
from typing import Callable, Final

class Job[_R_co]:
def __init__(self, target: Callable[[], _R_co]) -> None:
self.target: Final = target

def func(
action: Job[int | None],
a1: Job[int | None],
a2: Job[int],
a3: Job[None],
) -> None:
action = a1
action = a2
action = a3
a2 = action # Error
[out]
_testPEP695VarianceInference.py:17: error: Incompatible types in assignment (expression has type "Job[None]", variable has type "Job[int]")

0 comments on commit 369a4af

Please sign in to comment.