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
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 Next commit
Slight improvements
  • Loading branch information
Gobot1234 committed Nov 5, 2021
commit b1d67ca043cb2f4fd3f37f6543ed95cb53ffebd3
28 changes: 24 additions & 4 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,18 +670,38 @@ def prepare_method_signature(self, func: FuncDef, info: TypeInfo) -> None:
leading_type = func.type.arg_types[0]
if not isinstance(leading_type, (Instance, TypeType)):
return
self_type = leading_type if not func.is_class else leading_type.item
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:
Expand Down Expand Up @@ -3296,7 +3316,7 @@ 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]) -> None:
def is_self_type(self, typ: Optional[Type]) -> bool:
if not isinstance(typ, UnboundType):
return False
sym = self.lookup_qualified(typ.name, typ)
Expand Down Expand Up @@ -3629,8 +3649,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.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
1 change: 0 additions & 1 deletion mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,6 @@ def __init__(self,
else:
self.def_extras = {}
self.type_guard = type_guard
# self.created = __import__('inspect').stack()

def copy_modified(self,
arg_types: Bogus[Sequence[Type]] = _dummy,
Expand Down
0