python / python/mypy

Configuration to turn off disjoint base checks for custom metaclasses

Open
#19,887 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimum example in the issue and reproduce the disjoint-base diagnostic for the custom metaclass. Trace how disjoint base checks produce the misc error, then determine whether configuration or a dedicated error code better scopes suppression for these classes. Done means the noisy diagnostics can be disabled without suppressing unrelated misc errors, with coverage for the example behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.