[Python] Instantiate empty type-hinted list [duplicate]

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

  1. Stack

    Stack Membro Participativo

    I have the following code:

    from typing import List, NewType

    MultiList = NewType("MultiList", List[List[int]])

    def myfunc():
    multi: MultiList = []
    # More stuff here


    The code works fine, it's just my IDE (PyCharm) doesn't like the instantiation of multi to an empty list, I get this error:


    "Expected type 'MultiList', got 'list[list[int]]' instead"
    ​

    I mean, a MultiList is a list[list[int]], so I really don't know why it's complaining. Unless it's because the list is empty, but that doesn't make a lot of sense to me either.

    It's not the end of the world, the code works just fine, I'd just like to know why it's marked as wrong.

    Continue reading...

Compartilhe esta Página