Skip to content

[WIP] Support metaclasses #2392

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 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 11 additions & 11 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,8 @@ def split_around_star(self, items: List[T], star_index: int,
return (left, star, right)

def type_is_iterable(self, type: Type) -> bool:
if isinstance(type, CallableType) and type.is_type_obj():
type = type.fallback
return (is_subtype(type, self.named_generic_type('typing.Iterable',
[AnyType()])) and
isinstance(type, Instance))
Expand Down Expand Up @@ -1817,11 +1819,14 @@ def analyze_iterable_item_type(self, expr: Expression) -> Type:
return joined
else:
# Non-tuple iterable.
self.check_subtype(iterable,
self.named_generic_type('typing.Iterable',
[AnyType()]),
expr, messages.ITERABLE_EXPECTED)

if isinstance(iterable, CallableType) and iterable.is_type_obj():
self.check_subtype(iterable.fallback,
self.named_generic_type('typing.Iterable', [AnyType()]),
expr, messages.ITERABLE_EXPECTED)
else:
self.check_subtype(iterable,
self.named_generic_type('typing.Iterable', [AnyType()]),
expr, messages.ITERABLE_EXPECTED)
echk = self.expr_checker
method = echk.analyze_external_member_access('__iter__', iterable,
expr)
Expand Down Expand Up @@ -1975,8 +1980,7 @@ def visit_yield_from_expr(self, e: YieldFromExpr) -> Type:
# by __iter__.
if isinstance(subexpr_type, AnyType):
iter_type = AnyType()
elif (isinstance(subexpr_type, Instance) and
is_subtype(subexpr_type, self.named_type('typing.Iterable'))):
elif self.type_is_iterable(subexpr_type):
if self.is_async_def(subexpr_type) and not self.has_coroutine_decorator(return_type):
self.msg.yield_from_invalid_operand_type(subexpr_type, e)
iter_method_type = self.expr_checker.analyze_external_member_access(
Expand Down Expand Up @@ -2243,10 +2247,6 @@ def lookup_typeinfo(self, fullname: str) -> TypeInfo:
sym = self.lookup_qualified(fullname)
return cast(TypeInfo, sym.node)

def type_type(self) -> Instance:
"""Return instance type 'type'."""
return self.named_type('builtins.type')

def object_type(self) -> Instance:
"""Return instance type 'object'."""
return self.named_type('builtins.object')
Expand Down
4 changes: 3 additions & 1 deletion mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ def analyze_member_access(name: str,
if result:
return result
fallback = builtin_type('builtins.type')
if item is not None:
fallback = item.type.metaclass_type or fallback
return analyze_member_access(name, fallback, node, is_lvalue, is_super,
is_operator, builtin_type, not_ready_callback, msg,
original_type=original_type, chk=chk)
Expand Down Expand Up @@ -438,7 +440,7 @@ def type_object_type(info: TypeInfo, builtin_type: Callable[[str], Instance]) ->
# Must be an invalid class definition.
return AnyType()
else:
fallback = builtin_type('builtins.type')
fallback = info.metaclass_type or builtin_type('builtins.type')
if init_method.info.fullname() == 'builtins.object':
# No non-default __init__ -> look at __new__ instead.
new_method = info.get_method('__new__')
Expand Down
4 changes: 4 additions & 0 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ def do_func_def(self, n: Union[ast35.FunctionDef, ast35.AsyncFunctionDef],

func_type = None
if any(arg_types) or return_type:
if len(arg_types) > len(arg_kinds):
raise FastParserError('Type signature has too many arguments', n.lineno, offset=0)
if len(arg_types) < len(arg_kinds):
raise FastParserError('Type signature has too few arguments', n.lineno, offset=0)
func_type = CallableType([a if a is not None else AnyType() for a in arg_types],
arg_kinds,
arg_names,
Expand Down
5 changes: 5 additions & 0 deletions mypy/fastparse2.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
print('The typed_ast package required by --fast-parser is only compatible with'
' Python 3.3 and greater.')
sys.exit(1)
from mypy.fastparse import FastParserError

T = TypeVar('T', bound=Union[ast27.expr, ast27.stmt])
U = TypeVar('U', bound=Node)
Expand Down Expand Up @@ -302,6 +303,10 @@ def visit_FunctionDef(self, n: ast27.FunctionDef) -> Statement:

func_type = None
if any(arg_types) or return_type:
if len(arg_types) > len(arg_kinds):
raise FastParserError('Type signature has too many arguments', n.lineno, offset=0)
if len(arg_types) < len(arg_kinds):
raise FastParserError('Type signature has too few arguments', n.lineno, offset=0)
func_type = CallableType([a if a is not None else AnyType() for a in arg_types],
arg_kinds,
arg_names,
Expand Down
23 changes: 23 additions & 0 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1920,6 +1920,29 @@ def __init__(self, names: 'SymbolTable', defn: ClassDef, module_name: str) -> No
for vd in defn.type_vars:
self.type_vars.append(vd.name)

_metaclass_type = None # type: Optional[mypy.types.Instance]

@property
def metaclass_type(self) -> 'Optional[mypy.types.Instance]':
if self._metaclass_type:
return self._metaclass_type
if self._fullname == 'builtins.type':
return mypy.types.Instance(self, [])
if self.mro is None:
# XXX why does this happen?
return None
if len(self.mro) > 1:
return self.mro[1].metaclass_type
# FIX: assert False
return None

@metaclass_type.setter
def metaclass_type(self, value: 'mypy.types.Instance'):
self._metaclass_type = value

def is_metaclass(self) -> bool:
return self.has_base('builtins.type')

def name(self) -> str:
"""Short name."""
return self.defn.name
Expand Down
4 changes: 4 additions & 0 deletions mypy/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ def parse_function(self, no_type_checks: bool = False) -> FuncDef:
else:
self.check_argument_kinds(arg_kinds, sig.arg_kinds,
def_tok.line, def_tok.column)
if len(sig.arg_types) > len(arg_kinds):
raise ParseError('Type signature has too many arguments')
if len(sig.arg_types) < len(arg_kinds):
raise ParseError('Type signature has too few arguments')
typ = CallableType(
sig.arg_types,
arg_kinds,
Expand Down
7 changes: 6 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,12 @@ def analyze_metaclass(self, defn: ClassDef) -> None:
self.fail("Dynamic metaclass not supported for '%s'" % defn.name, defn)
return
sym = self.lookup_qualified(defn.metaclass, defn)
if sym is not None and not isinstance(sym.node, TypeInfo):
if sym is not None and isinstance(sym.node, TypeInfo):
inst = fill_typevars(sym.node)
assert isinstance(inst, Instance)
defn.info.metaclass_type = inst
return
else:
self.fail("Invalid metaclass '%s'" % defn.metaclass, defn)

def object_type(self) -> Instance:
Expand Down
4 changes: 3 additions & 1 deletion mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ def __init__(self,
) -> None:
if variables is None:
variables = []
assert len(arg_types) == len(arg_kinds)
self.arg_types = arg_types
self.arg_kinds = arg_kinds
self.arg_names = arg_names
Expand Down Expand Up @@ -624,7 +625,8 @@ def copy_modified(self,
)

def is_type_obj(self) -> bool:
return self.fallback.type is not None and self.fallback.type.fullname() == 'builtins.type'
t = self.fallback.type
return t is not None and t.is_metaclass()

def is_concrete_type_obj(self) -> bool:
return self.is_type_obj() and self.is_classmethod_class
Expand Down
4 changes: 0 additions & 4 deletions test-data/unit/check-fastparse.test
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,12 @@ main: note: In function "f":
def f(): # E: Type signature has too many arguments
# type: (int) -> None
pass
[out]
main: note: In function "f":

[case testFasterParseTooFewArgumentsAnnotation]
# flags: --fast-parser
def f(x): # E: Type signature has too few arguments
# type: () -> None
pass
[out]
main: note: In function "f":

[case testFasterParseTypeCommentError_python2]
# flags: --fast-parser
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/lib-stub/abc.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class ABCMeta: pass
class ABCMeta(type): pass
abstractmethod = object()
abstractproperty = object()
8 changes: 6 additions & 2 deletions test-data/unit/semanal-errors.test
Original file line number Diff line number Diff line change
Expand Up @@ -983,13 +983,17 @@ class A(Generic[T], Generic[S]): pass \
[out]

[case testInvalidMetaclass]
class A(metaclass=x): pass # E: Name 'x' is not defined
class A(metaclass=x): pass
[out]
main:1: error: Name 'x' is not defined
main:1: error: Invalid metaclass 'x'

[case testInvalidQualifiedMetaclass]
import abc
class A(metaclass=abc.Foo): pass # E: Name 'abc.Foo' is not defined
class A(metaclass=abc.Foo): pass
[out]
main:2: error: Name 'abc.Foo' is not defined
main:2: error: Invalid metaclass 'abc.Foo'

[case testNonClassMetaclass]
def f(): pass
Expand Down