MyPy gives false alarm on `mutagen.id3.PictureType`

127 Views Asked by At

I am writing some Python code (to manipulate audio metadata) using mutagen library. The library does not have type hints, however I have added type hints to my routines. In particular, I have a function with the following signature:

from mutagen import id3

def fun(arg1: str, arg2: id3.PictureType = id3.PictureType.ARTIST):
    # the body of the function 

But, MyPy complains about the default argument assignment:

error: Incompatible default for argument "arg2" (default has type "int", argument has type "PictureType")

I am confused and cannot figure out why this is happening. I checked that:

>>> isinstance(id3.PictureType.ARTIST, id3.PictureType)
True
>>> isinstance(id3.PictureType.ARTIST, int)
True
>>> issubclass(id3.PictureType.ARTIST.__class__, int)
True
>>> inspect.getmro(id3.PictureType.ARTIST.__class__)
(mutagen.id3._specs.PictureType, int, object)

So, it looks like the type hierarchy is correct (id3.PictureType.ARTIST should be an instance of id3.PictureType which, itself is a subclass of int). Yet, MyPy is assuming id3.PictureType.ARTIST is of type int, why?

Could that be related to the "particular" way mutagen author's implement id3.PictureType as an enum (see here and here)? And if so, is there a way to fix this (without re-implementing PictureType as a proper Python enum)?

0

There are 0 best solutions below