mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Mypy doesn't detect new metaclass on subclass

Open sshane opened this issue 1 year ago • 0 comments

Bug Report

If you subclass a class with a metaclass but change the metaclass to your own with a different method signature, mypy does not pick up on that.

To Reproduce

Example:

from enum import EnumType, StrEnum as _StrEnum, auto

class EnumMeta(EnumType):
  def __call__(cls, value=None, **kwargs):
    # Return first enum if value is None
    if value is None:
      value = next(iter(cls))
    return super().__call__(value, **kwargs)


class StrEnum(_StrEnum, metaclass=EnumMeta):
  @staticmethod
  def _generate_next_value_(name, *args):
    # auto() defaults to name.lower()
    return name


class FingerprintSource(StrEnum):
  can = auto()
  fw = auto()
  fixed = auto()


FingerprintSource()

Out: <FingerprintSource.can: 'can'>

Expected Behavior

Mypy passes

Actual Behavior

selfdrive/car/data_structures.py:310: error: Missing positional argument "value" in call to "FingerprintSource"  [call-arg]

Your Environment

mypy 1.11.1 (compiled: yes) Python 3.11.4

sshane avatar Aug 09 '24 22:08 sshane