[Python] Python type hints - specify key, value type for dict subclass

Discussão em 'Python' iniciado por Stack, Julho 10, 2025.

  1. Stack

    Stack Membro Participativo

    Say I have

    class A(dict): pass


    Is there any way to specify the type of the key and values of A ? Can it be done in a way that the types are inherited - so class B(A) would inherit the type values for, say, key and would be able to override the type of value ?

    Ideally this would be done via the type hints introduced in pep-0484 - but note I am on python 2 so I would need a solution with type comments. If however this is not possible a solution involving metaclasses or any other hack would be acceptable.

    Continue reading...

Compartilhe esta Página