8000 Implementation of PEP 673 (typing.Self) by Gobot1234 · Pull Request #11666 · python/mypy · GitHub
[go: up one dir, main page]

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 1 commit
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
Prev Previous commit
Next 8000 Next commit
Make tests pass
  • Loading branch information
erikkemperman committed May 20, 2022
commit 67661328dbec0f3fbec02087cd6e63c9a0c2995f
6 changes: 3 additions & 3 deletions 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,
SelfType, ENUM_REMOVED_PROPS
SelfType, ENUM_REMOVED_PROPS,
)
from mypy.nodes import (
TypeInfo, FuncBase, Var, FuncDef, SymbolNode, SymbolTable, Context,
Expand Down Expand Up @@ -342,7 +342,7 @@ def analyze_none_member_access(name: str, typ: NoneType, mx: MemberContext) -> T
return _analyze_member_access(name, mx.named_type('builtins.object'), mx)


def analyze_member_var_access(name: str, # what here?
def analyze_member_var_access(name: str,
itype: Instance,
info: TypeInfo,
mx: MemberContext) -> Type:
Expand Down Expand Up @@ -470,7 +470,7 @@ def analyze_descriptor_access(descriptor_type: Type,

if isinstance(descriptor_type, UnionType):
for idx, item in enumerate(descriptor_type.items):
if isinstance(item, SelfType):
if isinstance(get_proper_type(item), SelfType):
descriptor_type.items[idx] = instance_type
# Map the access over union types
return make_simplified_union([
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
65 changes: 39 additions & 26 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
TypeTranslator, TypeOfAny, TypeType, NoneType, PlaceholderType, TPDICT_NAMES, ProperType,
get_proper_type, get_proper_types, TypeAliasType, TypeVarLikeType, Parameters, ParamSpecType,
PROTOCOL_NAMES, TYPE_ALIAS_NAMES, FINAL_TYPE_NAMES, FINAL_DECORATOR_NAMES, REVEAL_TYPE_NAMES,
ASSERT_TYPE_NAMES, OVERLOAD_NAMES, is_named_instance, SelfType,
ASSERT_TYPE_NAMES, OVERLOAD_NAMES, is_named_instance, SelfType, SELF_TYPE_NAMES,
)
from mypy.typeops import function_type, get_type_vars
from mypy.type_visitor import TypeQuery
Expand Down Expand Up @@ -710,18 +710,22 @@ def prepare_method_signature(self, func: FuncDef, info: TypeInfo) -> None:
leading_type = self.class_type(leading_type)
func.type = replace_implicit_first_type(func.type, leading_type)

assert isinstance(func.type, CallableType)
leading_type = func.type.arg_types[0]
if not isinstance(leading_type, (Instance, TypeType)):
proper_leading_type = get_proper_type(leading_type)
if not isinstance(proper_leading_type, (Instance, TypeType)):
return
self_type = leading_type.item if isinstance(leading_type, TypeType) else leading_type
if isinstance(proper_leading_type, TypeType):
self_type = proper_leading_type.item
else:
self_type = proper_leading_type
fullname = None
# bind any SelfTypes
assert isinstance(func.type, CallableType)
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, "
"Self-type annotations of staticmethods are not supported, "
"please replace the type with {}".format(self_type.type.name),
func
)
Expand All @@ -730,24 +734,32 @@ def prepare_method_signature(self, func: FuncDef, info: TypeInfo) -> None:
) # we replace them here for them
continue
if fullname is None:
fullname = self.lookup_qualified(arg.name, arg).node.fullname
aname = getattr(arg, 'name', 'EEEK')
if aname == 'EEEK':
self.fail("Oh no {} has no name".format(arg), func)
table_node = self.lookup_qualified(aname, arg)
assert isinstance(table_node, SymbolTableNode) and table_node.node
fullname = table_node.node.fullname
assert isinstance(self_type, Instance)
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)
table_node = self.lookup_qualified(func.type.ret_type.name, func.type.ret_type)
tname = getattr(func.type.ret_type, 'name', 'ARGH')
if tname == 'ARGH':
self.fail("Oh no {} has no name".format(func.type.ret_type), func)
table_node = self.lookup_qualified(tname, func.type.ret_type)
assert isinstance(table_node, SymbolTableNode) and table_node.node
fullname = cast(str, table_node.node.fullname)
fullname = table_node.node.fullname
if func.is_static:
self.fail(
"Self types in the annotations of staticmethods are not supported, "
"Self-type 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

assert isinstance(self_type, Instance)
func.type.ret_type = SelfType(self_type, fullname=fullname)

def set_original_def(self, previous: Optional[Node], new: Union[FuncDef, Decorator]) -> bool:
Expand Down Expand Up @@ -1647,18 +1659,19 @@ 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
])

# 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 @@ -3515,7 +3528,7 @@ def is_self_type(self, typ: Optional[Type]) -> bool:
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')
return sym.node.fullname in SELF_TYPE_NAMES

def fail_invalid_classvar(self, context: Context) -> None:
self.fail(message_registry.CLASS_VAR_OUTSIDE_OF_CLASS, context)
Expand Down Expand Up @@ -3917,8 +3930,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 sym.node and sym.node.fullname in SELF_TYPE_NAMES 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
51 changes: 29 additions & 22 deletions mypy/type_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
other modules refer to them.
"""

from abc import ABCMeta, abstractmethod
from abc import abstractmethod
from mypy.backports import OrderedDict
from typing import Generic, TypeVar, cast, Any, List, Callable, Iterable, Optional, Set, Sequence
from typing import Generic, TypeVar, cast, Any, List, Callable, Iterable, Optional,\
Set, Sequence
from mypy_extensions import trait, mypyc_attr

T = TypeVar('T')
Expand Down Expand Up @@ -403,11 +404,8 @@ def query_types(self, types: Iterable[Type]) -> T:
return self.strategy(res)


TypeT = TypeVar("TypeT", bound=Type)


class SelfTypeVisitor(TypeVisitor[Any]):
def __init__(self, self_type: Instance) -> None:
def __init__(self, self_type: Type) -> None:
# NOTE this visitor will mutate `func`
self.self_type = self_type

Expand Down Expand Up @@ -438,45 +436,54 @@ def visit_self_type(self, t: SelfType) -> None:
def visit_param_spec(self, t: ParamSpecType) -> None:
pass

def visit_type_var_tuple(self, t: TypeVarTupleType) -> T:
pass

def visit_unpack_type(self, t: UnpackType) -> T:
pass

def visit_parameters(self, t: Parameters) -> T:
pass

def visit_instance(self, t: Instance) -> None:
t.args = self.replace(t.args)
t.args = tuple(self.replace_types(t.args))

def visit_callable_type(self, t: CallableType) -> None:
t.arg_types = self.replace(t.arg_types)
t.ret_type, = self.replace([t.ret_type])
t.arg_types = self.replace_types(t.arg_types)
t.ret_type = self.replace_type(t.ret_type)

def visit_overloaded(self, t: Overloaded) -> None:
for item in t.items:
item.accept(self)

def visit_tuple_type(self, t: TupleType) -> None:
t.items = self.replace(t.items)
t.items = self.replace_types(t.items)

def visit_typeddict_type(self, t: TypedDictType) -> None:
for key, value in zip(t.items, self.replace(t.items.values())):
for key, value in zip(t.items, self.replace_types(t.items.values())):
t.items[key] = value

def visit_literal_type(self, t: LiteralType) -> None:
pass

def visit_union_type(self, t: UnionType) -> None:
t.items = self.replace(t.items)
t.items = self.replace_types(t.items)

def visit_partial_type(self, t: PartialType) -> None:
pass

def visit_type_type(self, t: TypeType) -> None:
t.item, = self.replace([t.item])
t.item = get_proper_type(self.replace_type(t.item))

def visit_type_alias_type(self, t: TypeAliasType) -> None:
pass # TODO this is probably invalid

def replace(self, types: Iterable[TypeT]) -> List[TypeT]:
ret: List[TypeT] = []
for type in types:
if isinstance(type, SelfType):
type = self.self_type
else:
type.accept(self)
ret.append(type) # type: ignore # not sure if this is actually unsafe
return ret
def replace_types(self, types: Iterable[Type]) -> List[Type]:
return [self.replace_type(typ) for typ in types]

def replace_type(self, typ: Type) -> Type:
if isinstance(typ, SelfType): # type: ignore
typ = self.self_type
else:
typ.accept(self)
return typ
12 changes: 8 additions & 4 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Parameters, TypeQuery, union_items, TypeOfAny, LiteralType, RawExpressionType,
PlaceholderType, Overloaded, get_proper_type, TypeAliasType, RequiredType,
TypeVarLikeType, ParamSpecType, ParamSpecFlavor, UnpackType, TypeVarTupleType,
callable_with_ellipsis, TYPE_ALIAS_NAMES, FINAL_TYPE_NAMES,
callable_with_ellipsis, TYPE_ALIAS_NAMES, FINAL_TYPE_NAMES, SELF_TYPE_NAMES,
LITERAL_TYPE_NAMES, ANNOTATED_TYPE_NAMES, SelfType,
)

Expand Down Expand Up @@ -428,11 +428,15 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Opt
self.fail("NotRequired[] must have exactly one type argument", t)
return AnyType(TypeOfAny.from_error)
return RequiredType(self.anal_type(t.args[0]), required=False)
elif fullname in ('typing_extensions.Self', 'typing.Self'):
elif fullname in SELF_TYPE_NAMES:
from mypy.semanal import SemanticAnalyzer # circular import

if not isinstance(self.api, SemanticAnalyzer):
return self.fail("Self is unbound", t)
self.fail("Self is unbound", t)
return AnyType(TypeOfAny.from_error)
if not isinstance(self.api.type, TypeInfo):
self.fail("Self is not enclosed in a class", t)
return AnyType(TypeOfAny.from_error)
bound = self.named_type(self.api.type.fullname)
return SelfType(bound, fullname, line=t.line, column=t.column)
elif self.anal_type_guard_arg(t, fullname) is not None:
Expand Down Expand Up @@ -1141,7 +1145,7 @@ def bind_function_type_variables(
var_node = self.lookup_qualified(var.name, defn)
assert var_node, "Binding for function type variable not found within function"
var_expr = var_node.node
assert isinstance(var_expr, TypeVarLikeExpr), f"got {var.__class__} {var_expr.__class__}"
assert isinstance(var_expr, TypeVarLikeExpr)
self.tvar_scope.bind_new(var.name, var_expr)
return fun_type.variables
typevars = self.infer_type_variables(fun_type)
Expand Down
3 changes: 2 additions & 1 deletion mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ def expand(target: Type) -> Type:
variables=variables,
ret_type=ret_type,
bound_args=[original_type])
res.accept(SelfTypeVisitor(original_type))
if original_type:
res.accept(SelfTypeVisitor(original_type))
return cast(F, res)


Expand Down
18 changes: 12 additions & 6 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@
'typing_extensions.final',
)

# Supported Self type names.
SELF_TYPE_NAMES: Final = (
'typing.Self',
'typing_extensions.Self',
)

# Supported Literal type names.
LITERAL_TYPE_NAMES: Final = (
'typing.Literal',
Expand Down Expand Up @@ -554,12 +560,11 @@ class SelfType(ProperType):

__slots__ = ('fullname', 'instance')

def __init__(self, instance: 'Instance', fullname: str, line: int = -1, column: int = -1) -> None:
def __init__(self, instance: 'Instance', fullname: str,
line: int = -1, column: int = -1) -> None:
super().__init__(line, column)
self.fullname = fullname
self.instance = instance
self.line = line
self.column = column

def accept(self, visitor: 'TypeVisitor[T]') -> T:
return visitor.visit_self_type(self)
Expand All @@ -574,9 +579,8 @@ def serialize(self) -> JsonDict:
@classmethod
def deserialize(cls, data: JsonDict) -> 'SelfType':
assert data['.class'] == 'SelfType'
instance = deserialize_type(data['instance'])
assert isinstance(instance, Instance)
return cls(instance, data['fullname'])
assert isinstance(data['instance'], str)
return SelfType(Instance.deserialize(data['instance']), data['fullname'])


class ParamSpecFlavor:
Expand Down Expand Up @@ -1486,6 +1490,8 @@ class CallableType(FunctionLike):
'definition', # For error messages. May be None.
'variables', # Type variables for a generic function
'is_ellipsis_args', # Is this Callable[..., t] (with literal '...')?
'is_classmethod_class', # Is this callable constructed for the benefit
# of a classmethod's 'cls' argument?
'implicit', # Was this type implicitly generated instead of explicitly
# specified by the user?
'special_sig', # Non-None for signatures that require special handling
Expand Down
2 changes: 1 addition & 1 deletion mypy/typetraverser.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def visit_instance(self, t: Instance) -> None:
self.traverse_types(t.args)

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

def visit_callable_type(self, t: CallableType) -> None:
# FIX generics
Expand Down
2 changes: 1 addition & 1 deletion runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
options.raise_exceptions = True
# options.verbosity = 10
result = build([BuildSource("test.py", None, )], options, stderr=sys.stderr, stdout=sys.stdout)
print(*result.errors, sep="\n")
print(*result.errors, sep="\n")
0