-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Self Type #2193
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
Self Type #2193
Changes from 17 commits
04934c3
4eb5f75
181ff09
25e104a
5e72623
29d2e0f
4d5d479
ea6c485
00596a6
39615c2
f8875f9
9ae3bf9
b29bf87
f097967
747c5b2
9b68a2f
65d6428
6c161b9
1cae034
733edc1
91946e2
abcb094
9f831c4
aade1ed
a6a0a3b
8e81051
a9b0b68
ec0f33a
09cd7bc
8b242e2
a8be2a6
3055ab0
72af252
bb2ac78
5f26cac
1f44c73
fcbcf4b
860c96a
b078d61
fc29a3a
2299a00
137f452
4bcbf59
6155c7f
47e5e2f
00ae83d
30f847e
c152c20
4f2017e
fddbba0
3cf8ecf
707da4e
48e139d
25b84f8
8944ff1
9221001
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
"""Abstract syntax tree node classes (i.e. parse tree).""" | ||
|
||
import os | ||
import re | ||
from abc import abstractmethod, ABCMeta | ||
from abc import abstractmethod | ||
|
||
from typing import ( | ||
Any, TypeVar, List, Tuple, cast, Set, Dict, Union, Optional | ||
|
@@ -129,7 +128,7 @@ def get_column(self) -> int: | |
return self.column | ||
|
||
def accept(self, visitor: NodeVisitor[T]) -> T: | ||
raise RuntimeError('Not implemented') | ||
raise RuntimeError('Not implemented: {}.accept({})'.format(type(self), type(visitor))) | ||
|
||
|
||
# These are placeholders for a future refactoring; see #1783. | ||
|
@@ -1648,9 +1647,10 @@ def accept(self, visitor: NodeVisitor[T]) -> T: | |
# | ||
# If T is contravariant in Foo[T], Foo[object] is a subtype of | ||
# Foo[int], but not vice versa. | ||
CONTRAVARIANT = -1 # type: int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change of the constant value seems unrelated to the rest of diff and unnecessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My poor git skills seems to have failed me. I did revert this one. |
||
INVARIANT = 0 # type: int | ||
COVARIANT = 1 # type: int | ||
CONTRAVARIANT = 2 # type: int | ||
# TODO: SELF_VARIANCE = 2 # type: int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think? I am not sure what this is, For now simple INVARIANT seems to suffice There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. INVARIANT should be fine. I don't see a need for special variance. |
||
|
||
|
||
class TypeVarExpr(SymbolNode, Expression): | ||
|
@@ -2229,57 +2229,6 @@ def deserialize(cls, data: JsonDict) -> 'SymbolTable': | |
return st | ||
|
||
|
||
def function_type(func: FuncBase, fallback: 'mypy.types.Instance') -> 'mypy.types.FunctionLike': | ||
if func.type: | ||
assert isinstance(func.type, mypy.types.FunctionLike) | ||
return func.type | ||
else: | ||
# Implicit type signature with dynamic types. | ||
# Overloaded functions always have a signature, so func must be an ordinary function. | ||
fdef = cast(FuncDef, func) | ||
name = func.name() | ||
if name: | ||
name = '"{}"'.format(name) | ||
|
||
return mypy.types.CallableType( | ||
[mypy.types.AnyType()] * len(fdef.arg_names), | ||
fdef.arg_kinds, | ||
fdef.arg_names, | ||
mypy.types.AnyType(), | ||
fallback, | ||
name, | ||
implicit=True, | ||
) | ||
|
||
|
||
def method_type_with_fallback(func: FuncBase, | ||
fallback: 'mypy.types.Instance') -> 'mypy.types.FunctionLike': | ||
"""Return the signature of a method (omit self).""" | ||
return method_type(function_type(func, fallback)) | ||
|
||
|
||
def method_type(sig: 'mypy.types.FunctionLike') -> 'mypy.types.FunctionLike': | ||
if isinstance(sig, mypy.types.CallableType): | ||
return method_callable(sig) | ||
else: | ||
sig = cast(mypy.types.Overloaded, sig) | ||
items = [] # type: List[mypy.types.CallableType] | ||
for c in sig.items(): | ||
items.append(method_callable(c)) | ||
return mypy.types.Overloaded(items) | ||
|
||
|
||
def method_callable(c: 'mypy.types.CallableType') -> 'mypy.types.CallableType': | ||
if c.arg_kinds and c.arg_kinds[0] == ARG_STAR: | ||
# The signature is of the form 'def foo(*args, ...)'. | ||
# In this case we shouldn't drop the first arg, | ||
# since self will be absorbed by the *args. | ||
return c | ||
return c.copy_modified(arg_types=c.arg_types[1:], | ||
arg_kinds=c.arg_kinds[1:], | ||
arg_names=c.arg_names[1:]) | ||
|
||
|
||
class MroError(Exception): | ||
"""Raised if a consistent mro cannot be determined for a class.""" | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add docstring. In particular, describe
report_type
.