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 10 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
2 changes: 1 addition & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4926,7 +4926,7 @@ def check_subtype(self,
if subtype_label is not None:
extra_info.append(subtype_label + ' ' + subtype_str)
if supertype_label is not None:
extra_info.append(supertype_label + ' ' + supertype_str)
extra_info.append(f'{supertype_label} {supertype_str}')
note_msg = make_inferred_type_note(outer_context or context, subtype,
supertype, supertype_str)
if isinstance(subtype, Instance) and isinstance(supertype, Instance):
Expand Down
40 changes: 36 additions & 4 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):
# no such thing as variance for ParamSpecs
# TODO: is there a case I am missing?
# TODO: what is setting meta_level to 0?
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):
# no such thing as variance for ParamSpecs
# TODO: is there a case I am missing?
# TODO: what is setting meta_level to 0?
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 @@ -562,9 +587,16 @@ def visit_callable_type(self, template: CallableType) -> List[Constraint]:
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())))

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
17 changes: 15 additions & 2 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,26 @@ 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):
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:
# returning parameters
# 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 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
5 changes: 4 additions & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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 @@ -1792,6 +1792,9 @@ def format(typ: Type) -> str:
return 'overloaded function'
elif isinstance(typ, UnboundType):
return str(typ)
elif isinstance(typ, Parameters):
# TODO: technically this is not the right way to format (there could be non-pos).
return '[{}]'.format(', '.join(map(format, typ.arg_types)))
elif typ is None:
raise RuntimeError('Type is None')
else:
Expand Down
8 changes: 7 additions & 1 deletion mypy/sametypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Type, UnboundType, AnyType, NoneType, TupleType, TypedDictType,
UnionType, CallableType, TypeVarType, Instance, TypeVisitor, ErasedType,
Overloaded, PartialType, DeletedType, UninhabitedType, TypeType, LiteralType,
ProperType, get_proper_type, TypeAliasType, ParamSpecType
ProperType, get_proper_type, TypeAliasType, ParamSpecType, Parameters
)
from mypy.typeops import tuple_fallback, make_simplified_union

Expand Down Expand Up @@ -102,6 +102,12 @@ def visit_param_spec(self, left: ParamSpecType) -> bool:
return (isinstance(self.right, ParamSpecType) and
left.id == self.right.id and left.flavor == self.right.flavor)

def visit_parameters(self, left: Parameters) -> bool:
return (isinstance(self.right, Parameters) and
left.arg_names == self.right.arg_names and
is_same_types(left.arg_types, self.right.arg_types) and
left.arg_kinds == self.right.arg_kinds)

def visit_callable_type(self, left: CallableType) -> bool:
# FIX generics
if isinstance(self.right, CallableType):
Expand Down
9 changes: 8 additions & 1 deletion mypy/server/astdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class level -- these are handled at attribute level (say, 'mod.Cls.method'
from mypy.types import (
Type, TypeVisitor, UnboundType, AnyType, NoneType, UninhabitedType,
ErasedType, DeletedType, Instance, TypeVarType, CallableType, TupleType, TypedDictType,
UnionType, Overloaded, PartialType, TypeType, LiteralType, TypeAliasType, ParamSpecType
UnionType, Overloaded, PartialType, TypeType, LiteralType, TypeAliasType, ParamSpecType,
Parameters
)
from mypy.util import get_prefix

Expand Down Expand Up @@ -317,6 +318,12 @@ def visit_param_spec(self, typ: ParamSpecType) -> SnapshotItem:
typ.flavor,
snapshot_type(typ.upper_bound))

def visit_parameters(self, typ: Parameters) -> SnapshotItem:
return ('Parameters',
snapshot_types(typ.arg_types),
tuple(encode_optional_str(name) for name in typ.arg_names),
tuple(typ.arg_kinds))

def visit_callable_type(self, typ: CallableType) -> SnapshotItem:
# FIX generics
return ('CallableType',
Expand Down
6 changes: 5 additions & 1 deletion mypy/server/astmerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
Type, SyntheticTypeVisitor, Instance, AnyType, NoneType, CallableType, ErasedType, DeletedType,
TupleType, TypeType, TypedDictType, UnboundType, UninhabitedType, UnionType,
Overloaded, TypeVarType, TypeList, CallableArgument, EllipsisType, StarType, LiteralType,
RawExpressionType, PartialType, PlaceholderType, TypeAliasType, ParamSpecType
RawExpressionType, PartialType, PlaceholderType, TypeAliasType, ParamSpecType, Parameters
)
from mypy.util import get_prefix, replace_object_state
from mypy.typestate import TypeState
Expand Down Expand Up @@ -411,6 +411,10 @@ def visit_type_var(self, typ: TypeVarType) -> None:
def visit_param_spec(self, typ: ParamSpecType) -> None:
pass

def visit_parameters(self, typ: Parameters) -> None:
for arg in typ.arg_types:
arg.accept(self)

def visit_typeddict_type(self, typ: TypedDictType) -> None:
for value_type in typ.items.values():
value_type.accept(self)
Expand Down
8 changes: 7 additions & 1 deletion mypy/server/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class 'mod.Cls'. This can also refer to an attribute inherited from a
Type, Instance, AnyType, NoneType, TypeVisitor, CallableType, DeletedType, PartialType,
TupleType, TypeType, TypeVarType, TypedDictType, UnboundType, UninhabitedType, UnionType,
FunctionLike, Overloaded, TypeOfAny, LiteralType, ErasedType, get_proper_type, ProperType,
TypeAliasType, ParamSpecType
TypeAliasType, ParamSpecType, Parameters
)
from mypy.server.trigger import make_trigger, make_wildcard_trigger
from mypy.util import correct_relative_import
Expand Down Expand Up @@ -961,6 +961,12 @@ def visit_param_spec(self, typ: ParamSpecType) -> List[str]:
triggers.extend(self.get_type_triggers(typ.upper_bound))
return triggers

def visit_parameters(self, typ: Parameters) -> List[str]:
triggers = []
for arg in typ.arg_types:
triggers.extend(self.get_type_triggers(arg))
return triggers

def visit_typeddict_type(self, typ: TypedDictType) -> List[str]:
triggers = []
for item in typ.items.values():
Expand Down
Loading