Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
dont use set. fail for metaclass(Tuple)
  • Loading branch information
elazarg committed Feb 7, 2017
commit b083a9748f7b01564e8142abc734c87989e68bd2
2 changes: 1 addition & 1 deletion mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2015,7 +2015,7 @@ def calculate_metaclass_type(self) -> 'Optional[mypy.types.Instance]':
return declared
if self._fullname == 'builtins.type':
return mypy.types.Instance(self, [])
candidates = {s.declared_metaclass for s in self.mro} - {None}
candidates = [s.declared_metaclass for s in self.mro if s.declared_metaclass is not None]
for c in candidates:
if all(other.type in c.type.mro for other in candidates):
return c
Expand Down
6 changes: 2 additions & 4 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,13 +908,11 @@ def analyze_metaclass(self, defn: ClassDef) -> None:
if sym is None:
# Probably a name error - it is already handled elsewhere
return
if not isinstance(sym.node, TypeInfo):
if not isinstance(sym.node, TypeInfo) or sym.node.tuple_type is not None:
self.fail("Invalid metaclass '%s'" % defn.metaclass, defn)
return
inst = fill_typevars(sym.node)
if not isinstance(inst, Instance):
self.fail("Invalid metaclass '%s'" % defn.metaclass, defn)
return
assert isinstance(inst, Instance)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can trigger this assert as follows:

class M(Tuple[int]): pass
class C(metaclass=M): pass

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be an error then. Do you agree? I don't want to go silently to a fallback,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you can add the check to line 911.

defn.info.declared_metaclass = inst
defn.info.metaclass_type = defn.info.calculate_metaclass_type()
if defn.info.metaclass_type is None:
Expand Down
2 changes: 0 additions & 2 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -2781,8 +2781,6 @@ class M(type):

class A(metaclass=M): pass

reveal_type(A.x) # E: Revealed type is 'builtins.int'

def f(TA: Type[A]):
reveal_type(TA) # E: Revealed type is 'Type[__main__.A]'
reveal_type(TA.x) # E: Revealed type is 'builtins.int'
Expand Down