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

Implementation of PEP 673 (typing.Self) #11666

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -7,7 +7,7 @@
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,
ENUM_REMOVED_PROPS
SelfType, ENUM_REMOVED_PROPS,
)
from mypy.nodes import (
TypeInfo, FuncBase, Var, FuncDef, SymbolNode, SymbolTable, Context,
Expand Down Expand Up @@ -145,6 +145,9 @@ 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):
mx.self_type = typ.instance
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 @@ -495,13 +498,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(get_proper_type(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, is_union_with_any, SelfType,
UnpackType, callable_with_ellipsis, Parameters, TUPLE_LIKE_INSTANCE_NAMES, TypeVarTupleType,
)
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/copytype.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ProperType, UnboundType, AnyType, NoneType, UninhabitedType, ErasedType, DeletedType,
Instance, TypeVarType, ParamSpecType, PartialType, CallableType, TupleType, TypedDictType,
LiteralType, UnionType, Overloaded, TypeType, TypeAliasType, UnpackType, Parameters,
TypeVarTupleType
TypeVarTupleType, SelfType
)
from mypy.type_visitor import TypeVisitor

Expand Down Expand Up @@ -75,6 +75,9 @@ def visit_unpack_type(self, t: UnpackType) -> ProperType:
dup = UnpackType(t.type)
return self.copy_common(t, dup)

def visit_self_type(self, t: SelfType) -> ProperType:
return self.copy_common(t, SelfType(t.instance, t.fullname, t.line, t.column))

def visit_partial_type(self, t: PartialType) -> ProperType:
return self.copy_common(t, PartialType(t.type, t.var, t.value_type))

Expand Down
5 changes: 4 additions & 1 deletion mypy/erasetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
CallableType, TupleType, TypedDictType, UnionType, Overloaded, ErasedType, PartialType,
DeletedType, TypeTranslator, UninhabitedType, TypeType, TypeOfAny, LiteralType, ProperType,
get_proper_type, get_proper_types, TypeAliasType, ParamSpecType, Parameters, UnpackType,
TypeVarTupleType
TypeVarTupleType, 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 @@ -6,7 +6,7 @@
ErasedType, PartialType, DeletedType, UninhabitedType, TypeType, TypeVarId,
FunctionLike, TypeVarType, LiteralType, get_proper_type, ProperType,
TypeAliasType, ParamSpecType, TypeVarLikeType, Parameters, ParamSpecFlavor,
UnpackType, TypeVarTupleType
UnpackType, TypeVarTupleType, SelfType,
)


Expand Down Expand Up @@ -100,6 +100,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
5 changes: 4 additions & 1 deletion mypy/fixup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
CallableType, Instance, Overloaded, TupleType, TypedDictType,
TypeVarType, UnboundType, UnionType, TypeVisitor, LiteralType,
TypeType, NOT_READY, TypeAliasType, AnyType, TypeOfAny, ParamSpecType,
Parameters, UnpackType, TypeVarTupleType
Parameters, UnpackType, TypeVarTupleType, SelfType,
)
from mypy.visitor import NodeVisitor
from mypy.lookup import lookup_fully_qualified
Expand Down Expand Up @@ -246,6 +246,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 @@ -8,7 +8,7 @@
TupleType, TypedDictType, ErasedType, UnionType, FunctionLike, Overloaded, LiteralType,
PartialType, DeletedType, UninhabitedType, TypeType, TypeOfAny, get_proper_type,
ProperType, get_proper_types, TypeAliasType, PlaceholderType, ParamSpecType, Parameters,
UnpackType, TypeVarTupleType,
UnpackType, TypeVarTupleType, SelfType,
)
from mypy.maptype import map_instance_to_supertype
from mypy.subtypes import (
Expand Down Expand Up @@ -256,6 +256,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 @@ -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, Parameters, UnpackType, TypeVarTupleType, TypeVarLikeType
ParamSpecType, Parameters, UnpackType, TypeVarTupleType, SelfType, TypeVarLikeType
)
from mypy.subtypes import is_equivalent, is_subtype, is_callable_compatible, is_proper_subtype
from mypy.erasetype import erase_type
Expand Down Expand Up @@ -537,6 +537,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 @@ -5,7 +5,7 @@
UnionType, CallableType, TypeVarType, Instance, TypeVisitor, ErasedType,
Overloaded, PartialType, DeletedType, UninhabitedType, TypeType, LiteralType,
ProperType, get_proper_type, TypeAliasType, ParamSpecType, Parameters,
UnpackType, TypeVarTupleType,
UnpackType, TypeVarTupleType, SelfType,
)
from mypy.typeops import tuple_fallback, make_simplified_union, is_simple_literal

Expand Down Expand Up @@ -114,6 +114,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
Loading