Skip to content
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

False positive error when subclassing List type #15040

Open
thepabloaguilar opened this issue Apr 12, 2023 · 2 comments
Open

False positive error when subclassing List type #15040

thepabloaguilar opened this issue Apr 12, 2023 · 2 comments
Labels
bug mypy got something wrong

Comments

@thepabloaguilar
Copy link

thepabloaguilar commented Apr 12, 2023

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

@thepabloaguilar thepabloaguilar added the bug mypy got something wrong label Apr 12, 2023
@ikonst
Copy link
Contributor

ikonst commented Apr 12, 2023

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.

@sobolevn
Copy link
Member

sobolevn commented Apr 12, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants