Skip to content

Commit 9b41655

Browse files
committed
Treat Any metaclass the same as Any base class, refs python#13599
1 parent 130e1a4 commit 9b41655

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

mypy/semanal.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,12 +2074,12 @@ def get_declared_metaclass(
20742074
# Probably a name error - it is already handled elsewhere
20752075
return None, False
20762076
if isinstance(sym.node, Var) and isinstance(get_proper_type(sym.node.type), AnyType):
2077-
# 'Any' metaclass -- just ignore it.
2078-
#
2079-
# TODO: A better approach would be to record this information
2080-
# and assume that the type object supports arbitrary
2081-
# attributes, similar to an 'Any' base class.
2082-
return None, False
2077+
# Create a fake TypeInfo that fallbacks to `Any`, basically allowing
2078+
# all the attributes. Same thing as we do for `Any` base class.
2079+
any_info = self.make_empty_type_info(ClassDef(sym.node.name, Block([])))
2080+
any_info.fallback_to_any = True
2081+
any_info._fullname = sym.node.fullname
2082+
return Instance(any_info, []), False
20832083
if isinstance(sym.node, PlaceholderNode):
20842084
return None, True # defer later in the caller
20852085

test-data/unit/check-classes.test

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4392,6 +4392,17 @@ def f(TB: Type[B]):
43924392
reveal_type(TB) # N: Revealed type is "Type[__main__.B]"
43934393
reveal_type(TB.x) # N: Revealed type is "builtins.int"
43944394

4395+
[case testMetaclassAsAny]
4396+
from typing import Any, ClassVar
4397+
4398+
MyAny: Any
4399+
class WithMeta(metaclass=MyAny):
4400+
x: ClassVar[int]
4401+
4402+
reveal_type(WithMeta.a) # N: Revealed type is "Any"
4403+
reveal_type(WithMeta.m) # N: Revealed type is "Any"
4404+
reveal_type(WithMeta.x) # N: Revealed type is "builtins.int"
4405+
43954406
[case testMetaclassIterable]
43964407
from typing import Iterable, Iterator
43974408

@@ -4476,15 +4487,7 @@ from missing import M
44764487
class A(metaclass=M):
44774488
y = 0
44784489
reveal_type(A.y) # N: Revealed type is "builtins.int"
4479-
A.x # E: "Type[A]" has no attribute "x"
4480-
4481-
[case testAnyMetaclass]
4482-
from typing import Any
4483-
M = None # type: Any
4484-
class A(metaclass=M):
4485-
y = 0
4486-
reveal_type(A.y) # N: Revealed type is "builtins.int"
4487-
A.x # E: "Type[A]" has no attribute "x"
4490+
reveal_type(A.x) # N: Revealed type is "Any"
44884491

44894492
[case testValidTypeAliasAsMetaclass]
44904493
from typing_extensions import TypeAlias

0 commit comments

Comments
 (0)