Skip to content

False positive error when subclassing List type #15040

Closed

Description

@thepabloaguilar

Bug Report

This bug is experienced in classes lib

To Reproduce

from typing import List


class ListOfStrMeta(type):
    def __instancecheck__(cls, other) -> bool:
        return (
            isinstance(other, list) and
            bool(other) and
            all(isinstance(list_item, str) for list_item in other)
        )


class ListOfStr(List[str], metaclass=ListOfStrMeta):
    ...

Expected Behavior

No errors should appear

Actual Behavior

Error from the code above:

poetry run mypy ex.py
ex.py:13: error: Metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases  [misc]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.2.0
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
# Plugins, includes custom:
plugins =
  classes.contrib.mypy.classes_plugin

disable_error_code = empty-body

allow_redefinition = false
check_untyped_defs = true
disallow_any_explicit = true
# disallow_any_generics = true
disallow_untyped_calls = true
ignore_errors = false
ignore_missing_imports = true
implicit_reexport = false
local_partial_types = true
strict_optional = true
strict_equality = true
no_implicit_optional = true
warn_no_return = true
warn_unused_ignores = true
warn_redundant_casts = true
warn_unused_configs = true
warn_unreachable = true
  • Python version used: 3.10.10

cc @sobolevn

Activity

  1. ikonst commented on Apr 12, 2023

    @ikonst
    Contributor

    This is probably due to #6042:

    mypy/mypy/semanal.py

    Lines 2363 to 2372 in 0f09be4

    if any(info.is_protocol for info in defn.info.mro):
    if (
    not defn.info.metaclass_type
    or defn.info.metaclass_type.type.fullname == "builtins.type"
    ):
    # All protocols and their subclasses have ABCMeta metaclass by default.
    # TODO: add a metaclass conflict check if there is another metaclass.
    abc_meta = self.named_type_or_none("abc.ABCMeta", [])
    if abc_meta is not None: # May be None in tests with incomplete lib-stub.
    defn.info.metaclass_type = abc_meta

    Can take a look @ilevkivskyi?

    Since list derives from a handful of protocols (e.g. Sequence), its metaclass is ABCMeta.

    Changing

    -class ListOfStrMeta(typing):
    +class ListOfStrMeta(ABCMeta):

    eliminates the error, though it's probably wrong since type(list) is type.

  2. sobolevn commented on Apr 12, 2023

    @sobolevn
    Member

    Yes, this is due to the fact that typeshed has Sequence in the base classes of list.
    While runtime does not have it.

  3. hauntsaninja commented on Jun 20, 2025

    @hauntsaninja
    Collaborator

    #17713 makes the error here go away

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions