Skip to content

Detect Any used as a metaclass #13599

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
sobolevn opened this issue Sep 3, 2022 · 0 comments · Fixed by #13605
Closed

Detect Any used as a metaclass #13599

sobolevn opened this issue Sep 3, 2022 · 0 comments · Fixed by #13605

Comments

@sobolevn
Copy link
Member

sobolevn commented Sep 3, 2022

Right now there's an inconsistency in how base classes / metaclasses behave when working with Any.

Example:

from typing import Any

MyAny: Any = type('MyAny', (type,), {'a': 1})

class Subclass(MyAny):
    pass

class WithMeta(metaclass=MyAny):
    pass

reveal_type(Subclass.a)  # N: Revealed type is "Any"
reveal_type(WithMeta.a)  # E: "Type[WithMeta]" has no attribute "a" \
                         # N: Revealed type is "Any"

Both Subclass.a and WithMeta.a return 1 in runtime. But, for some reason metaclass=Any is not recognised.

It even has an existing TODO item:

mypy/mypy/semanal.py

Lines 2076 to 2082 in dfbaff7

if isinstance(sym.node, Var) and isinstance(get_proper_type(sym.node.type), AnyType):
# 'Any' metaclass -- just ignore it.
#
# TODO: A better approach would be to record this information
# and assume that the type object supports arbitrary
# attributes, similar to an 'Any' base class.
return None, False

But, it was never implemented. I would love to fix it.

Related:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants