Skip to content

Fix daemon crashes related to ParamSpec and TypeVarTuple #13381

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

Merged
merged 3 commits into from
Aug 23, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions mypy/fixup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
FuncDef,
MypyFile,
OverloadedFuncDef,
ParamSpecExpr,
SymbolTable,
TypeAlias,
TypeInfo,
TypeVarExpr,
TypeVarTupleExpr,
Var,
)
from mypy.types import (
Expand Down Expand Up @@ -164,6 +166,12 @@ def visit_type_var_expr(self, tv: TypeVarExpr) -> None:
value.accept(self.type_fixer)
tv.upper_bound.accept(self.type_fixer)

def visit_paramspec_expr(self, p: ParamSpecExpr) -> None:
p.upper_bound.accept(self.type_fixer)

def visit_type_var_tuple_expr(self, tv: TypeVarTupleExpr) -> None:
tv.upper_bound.accept(self.type_fixer)

def visit_var(self, v: Var) -> None:
if self.current_info is not None:
v.info = self.current_info
Expand Down
3 changes: 3 additions & 0 deletions mypy/server/astdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class level -- these are handled at attribute level (say, 'mod.Cls.method'
TypeAlias,
TypeInfo,
TypeVarExpr,
TypeVarTupleExpr,
Var,
)
from mypy.types import (
Expand Down Expand Up @@ -189,6 +190,8 @@ def snapshot_symbol_table(name_prefix: str, table: SymbolTable) -> dict[str, Sna
)
elif isinstance(node, ParamSpecExpr):
result[name] = ("ParamSpec", node.variance, snapshot_type(node.upper_bound))
elif isinstance(node, TypeVarTupleExpr):
result[name] = ("TypeVarTuple", node.variance, snapshot_type(node.upper_bound))
else:
assert symbol.kind != UNBOUND_IMPORTED
if node and get_prefix(node.fullname) != name_prefix:
Expand Down
1 change: 1 addition & 0 deletions mypy/test/testfinegrained.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def get_options(self, source: str, testcase: DataDrivenTestCase, build_cache: bo
options.use_fine_grained_cache = self.use_cache and not build_cache
options.cache_fine_grained = self.use_cache
options.local_partial_types = True
options.enable_incomplete_features = True
if re.search("flags:.*--follow-imports", source) is None:
# Override the default for follow_imports
options.follow_imports = "error"
Expand Down
83 changes: 83 additions & 0 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -9819,6 +9819,89 @@ x: str
[out]
==

[case testParamSpecCached]
import a

[file a.py]
import b

def f(x: int) -> str: return 'x'

b.foo(f)

[file a.py.2]
import b

def f(x: int) -> str: return 'x'

reveal_type(b.foo(f))

[file b.py]
from typing import TypeVar, Callable, Union
from typing_extensions import ParamSpec

P = ParamSpec("P")
T = TypeVar("T")

def foo(f: Callable[P, T]) -> Callable[P, Union[T, None]]:
return f

[file b.py.2]
from typing import TypeVar, Callable, Union
from typing_extensions import ParamSpec

P = ParamSpec("P")
T = TypeVar("T")

def foo(f: Callable[P, T]) -> Callable[P, Union[T, None]]:
return f

x = 0 # Arbitrary change to trigger reprocessing

[builtins fixtures/dict.pyi]
[out]
==
a.py:5: note: Revealed type is "def (x: builtins.int) -> builtins.str"

[case testTypeVarTupleCached]
import a

[file a.py]
import b

def f(x: int) -> str: return 'x'

b.foo((1, 'x'))

[file a.py.2]
import b

reveal_type(b.foo((1, 'x')))

[file b.py]
from typing import Tuple
from typing_extensions import TypeVarTuple, Unpack

Ts = TypeVarTuple("Ts")

def foo(t: Tuple[Unpack[Ts]]) -> Tuple[Unpack[Ts]]:
return t

[file b.py.2]
from typing import Tuple
from typing_extensions import TypeVarTuple, Unpack

Ts = TypeVarTuple("Ts")

def foo(t: Tuple[Unpack[Ts]]) -> Tuple[Unpack[Ts]]:
return t

x = 0 # Arbitrary change to trigger reprocessing
[builtins fixtures/dict.pyi]
[out]
==
a.py:3: note: Revealed type is "Tuple[Literal[1]?, Literal['x']?]"

[case testUnpackKwargsUpdateFine]
# flags: --enable-incomplete-features
import m
Expand Down