Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
2007165
[WIP] Include param_spec in callable types and support reveal_Type
JukkaL Nov 18, 2021
da6d041
[WIP] Simple type inference involving ParamSpec
JukkaL Nov 18, 2021
2506647
[WIP] Work towards using ParamSpec in classes
JukkaL Nov 18, 2021
56971f0
[WIP] Support defining decorators
JukkaL Nov 18, 2021
774a558
Various fixes
JukkaL Nov 19, 2021
cc8fc46
Fixes to mypy daemon (not tested yet)
JukkaL Nov 19, 2021
5b8a1c6
Various updates/fixes to ParamSpecType
JukkaL Nov 19, 2021
bb928ce
Fix to type semantic analysis
JukkaL Nov 19, 2021
f3a2596
Add missing get_proper_type call
JukkaL Nov 19, 2021
ef52db7
Fixes to self check
JukkaL Nov 19, 2021
c76ce08
Fix type check of mypy.constraints
JukkaL Nov 19, 2021
8a69923
Add missing get_proper_type() calls
JukkaL Nov 19, 2021
76ffb14
Move some param spec logic to CallableType
JukkaL Nov 19, 2021
be6f8e8
Fix applying types with ParamSpec
JukkaL Nov 19, 2021
c1686b0
Switch to simpler ParamSpec implementation + cleanup
JukkaL Nov 19, 2021
a3cb0ad
Fix inference against Any
JukkaL Nov 19, 2021
fd124c5
Update test cases
JukkaL Nov 19, 2021
22afc1d
Don't wrap ParamSpec *args and **kwargs types in tuple[...] or dict[...]
JukkaL Nov 19, 2021
044ddfd
Implement basic type checking for ParamSpec types
JukkaL Nov 19, 2021
6d25da4
Use Callable[P, T] in error messages
JukkaL Nov 22, 2021
1047f85
Support subtype checks
JukkaL Nov 22, 2021
a9b410e
Implement ParamSpec join
JukkaL Nov 22, 2021
f621fb7
Fix type erasure
JukkaL Nov 22, 2021
080049e
Add test case
JukkaL Nov 22, 2021
504267c
Disallow ParamSpec type in bad location
JukkaL Nov 22, 2021
2d02b62
Fix type check and lint
JukkaL Nov 22, 2021
dd28b93
Minor tweaks
JukkaL Nov 22, 2021
01ae7d5
Fix TODO in string conversion
JukkaL Nov 22, 2021
b6b09f1
Update docstrings
JukkaL Nov 22, 2021
1af097f
Respond to feedback
JukkaL Nov 23, 2021
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
Prev Previous commit
Next Next commit
Fix applying types with ParamSpec
  • Loading branch information
JukkaL committed Nov 22, 2021
commit be6f8e86651bc652ed46190c1d82113078e42878
10 changes: 9 additions & 1 deletion mypy/applytype.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from mypy.expandtype import expand_type
from mypy.types import (
Type, TypeVarId, TypeVarType, CallableType, AnyType, PartialType, get_proper_types,
TypeVarLikeType, ProperType, ParamSpecType
TypeVarLikeType, ProperType, ParamSpecType, get_proper_type
)
from mypy.nodes import Context

Expand Down Expand Up @@ -89,6 +89,14 @@ def apply_generic_arguments(
if target_type is not None:
id_to_type[tvar.id] = target_type

param_spec = callable.param_spec2()
if param_spec is not None:
nt = id_to_type.get(param_spec.id)
if nt is not None:
nt = get_proper_type(nt)
if isinstance(nt, CallableType):
callable = callable.expand_param_spec(nt)

# Apply arguments to argument types.
arg_types = [expand_type(at, id_to_type) for at in callable.arg_types]

Expand Down
43 changes: 43 additions & 0 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ reveal_type(C(f)) # N: Revealed type is "__main__.C[def (x: builtins.int, y: bu
reveal_type(C(f).m) # N: Revealed type is "def (x: builtins.int, y: builtins.str) -> builtins.int"
[builtins fixtures/dict.pyi]

[case testParamSpecClassWithPrefixArgument]
from typing import Callable, TypeVar, Generic
from typing_extensions import ParamSpec

P = ParamSpec('P')

class C(Generic[P]):
def __init__(self, x: Callable[P, None]) -> None: ...

def m(self, a: str, *args: P.args, **kwargs: P.kwargs) -> int:
return 1

def f(x: int, y: str) -> None: ...

reveal_type(C(f).m) # N: Revealed type is "def (a: builtins.str, x: builtins.int, y: builtins.str) -> builtins.int"
reveal_type(C(f).m('', 1, '')) # N: Revealed type is "builtins.int"
[builtins fixtures/dict.pyi]

[case testParamSpecDecorator]
from typing import Callable, TypeVar, Generic
from typing_extensions import ParamSpec
Expand All @@ -133,4 +151,29 @@ def f(a: int, b: str) -> None: ...
reveal_type(f) # N: Revealed type is "__main__.W[def (a: builtins.int, b: builtins.str), None]"
reveal_type(f(1, '')) # N: Revealed type is "None"
reveal_type(f.x) # N: Revealed type is "builtins.int"

class C:
@dec()
def m(self, x: int) -> str: ...

#reveal_type(C().m(x=1)) # xN: x
[builtins fixtures/dict.pyi]

[case testParamSpecFunction]
from typing import Callable, TypeVar
from typing_extensions import ParamSpec

P = ParamSpec('P')
R = TypeVar('R')

def f(x: Callable[P, R], *args: P.args, **kwargs: P.kwargs) -> R:
return x(*args, **kwargs)

def g(x: int, y: str) -> None: ...

reveal_type(f(g, 1, y='x')) # N: Revealed type is "None"
f(g, 'x', y='x') # E: Argument 2 to "f" has incompatible type "str"; expected "int"
f(g, 1, y=1) # E: Argument "y" to "f" has incompatible type "int"; expected "str"
f(g) # E: Missing positional arguments "x", "y" in call to "f"

[builtins fixtures/dict.pyi]