10000 Basic ParamSpec Concatenate and literal support by A5rocks · Pull Request #11847 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Basic ParamSpec Concatenate and literal support #11847

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 50 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
ef32680
Add ParamSpec literals
A5rocks Dec 24, 2021
816f3cd
Improve ParamSpec and Parameters checking
A5rocks Dec 25, 2021
d9b352f
Get basic Concatenate features working
A5rocks Dec 26, 2021
58e6dbe
Fix "cache" bug
A5rocks Dec 26, 2021
51ba4ea
Check Concatenate prefixes
A5rocks Dec 26, 2021
24432ee
Polish work
A5rocks Dec 26, 2021
c507152
Merge branch 'master' into paramspec-literals
A5rocks Dec 26, 2021
d202d1e
Tests for literals
A5rocks Dec 26, 2021
9ed9830
Tests for Concatenate
A5rocks Dec 26, 2021
3ffc343
Appease CI
A5rocks Dec 26, 2021 8000
ae8ac73
Forgot to comment out the directives...
A5rocks Dec 26, 2021
9c849cc
Improve literal TODOs
A5rocks Dec 27, 2021
d9dcc76
Add more tests
A5rocks Dec 28, 2021
0e2b207
Allow TypeVars in Concatenate
A5rocks Dec 28, 2021
bd445e5
Fix a couple of dumb oversights
A5rocks Dec 28, 2021
604c304
Allow Callables along with Parameters
A5rocks Dec 28, 2021
f8004ec
Fix tests
A5rocks Dec 28, 2021
9e75481
Misc changes
A5rocks Dec 29, 2021
7b89f06
Solve with self types
A5rocks Jan 1, 2022
f24cf4f
Add fallback return to meeting paramspec literals
A5rocks Jan 1, 2022
472b20c
Type application of ParamSpec literals
A5rocks Jan 3, 2022
14ecfb9
Ellipsis paramspec literals
A5rocks Jan 3, 2022
5e0ae49
Merge branch 'master' into paramspec-literals
A5rocks Jan 3, 2022
45c8057
Appease flake8
A5rocks Jan 3, 2022
10966ea
Merge branch 'master' into paramspec-literals
hauntsaninja Jan 7, 2022
c46feec
Minor code cleanup
A5rocks Jan 9, 2022
6a9cd71
Error notes and better subtyping for paramspec literals
A5rocks Jan 9, 2022
afc1a57
Appease CI
A5rocks Jan 9, 2022
41e38b2
Merge remote-tracking branch 'upstream/master' into paramspec-literals
A5rocks Jan 19, 2022
86e23c2
Fix something I assumed incorrectly
A5rocks Jan 27, 2022
3f4cf5c
Merge branch 'master' into paramspec-literals
A5rocks Jan 27, 2022
61b00cd
Revert "Minor code cleanup"
A5rocks Jan 29, 2022
9c2cefd
Merge branch 'master' into paramspec-literals
A5rocks Mar 1, 2022
a44937b
Fixed raised bugs
A5rocks Mar 1, 2022
bbabbf1
Fix CI errors
A5rocks Mar 1, 2022
ddfd34a
Squash some more bugs
A5rocks Mar 5, 2022
2d54ac4
Concatenate flag
A5rocks Mar 5, 2022
bba91e5
Prepare for GitHub Actions
A5rocks Mar 5, 2022
e0a7663
Merge branch 'master' into paramspec-literals
A5rocks Mar 7, 2022
0363803
Bug report with nested decorators and Concatenate
A5rocks Mar 7, 2022
278b8c4
Switch over to using Parameters instead of CallableType
A5rocks Mar 7, 2022
c2b7628
Add variance to paramspecs
A5rocks Mar 7, 2022
0b1fdfb
Apply suggestions from code review
A5rocks Mar 10, 2022
0fff609
Update tests
A5rocks Mar 10, 2022
4475515
Some of the PR feedback
A5rocks Mar 25, 2022
0091762
Merge branch 'master' into paramspec-literals
A5rocks Mar 26, 2022
81994f1
Prepare for GitHub actions
A5rocks Mar 26, 2022
1ff96c1
Merge branch 'master' into paramspec-literals
A5rocks Apr 5, 2022
c79918e
Fix tests to latest output
A5rocks Apr 5, 2022
9b1fc75
Copy pyright's representation of Concatenate
A5rocks Apr 5, 2022
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
Allow TypeVars in Concatenate
  • Loading branch information
A5rocks committed Dec 28, 2021
commit 0e2b207b19bebe7a76f84baa015d133a8558256d
15 changes: 12 additions & 3 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def visit_instance(self, template: Instance) -> List[Constraint]:
if isinstance(suffix, Parameters):
# no such thing as variance for ParamSpecs
# TODO: is there a case I am missing?
# TODO: what is setting meta_level to 0?
# TODO: constraints between prefixes
prefix = mapped_arg.prefix
suffix = suffix.copy_modified(suffix.arg_types[len(prefix.arg_types):],
suffix.arg_kinds[len(prefix.arg_kinds):],
Expand Down Expand Up @@ -490,7 +490,7 @@ def visit_instance(self, template: Instance) -> List[Constraint]:
if isinstance(suffix, Parameters):
# no such thing as variance for ParamSpecs
# TODO: is there a case I am missing?
# TODO: what is setting meta_level to 0?
# TODO: constraints between prefixes
prefix = template_arg.prefix
suffix = suffix.copy_modified(suffix.arg_types[len(prefix.arg_types):],
suffix.arg_kinds[len(prefix.arg_kinds):],
Expand Down Expand Up @@ -586,7 +586,6 @@ def visit_callable_type(self, template: CallableType) -> List[Constraint]:
res.extend(infer_constraints(t, a, neg_op(self.direction)))
else:
# TODO: Direction
# TODO: Deal with arguments that come before param spec ones?
# TODO: check the prefixes match
prefix = param_spec.prefix
prefix_len = len(prefix.arg_types)
Expand All @@ -597,6 +596,16 @@ def visit_callable_type(self, template: CallableType) -> List[Constraint]:
arg_kinds=cactual.arg_kinds[prefix_len:],
arg_names=cactual.arg_names[prefix_len:],
ret_type=NoneType())))
# compare prefixes
cactual_prefix = cactual.copy_modified(
arg_types=cactual.arg_types[:prefix_len],
arg_kinds=cactual.arg_kinds[:prefix_len],
arg_names=cactual.arg_names[:prefix_len])

# 8000 TODO: see above "FIX" comments for param_spec is None case
# TODO: this assume positional arguments
for t, a in zip(prefix.arg_types, cactual_prefix.arg_types):
res.extend(infer_constraints(t, a, neg_op(self.direction)))

template_ret_type, cactual_ret_type = template.ret_type, cactual.ret_type
if template.type_guard is not None:
Expand Down
13 changes: 8 additions & 5 deletions mypy/expandtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,14 @@ def visit_callable_type(self, t: CallableType) -> Type:
# the replacement is ignored.
if isinstance(repl, CallableType):
# Substitute *args: P.args, **kwargs: P.kwargs
t = t.expand_param_spec(repl)
# TODO: Substitute remaining arg types
return t.copy_modified(ret_type=t.ret_type.accept(self),
type_guard=(t.type_guard.accept(self)
if t.type_guard is not None else None))
prefix = param_spec.prefix
t = t.expand_param_spec(repl, no_prefix=True)
return t.copy_modified(
arg_types=self.expand_types(prefix.arg_types) + t.arg_types,
arg_kinds=prefix.arg_kinds + t.arg_kinds,
arg_names=prefix.arg_names + t.arg_names,
ret_type=t.ret_type.accept(self),
type_guard=(t.type_guard.accept(self) if t.type_guard is not None else None))

return t.copy_modified(arg_types=self.expand_types(t.arg_types),
ret_type=t.ret_type.accept(self),
Expand Down
4 changes: 3 additions & 1 deletion mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,10 +847,12 @@ def analyze_callable_args_for_concatenate(
obj = self.named_type('builtins.object')
# ick, CallableType should take ParamSpecType
prefix = tvar_def.prefix
# we don't set the prefix here as generic arguments will get updated at some point
# in the future. CallableType.param_spec() accounts for this.
return CallableType(
[*prefix.arg_types,
ParamSpecType(tvar_def.name, tvar_def.fullname, tvar_def.id, ParamSpecFlavor.ARGS,
upper_bound=obj, prefix=tvar_def.prefix),
upper_bound=obj),
ParamSpecType(tvar_def.name, tvar_def.fullname, tvar_def.id, ParamSpecFlavor.KWARGS,
upper_bound=obj)],
[*prefix.arg_kinds, nodes.ARG_STAR, nodes.ARG_STAR2],
Expand Down
13 changes: 9 additions & 4 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,10 +1520,15 @@ def param_spec(self) -> Optional[ParamSpecType]:
return ParamSpecType(arg_type.name, arg_type.fullname, arg_type.id, ParamSpecFlavor.BARE,
arg_type.upper_bound, prefix=prefix)

def expand_param_spec(self, c: 'CallableType') -> 'CallableType':
return self.copy_modified(arg_types=self.arg_types[:-2] + c.arg_types,
arg_kinds=self.arg_kinds[:-2] + c.arg_kinds,
arg_names=self.arg_names[:-2] + c.arg_names)
def expand_param_spec(self, c: 'CallableType', no_prefix: bool = False) -> 'CallableType':
if no_prefix:
return self.copy_modified(arg_types=c.arg_types,
arg_kinds=c.arg_kinds,
arg_names=c.arg_names)
else:
return self.copy_modified(arg_types=self.arg_types[:-2] + c.arg_types,
arg_kinds=self.arg_kinds[:-2] + c.arg_kinds,
arg_names=self.arg_names[:-2] + c.arg_names)

def __hash__(self) -> int:
return hash((self.ret_type, self.is_type_obj(),
Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,21 @@ def f2(c: Callable[P, R]) -> Callable[Concatenate[int, P], R]:
# reason for rejection:
f2(lambda x: 42)(42, x=42)
[builtins fixtures/tuple.pyi]

[case testParamSpecConcatenateWithTypeVar]
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, TypeVar

P = ParamSpec("P")
R = TypeVar("R")
S = TypeVar("S")

def f(c: Callable[Concatenate[S, P], R]) -> Callable[Concatenate[S, P], R]: ...

def a(n: int) -> None: ...

n = f(a)

reveal_type(n) # N: Revealed type is "def (builtins.int*)"
reveal_type(n(42)) # N: Revealed type is "None"
[builtins fixtures/tuple.pyi]
0