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

Basic ParamSpec Concatenate and literal support #11847

Merged
merged 50 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
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
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
51 changes: 46 additions & 5 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
TupleType, TypedDictType, UnionType, Overloaded, ErasedType, PartialType, DeletedType,
UninhabitedType, TypeType, TypeVarId, TypeQuery, is_named_instance, TypeOfAny, LiteralType,
ProperType, ParamSpecType, get_proper_type, TypeAliasType, is_union_with_any,
callable_with_ellipsis
callable_with_ellipsis, Parameters
)
from mypy.maptype import map_instance_to_supertype
import mypy.subtypes
Expand Down Expand Up @@ -403,6 +403,9 @@ def visit_param_spec(self, template: ParamSpecType) -> List[Constraint]:
# Can't infer ParamSpecs from component values (only via Callable[P, T]).
return []

def visit_parameters(self, template: Parameters) -> List[Constraint]:
raise RuntimeError("Parameters cannot be constrained to")

# Non-leaf types

def visit_instance(self, template: Instance) -> List[Constraint]:
Expand Down Expand Up @@ -443,7 +446,7 @@ def visit_instance(self, template: Instance) -> List[Constraint]:
# N.B: We use zip instead of indexing because the lengths might have
# mismatches during daemon reprocessing.
for tvar, mapped_arg, instance_arg in zip(tvars, mapped.args, instance.args):
# TODO: ParamSpecType
# TODO(PEP612): More ParamSpec work (or is Parameters the only thing accepted)
if isinstance(tvar, TypeVarType):
# The constraints for generic type parameters depend on variance.
# Include constraints from both directions if invariant.
Expand All @@ -453,6 +456,17 @@ def visit_instance(self, template: Instance) -> List[Constraint]:
if tvar.variance != COVARIANT:
res.extend(infer_constraints(
mapped_arg, instance_arg, neg_op(self.direction)))
elif isinstance(tvar, ParamSpecType) and isinstance(mapped_arg, ParamSpecType):
suffix = get_proper_type(instance_arg)
if isinstance(suffix, Parameters) or isinstance(suffix, CallableType):
# no such thing as variance for ParamSpecs
# TODO: is there a case I am missing?
# 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):],
suffix.arg_names[len(prefix.arg_names):])
res.append(Constraint(mapped_arg.id, SUPERTYPE_OF, suffix))
return res
elif (self.direction == SUPERTYPE_OF and
instance.type.has_base(template.type.fullname)):
Expand All @@ -461,7 +475,6 @@ def visit_instance(self, template: Instance) -> List[Constraint]:
# N.B: We use zip instead of indexing because the lengths might have
# mismatches during daemon reprocessing.
for tvar, mapped_arg, template_arg in zip(tvars, mapped.args, template.args):
# TODO: ParamSpecType
if isinstance(tvar, TypeVarType):
# The constraints for generic type parameters depend on variance.
# Include constraints from both directions if invariant.
Expand All @@ -471,6 +484,18 @@ def visit_instance(self, template: Instance) -> List[Constraint]:
if tvar.variance != COVARIANT:
res.extend(infer_constraints(
template_arg, mapped_arg, neg_op(self.direction)))
elif (isinstance(tvar, ParamSpecType) and
isinstance(template_arg, ParamSpecType)):
suffix = get_proper_type(mapped_arg)
if isinstance(suffix, Parameters) or isinstance(suffix, CallableType):
# no such thing as variance for ParamSpecs
# TODO: is there a case I am missing?
# 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):],
suffix.arg_names[len(prefix.arg_names):])
res.append(Constraint(template_arg.id, SUPERTYPE_OF, suffix))
return res
if (template.type.is_protocol and self.direction == SUPERTYPE_OF and
# We avoid infinite recursion for structural subtypes by checking
Expand Down Expand Up @@ -561,10 +586,26 @@ 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)
res.append(Constraint(param_spec.id,
SUBTYPE_OF,
cactual.copy_modified(ret_type=NoneType())))
cactual.copy_modified(
arg_types=cactual.arg_types[prefix_len:],
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])

# 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
5 changes: 4 additions & 1 deletion mypy/erasetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Type, TypeVisitor, UnboundType, AnyType, NoneType, TypeVarId, Instance, TypeVarType,
CallableType, TupleType, TypedDictType, UnionType, Overloaded, ErasedType, PartialType,
DeletedType, TypeTranslator, UninhabitedType, TypeType, TypeOfAny, LiteralType, ProperType,
get_proper_type, TypeAliasType, ParamSpecType
get_proper_type, TypeAliasType, ParamSpecType, Parameters
)
from mypy.nodes import ARG_STAR, ARG_STAR2

Expand Down Expand Up @@ -60,6 +60,9 @@ def visit_type_var(self, t: TypeVarType) -> ProperType:
def visit_param_spec(self, t: ParamSpecType) -> ProperType:
return AnyType(TypeOfAny.special_form)

def visit_parameters(self, t: Parameters) -> ProperType:
raise RuntimeError("Parameters should have been bound to a class")

def visit_callable_type(self, t: CallableType) -> ProperType:
# We must preserve the fallback type for overload resolution to work.
any_type = AnyType(TypeOfAny.special_form)
Expand Down
31 changes: 24 additions & 7 deletions mypy/expandtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
NoneType, Overloaded, TupleType, TypedDictType, UnionType,
ErasedType, PartialType, DeletedType, UninhabitedType, TypeType, TypeVarId,
FunctionLike, TypeVarType, LiteralType, get_proper_type, ProperType,
TypeAliasType, ParamSpecType, TypeVarLikeType
TypeAliasType, ParamSpecType, TypeVarLikeType, Parameters
)


Expand Down Expand Up @@ -104,13 +104,25 @@ def visit_param_spec(self, t: ParamSpecType) -> Type:
if isinstance(repl, Instance):
inst = repl
# Return copy of instance with type erasure flag on.
# TODO: what does prefix mean in this case?
# TODO: why does this case even happen? Instances aren't plural.
return Instance(inst.type, inst.args, line=inst.line,
column=inst.column, erased=True)
elif isinstance(repl, ParamSpecType):
return repl.with_flavor(t.flavor)
# TODO: what if both have prefixes???
# (realistically, `repl` is the unification variable for `t` so this is fine)
return repl.copy_modified(flavor=t.flavor, prefix=t.prefix)
elif isinstance(repl, Parameters) or isinstance(repl, CallableType):
return repl.copy_modified(t.prefix.arg_types + repl.arg_types,
t.prefix.arg_kinds + repl.arg_kinds,
t.prefix.arg_names + repl.arg_names)
else:
# TODO: should this branch be removed? better not to fail silently
return repl

def visit_parameters(self, t: Parameters) -> Type:
return t.copy_modified(arg_types=self.expand_types(t.arg_types))

def visit_callable_type(self, t: CallableType) -> Type:
param_spec = t.param_spec()
if param_spec is not None:
Expand All @@ -124,11 +136,16 @@ 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
# we need to expand the types in the prefix, so might as well
# not get them in the first place
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
8 changes: 7 additions & 1 deletion mypy/fixup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from mypy.types import (
CallableType, Instance, Overloaded, TupleType, TypedDictType,
TypeVarType, UnboundType, UnionType, TypeVisitor, LiteralType,
TypeType, NOT_READY, TypeAliasType, AnyType, TypeOfAny, ParamSpecType
TypeType, NOT_READY, TypeAliasType, AnyType, TypeOfAny, ParamSpecType,
Parameters
)
from mypy.visitor import NodeVisitor
from mypy.lookup import lookup_fully_qualified
Expand Down Expand Up @@ -251,6 +252,11 @@ def visit_type_var(self, tvt: TypeVarType) -> None:
def visit_param_spec(self, p: ParamSpecType) -> None:
p.upper_bound.accept(self)

def visit_parameters(self, p: Parameters) -> None:
for argt in p.arg_types:
if argt is not None:
argt.accept(self)

def visit_unbound_type(self, o: UnboundType) -> None:
for a in o.args:
a.accept(self)
Expand Down
3 changes: 3 additions & 0 deletions mypy/indirection.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def visit_type_var(self, t: types.TypeVarType) -> Set[str]:
def visit_param_spec(self, t: types.ParamSpecType) -> Set[str]:
return set()

def visit_parameters(self, t: types.Parameters) -> Set[str]:
return self._visit(t.arg_types)

def visit_instance(self, t: types.Instance) -> Set[str]:
out = self._visit(t.args)
if t.type:
Expand Down
5 changes: 4 additions & 1 deletion mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Type, AnyType, NoneType, TypeVisitor, Instance, UnboundType, TypeVarType, CallableType,
TupleType, TypedDictType, ErasedType, UnionType, FunctionLike, Overloaded, LiteralType,
PartialType, DeletedType, UninhabitedType, TypeType, TypeOfAny, get_proper_type,
ProperType, get_proper_types, TypeAliasType, PlaceholderType, ParamSpecType
ProperType, get_proper_types, TypeAliasType, PlaceholderType, ParamSpecType, Parameters
)
from mypy.maptype import map_instance_to_supertype
from mypy.subtypes import (
Expand Down Expand Up @@ -256,6 +256,9 @@ def visit_param_spec(self, t: ParamSpecType) -> ProperType:
return t
return self.default(self.s)

def visit_parameters(self, t: Parameters) -> ProperType:
raise NotImplementedError("joining two paramspec literals is not supported yet")

def visit_instance(self, t: Instance) -> ProperType:
if isinstance(self.s, Instance):
if self.instance_joiner is None:
Expand Down
5 changes: 4 additions & 1 deletion mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
TupleType, TypedDictType, ErasedType, UnionType, PartialType, DeletedType,
UninhabitedType, TypeType, TypeOfAny, Overloaded, FunctionLike, LiteralType,
ProperType, get_proper_type, get_proper_types, TypeAliasType, TypeGuardedType,
ParamSpecType
ParamSpecType, Parameters
)
from mypy.subtypes import is_equivalent, is_subtype, is_callable_compatible, is_proper_subtype
from mypy.erasetype import erase_type
Expand Down Expand Up @@ -506,6 +506,9 @@ def visit_param_spec(self, t: ParamSpecType) -> ProperType:
else:
return self.default(self.s)

def visit_parameters(self, t: Parameters) -> ProperType:
raise NotImplementedError("meeting two paramspec literals is not supported yet")

def visit_instance(self, t: Instance) -> ProperType:
if isinstance(self.s, Instance):
si = self.s
Expand Down
82 changes: 58 additions & 24 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import difflib
from textwrap import dedent

from typing import cast, List, Dict, Any, Sequence, Iterable, Iterator, Tuple, Set, Optional, Union
from typing import (
cast, List, Dict, Any, Sequence, Iterable, Iterator, Tuple, Set, Optional, Union, Callable
)
from typing_extensions import Final

from mypy.erasetype import erase_type
Expand All @@ -24,7 +26,7 @@
Type, CallableType, Instance, TypeVarType, TupleType, TypedDictType, LiteralType,
UnionType, NoneType, AnyType, Overloaded, FunctionLike, DeletedType, TypeType,
UninhabitedType, TypeOfAny, UnboundType, PartialType, get_proper_type, ProperType,
ParamSpecType, get_proper_types
ParamSpecType, Parameters, get_proper_types
)
from mypy.typetraverser import TypeTraverserVisitor
from mypy.nodes import (
Expand Down Expand Up @@ -1646,6 +1648,32 @@ def quote_type_string(type_string: str) -> str:
return '"{}"'.format(type_string)


def format_callable_args(arg_types: List[Type], arg_kinds: List[ArgKind],
arg_names: List[Optional[str]], format: Callable[[Type], str],
verbosity: int) -> str:
"""Format a bunch of Callable arguments into a string"""
arg_strings = []
for arg_name, arg_type, arg_kind in zip(
arg_names, arg_types, arg_kinds):
if (arg_kind == ARG_POS and arg_name is None
or verbosity == 0 and arg_kind.is_positional()):

arg_strings.append(format(arg_type))
else:
constructor = ARG_CONSTRUCTOR_NAMES[arg_kind]
if arg_kind.is_star() or arg_name is None:
arg_strings.append("{}({})".format(
constructor,
format(arg_type)))
else:
arg_strings.append("{}({}, {})".format(
constructor,
format(arg_type),
repr(arg_name)))

return ", ".join(arg_strings)


def format_type_inner(typ: Type,
verbosity: int,
fullnames: Optional[Set[str]]) -> str:
Expand Down Expand Up @@ -1694,7 +1722,18 @@ def format(typ: Type) -> str:
# This is similar to non-generic instance types.
return typ.name
elif isinstance(typ, ParamSpecType):
return typ.name_with_suffix()
# Concatenate[..., P]
if typ.prefix.arg_types:
args = format_callable_args(
typ.prefix.arg_types,
typ.prefix.arg_kinds,
typ.prefix.arg_names,
format,
verbosity)

return f'Concatenate[{args}, {typ.name_with_suffix()}]'
else:
return typ.name_with_suffix()
elif isinstance(typ, TupleType):
# Prefer the name of the fallback class (if not tuple), as it's more informative.
if typ.partial_fallback.type.fullname != 'builtins.tuple':
Expand Down Expand Up @@ -1764,34 +1803,29 @@ def format(typ: Type) -> str:
return 'Callable[..., {}]'.format(return_type)
param_spec = func.param_spec()
if param_spec is not None:
return f'Callable[{param_spec.name}, {return_type}]'
arg_strings = []
for arg_name, arg_type, arg_kind in zip(
func.arg_names, func.arg_types, func.arg_kinds):
if (arg_kind == ARG_POS and arg_name is None
or verbosity == 0 and arg_kind.is_positional()):

arg_strings.append(format(arg_type))
else:
constructor = ARG_CONSTRUCTOR_NAMES[arg_kind]
if arg_kind.is_star() or arg_name is None:
arg_strings.append("{}({})".format(
constructor,
format(arg_type)))
else:
arg_strings.append("{}({}, {})".format(
constructor,
format(arg_type),
repr(arg_name)))

return 'Callable[[{}], {}]'.format(", ".join(arg_strings), return_type)
return f'Callable[{format(param_spec)}, {return_type}]'
args = format_callable_args(
func.arg_types,
func.arg_kinds,
func.arg_names,
format,
verbosity)
return 'Callable[[{}], {}]'.format(args, return_type)
else:
# Use a simple representation for function types; proper
# function types may result in long and difficult-to-read
# error messages.
return 'overloaded function'
elif isinstance(typ, UnboundType):
return str(typ)
elif isinstance(typ, Parameters):
args = format_callable_args(
typ.arg_types,
typ.arg_kinds,
typ.arg_names,
format,
verbosity)
return f'[{args}]'
elif typ is None:
raise RuntimeError('Type is None')
else:
Expand Down
Loading