Skip to content

Commit

Permalink
[daemon] Fix return type change to optional in generic function (#16342)
Browse files Browse the repository at this point in the history
Previously changing a return type to an optional type was not propagated
at least in some cases, since astdiff could simplify away the optional
type.
  • Loading branch information
JukkaL authored Oct 27, 2023
1 parent f7d047c commit 5ef9c82
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mypy/server/astdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class level -- these are handled at attribute level (say, 'mod.Cls.method'
Var,
)
from mypy.semanal_shared import find_dataclass_transform_spec
from mypy.state import state
from mypy.types import (
AnyType,
CallableType,
Expand Down Expand Up @@ -456,7 +457,8 @@ def normalize_callable_variables(self, typ: CallableType) -> CallableType:
tv = v.copy_modified(id=tid)
tvs.append(tv)
tvmap[v.id] = tv
return expand_type(typ, tvmap).copy_modified(variables=tvs)
with state.strict_optional_set(True):
return expand_type(typ, tvmap).copy_modified(variables=tvs)

def visit_tuple_type(self, typ: TupleType) -> SnapshotItem:
return ("TupleType", snapshot_types(typ.items))
Expand Down
33 changes: 33 additions & 0 deletions test-data/unit/diff.test
Original file line number Diff line number Diff line change
Expand Up @@ -1497,3 +1497,36 @@ class C:
def meth(self) -> int: return 0
[out]
__main__.C.meth

[case testGenericFunctionWithOptionalReturnType]
from typing import Type, TypeVar

T = TypeVar("T")

class C:
@classmethod
def get_by_team_and_id(
cls: Type[T],
raw_member_id: int,
include_removed: bool = False,
) -> T:
pass

[file next.py]
from typing import Type, TypeVar, Optional

T = TypeVar("T")

class C:
@classmethod
def get_by_team_and_id(
cls: Type[T],
raw_member_id: int,
include_removed: bool = False,
) -> Optional[T]:
pass

[builtins fixtures/classmethod.pyi]
[out]
__main__.C.get_by_team_and_id
__main__.Optional

0 comments on commit 5ef9c82

Please sign in to comment.