-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
Feature
mypy recently added support for @disjoint_base
.
This is a feature request for mypy to add a configuration option for mypy config files to turn off disjoint base checks, or dedicate a mypy error code for the disjoint base check (currently it just uses the error code misc
), for classes created with user-defined metaclasses.
Pitch
Currently, mypy doesn't check __init_subclass__
signatures with custom metaclasses because arguments can be added or removed when passing up the MRO of builtins.type
constructors.
Since custom metaclasses can manipulate both __slots__
and the class bases that get passed up to builtins.type.__new__
, disjoint base checks should also be suppressed (or configured to be suppressible).
I'm currently working with code that does such manipulation, and the errors are fairly noisy, with the only recourse to # type: ignore[misc]
on all subclass definitions; having a misc
ignored at the module- or configuration-level suppresses far too many other useful errors. Here's a minimum example that runs at runtime to illustrate:
# mypy: disable-error-code=empty-body
from __future__ import annotations
def all_bases_have_same_slots(bases: tuple[type[object], ...], /) -> bool:
return True
def recreate_base_with_empty_slots(B: type[object], /) -> type[object]:
return type(B.__name__, B.__bases__, {**{k: v for k, v in B.__dict__.items() if k not in object.__dict__}, "__slots__": ()})
class M(type):
def __new__(mcs, name: str, bases: tuple[type[object], ...], namespace: dict[str, object], /) -> M:
if all_bases_have_same_slots(bases):
namespace["__slots__"] = getattr(bases[0], "__slots__")
bases = tuple(recreate_base_with_empty_slots(B) for B in bases)
return super().__new__(mcs, name, bases, namespace)
class A1:
__slots__ = ("a",)
class A2:
__slots__ = ("a",)
class C(A1, A2, metaclass=M): # E: class "C" has incompatible disjoint bases {misc]
pass