Skip to content

Implementation of PEP 673 (typing.Self) #11666

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

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
acf87a3
Much better initial implementation of typing.Self
Gobot1234 Aug 28, 2021
7a6d771
Some extra stuff
Gobot1234 Oct 2, 2021
b1d67ca
Slight improvements
Gobot1234 Nov 5, 2021
c09d95f
Merge branch 'master' into master
Gobot1234 Dec 5, 2021
6a7a084
Remove removed type
Gobot1234 Dec 5, 2021
2ac45c9
Update meet.py
Gobot1234 Dec 5, 2021
1ebe034
Update mypy/type_visitor.py
Gobot1234 Dec 5, 2021
0e6d128
Fix most of the CI issues and respond to comments
Gobot1234 Dec 6, 2021
f844fbe
Merge branch 'master' of https://github.com/Gobot1234/mypy
Gobot1234 Dec 6, 2021
5904918
Merge remote-tracking branch 'upstream/master'
Gobot1234 Jan 14, 2022
e932f52
Merge branch 'master' into master
Gobot1234 Feb 16, 2022
ff779e8
I think this works properly now
Gobot1234 Feb 18, 2022
f94254f
Merge branch 'master' of https://github.com/Gobot1234/mypy
Gobot1234 Feb 18, 2022
c9b2ac9
Merge branch 'master' into master
Gobot1234 Feb 18, 2022
ad0b9b0
Merge remote-tracking branch 'origin/master' into gobot-master
erikkemperman May 20, 2022
6766132
Make tests pass
erikkemperman May 20, 2022
cada36a
Unit tests
erikkemperman May 20, 2022
68c1339
Merge pull request #1 from erikkemperman/gobot-master
Gobot1234 Jun 22, 2022
46d8b70
Merge remote-tracking branch 'upstream/master'
Gobot1234 Jun 22, 2022
fb6d552
I don't think this is entirely correct but lets see
Gobot1234 Jun 22, 2022
6c71758
Fix tests
Gobot1234 Jun 27, 2022
791c9e3
Fixes for signatures of form (type[Self]/Self) -> Self
Gobot1234 Jul 1, 2022
09e966e
Fix some CI
Gobot1234 Jul 1, 2022
ce2d5fa
Fix some CI failures
Gobot1234 Jul 6, 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
10 changes: 9 additions & 1 deletion mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from mypy.types import (
Type, Instance, AnyType, TupleType, TypedDictType, CallableType, FunctionLike,
TypeVarLikeType, Overloaded, TypeVarType, UnionType, PartialType, TypeOfAny, LiteralType,
DeletedType, NoneType, TypeType, has_type_vars, get_proper_type, ProperType, ParamSpecType
DeletedType, NoneType, TypeType, has_type_vars, get_proper_type, ProperType, ParamSpecType,
SelfType
)
from mypy.nodes import (
TypeInfo, FuncBase, Var, FuncDef, SymbolNode, SymbolTable, Context,
Expand Down Expand Up @@ -144,6 +145,8 @@ def _analyze_member_access(name: str,
typ = get_proper_type(typ)
if isinstance(typ, Instance):
return analyze_instance_member_access(name, typ, mx, override_info)
elif isinstance(typ, SelfType):
return analyze_instance_member_access(name, typ.instance, mx, override_info)
elif isinstance(typ, AnyType):
# The base object has dynamic type.
return AnyType(TypeOfAny.from_another_any, source_any=typ)
Expand Down Expand Up @@ -465,13 +468,18 @@ def analyze_descriptor_access(descriptor_type: Type,
descriptor_type = get_proper_type(descriptor_type)

if isinstance(descriptor_type, UnionType):
for idx, item in enumerate(descriptor_type.items):
if isinstance(item, SelfType):
descriptor_type.items[idx] = instance_type
# Map the access over union types
return make_simplified_union([
analyze_descriptor_access(typ, mx)
for typ in descriptor_type.items
])
elif not isinstance(descriptor_type, Instance):
return descriptor_type
elif isinstance(descriptor_type, SelfType):
return instance_type

if not descriptor_type.type.has_readable_member('__get__'):
return descriptor_type
Expand Down
5 changes: 4 additions & 1 deletion mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
CallableType, Type, TypeVisitor, UnboundType, AnyType, NoneType, TypeVarType, Instance,
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,
ProperType, ParamSpecType, get_proper_type, TypeAliasType, SelfType, is_union_with_any,
callable_with_ellipsis
)
from mypy.maptype import map_instance_to_supertype
Expand Down Expand Up @@ -399,6 +399,9 @@ def visit_type_var(self, template: TypeVarType) -> List[Constraint]:
assert False, ("Unexpected TypeVarType in ConstraintBuilderVisitor"
" (should have been handled in infer_constraints)")

def visit_self_type(self, template: SelfType) -> List[Constraint]:
return self.visit_instance(template.instance)

def visit_param_spec(self, template: ParamSpecType) -> List[Constraint]:
# Can't infer ParamSpecs from component values (only via Callable[P, T]).
return []
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, SelfType
)
from mypy.nodes import ARG_STAR, ARG_STAR2

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

def visit_self_type(self, t: SelfType) -> ProperType:
return self.visit_instance(t.instance)

def visit_param_spec(self, t: ParamSpecType) -> ProperType:
return AnyType(TypeOfAny.special_form)

Expand Down
5 changes: 4 additions & 1 deletion 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, SelfType, TypeVarLikeType
)


Expand Down Expand Up @@ -99,6 +99,9 @@ def visit_type_var(self, t: TypeVarType) -> Type:
else:
return repl

def visit_self_type(self, t: SelfType) -> Type:
return t

def visit_param_spec(self, t: ParamSpecType) -> Type:
repl = get_proper_type(self.variables.get(t.id, t))
if isinstance(repl, Instance):
Expand Down
6 changes: 5 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,
SelfType
)
from mypy.visitor import NodeVisitor
from mypy.lookup import lookup_fully_qualified
Expand Down Expand Up @@ -248,6 +249,9 @@ def visit_type_var(self, tvt: TypeVarType) -> None:
if tvt.upper_bound is not None:
tvt.upper_bound.accept(self)

def visit_self_type(self, t: SelfType) -> None:
return t.instance.accept(self)

def visit_param_spec(self, p: ParamSpecType) -> None:
p.upper_bound.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 @@ -64,6 +64,9 @@ def visit_deleted_type(self, t: types.DeletedType) -> Set[str]:
def visit_type_var(self, t: types.TypeVarType) -> Set[str]:
return self._visit(t.values) | self._visit(t.upper_bound)

def visit_self_type(self, t: types.SelfType) -> Set[str]:
return set()

def visit_param_spec(self, t: types.ParamSpecType) -> Set[str]:
return set()

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, SelfType
)
from mypy.maptype import map_instance_to_supertype
from mypy.subtypes import (
Expand Down Expand Up @@ -251,6 +251,9 @@ def visit_type_var(self, t: TypeVarType) -> ProperType:
else:
return self.default(self.s)

def visit_self_type(self, t: SelfType) -> ProperType:
return self.join(self.s, t.instance)

def visit_param_spec(self, t: ParamSpecType) -> ProperType:
if self.s == t:
return t
Expand Down
5 changes: 4 additions & 1 deletion mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Type, AnyType, TypeVisitor, UnboundType, NoneType, TypeVarType, Instance, CallableType,
TupleType, TypedDictType, ErasedType, UnionType, PartialType, DeletedType,
UninhabitedType, TypeType, TypeOfAny, Overloaded, FunctionLike, LiteralType,
ProperType, get_proper_type, get_proper_types, TypeAliasType, TypeGuardedType,
ProperType, get_proper_type, get_proper_types, TypeAliasType, TypeGuardedType, SelfType,
ParamSpecType
)
from mypy.subtypes import is_equivalent, is_subtype, is_callable_compatible, is_proper_subtype
Expand Down Expand Up @@ -500,6 +500,9 @@ def visit_type_var(self, t: TypeVarType) -> ProperType:
else:
return self.default(self.s)

def visit_self_type(self, t: SelfType) -> ProperType:
return self.meet(self.s, t.instance)

def visit_param_spec(self, t: ParamSpecType) -> ProperType:
if self.s == t:
return self.s
Expand Down
6 changes: 5 additions & 1 deletion mypy/mixedtraverser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
CastExpr, TypeApplication, TypeAliasExpr, TypeVarExpr, TypedDictExpr, NamedTupleExpr,
PromoteExpr, NewTypeExpr
)
from mypy.types import Type
from mypy.types import Type, SelfType
from mypy.traverser import TraverserVisitor
from mypy.typetraverser import TypeTraverserVisitor

Expand Down Expand Up @@ -41,6 +41,10 @@ def visit_type_var_expr(self, o: TypeVarExpr) -> None:
for value in o.values:
value.accept(self)

def visit_self_type(self, o: SelfType) -> None:
super().visit_self_type(o)
o.instance.accept(self)

def visit_typeddict_expr(self, o: TypedDictExpr) -> None:
super().visit_typeddict_expr(o)
self.visit_optional_type(o.info.typeddict_type)
Expand Down
5 changes: 4 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, SelfType, ParamSpecType
)
from mypy.typeops import tuple_fallback, make_simplified_union

Expand Down Expand Up @@ -97,6 +97,9 @@ def visit_type_var(self, left: TypeVarType) -> bool:
return (isinstance(self.right, TypeVarType) and
left.id == self.right.id)

def visit_self_type(self, left: SelfType) -> bool:
return isinstance(self.right, SelfType) and self.right.instance == left.instance

def visit_param_spec(self, left: ParamSpecType) -> bool:
# Ignore upper bound since it's derived from flavor.
return (isinstance(self.right, ParamSpecType) and
Expand Down
69 changes: 64 additions & 5 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
FunctionLike, UnboundType, TypeVarType, TupleType, UnionType, StarType,
CallableType, Overloaded, Instance, Type, AnyType, LiteralType, LiteralValue,
TypeTranslator, TypeOfAny, TypeType, NoneType, PlaceholderType, TPDICT_NAMES, ProperType,
get_proper_type, get_proper_types, TypeAliasType, TypeVarLikeType
get_proper_type, get_proper_types, TypeAliasType, SelfType, TypeVarLikeType
)
from mypy.typeops import function_type, get_type_vars
from mypy.type_visitor import TypeQuery
Expand Down Expand Up @@ -681,19 +681,55 @@ def analyze_func_def(self, defn: FuncDef) -> None:
def prepare_method_signature(self, func: FuncDef, info: TypeInfo) -> None:
"""Check basic signature validity and tweak annotation of self/cls argument."""
# Only non-static methods are special.
functype = func.type
if not func.is_static:
if func.name in ['__init_subclass__', '__class_getitem__']:
func.is_class = True
if not func.arguments:
self.fail('Method must have at least one argument', func)
elif isinstance(functype, CallableType):
self_type = get_proper_type(functype.arg_types[0])
elif isinstance(func.type, CallableType):
self_type = get_proper_type(func.type.arg_types[0])
if isinstance(self_type, AnyType):
leading_type: Type = fill_typevars(info)
if func.is_class or func.name == '__new__':
leading_type = self.class_type(leading_type)
func.type = replace_implicit_first_type(functype, leading_type)
func.type = replace_implicit_first_type(func.type, leading_type)

leading_type = func.type.arg_types[0]
if not isinstance(leading_type, (Instance, TypeType)):
return
self_type = leading_type.item if isinstance(leading_type, TypeType) else leading_type
fullname = None
# bind any SelfTypes
for idx, arg in enumerate(func.type.arg_types):
if self.is_self_type(arg):
if func.is_static:
self.fail(
"Self types in the annotations of staticmethods are not supported, "
"please replace the type with {}".format(self_type.type.name),
func
)
func.type.arg_types[idx] = self.named_type(
self_type.type.name
) # we replace them here for them
continue
if fullname is None:
fullname = self.lookup_qualified(arg.name, arg).node.fullname
func.type.arg_types[idx] = SelfType(self_type, fullname=fullname)

if self.is_self_type(func.type.ret_type):
if fullname is None:
self.lookup_qualified(func.type.ret_type.name, func.type.ret_type)
fullname = self.lookup_qualified(func.type.ret_type.name, func.type.ret_type).node.fullname
if func.is_static:
self.fail(
"Self types in the annotations of staticmethods are not supported, "
"please replace the type with {}".format(self_type.type.name),
func,
)
func.type.ret_type = self.named_type(self_type.type.name)
return

func.type.ret_type = SelfType(self_type, fullname=fullname)

def set_original_def(self, previous: Optional[Node], new: Union[FuncDef, Decorator]) -> bool:
"""If 'new' conditionally redefine 'previous', set 'previous' as original
Expand Down Expand Up @@ -1601,6 +1637,18 @@ def configure_base_classes(self,
self.set_dummy_mro(defn.info)
return
self.calculate_class_mro(defn, self.object_type)
return
for base in info.mro:
for name, type in base.names.items():
if isinstance(type, SelfType): # bind Self
info.names[name] = SelfType(self.named_type(defn.fullname), type.fullname)
elif isinstance(type, UnionType):
info.names[name] = UnionType([
item
if not isinstance(item, SelfType)
else SelfType(self.named_type(defn.fullname), type.fullname)
for item in type.items
])

def configure_tuple_base_class(self,
defn: ClassDef,
Expand Down Expand Up @@ -3366,6 +3414,14 @@ def is_final_type(self, typ: Optional[Type]) -> bool:
return False
return sym.node.fullname in ('typing.Final', 'typing_extensions.Final')

def is_self_type(self, typ: Optional[Type]) -> bool:
if not isinstance(typ, UnboundType):
return False
sym = self.lookup_qualified(typ.name, typ)
if not sym or not sym.node:
return False
return sym.node.fullname in ('typing.Self', 'typing_extensions.Self')

def fail_invalid_classvar(self, context: Context) -> None:
self.fail(message_registry.CLASS_VAR_OUTSIDE_OF_CLASS, context)

Expand Down Expand Up @@ -3747,6 +3803,8 @@ def visit_name_expr(self, expr: NameExpr) -> None:

def bind_name_expr(self, expr: NameExpr, sym: SymbolTableNode) -> None:
"""Bind name expression to a symbol table node."""
# if sym.node.fullname in ('typing.Self', 'typing_extensions.Self') and not self.is_class_scope():
# self.fail('{} is unbound'.format(expr.name), expr)
if isinstance(sym.node, TypeVarExpr) and self.tvar_scope.get_binding(sym):
self.fail('"{}" is a type variable and only valid in type '
'context'.format(expr.name), expr)
Expand Down Expand Up @@ -5262,6 +5320,7 @@ def visit_placeholder_type(self, t: PlaceholderType) -> bool:
def has_placeholder(typ: Type) -> bool:
"""Check if a type contains any placeholder types (recursively)."""
return typ.accept(HasPlaceholders())
return t


def replace_implicit_first_type(sig: FunctionLike, new: Type) -> FunctionLike:
Expand Down
11 changes: 10 additions & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
Type, AnyType, UnboundType, TypeVisitor, FormalArgument, NoneType,
Instance, TypeVarType, CallableType, TupleType, TypedDictType, UnionType, Overloaded,
ErasedType, PartialType, DeletedType, UninhabitedType, TypeType, is_named_instance,
FunctionLike, TypeOfAny, LiteralType, get_proper_type, TypeAliasType, ParamSpecType
FunctionLike, TypeOfAny, LiteralType, get_proper_type, TypeAliasType, ParamSpecType,
SelfType
)
import mypy.applytype
import mypy.constraints
Expand Down Expand Up @@ -250,6 +251,8 @@ def visit_instance(self, left: Instance) -> bool:
return False
return True
right = self.right
if isinstance(right, SelfType):
return self._is_subtype(left, right.instance)
if isinstance(right, TupleType) and mypy.typeops.tuple_fallback(right).type.is_enum:
return self._is_subtype(left, mypy.typeops.tuple_fallback(right))
if isinstance(right, Instance):
Expand Down Expand Up @@ -317,6 +320,9 @@ def visit_type_var(self, left: TypeVarType) -> bool:
return True
return self._is_subtype(left.upper_bound, self.right)

def visit_self_type(self, left: SelfType) -> bool:
return self._is_subtype(left.instance, self.right)

def visit_param_spec(self, left: ParamSpecType) -> bool:
right = self.right
if (
Expand Down Expand Up @@ -1350,6 +1356,9 @@ def visit_type_var(self, left: TypeVarType) -> bool:
return True
return self._is_proper_subtype(left.upper_bound, self.right)

def visit_self_type(self, t: SelfType) -> bool:
return self._is_proper_subtype(t.instance, self.right)

def visit_param_spec(self, left: ParamSpecType) -> bool:
right = self.right
if (
Expand Down
Loading