Skip to content
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

[daemon] Fix return type change to optional in generic function #16342

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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