Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to avoid mypy complaints when inheriting from a built-in collection type?

Running mypy on code like this

class MySpecialList(list):
   # funky extra functionality

gives me

my_file.py:42: error: Missing type parameters for generic type "list"  [type-arg]

I can avoid this by not inheriting from list at all or ignoring this error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But how should I deal with this? Isn’t this a bug in mypy?

>Solution :

This is the result of using --disallow-any-generics (which is implied by --strict), as list is equivalent to list[Any]. You can fix it by making MySpecialist explicitly generic via a type variable.

from typing import TypeVar


T = TypeVar('T')


class MySpecialList(list[T]):
    ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading