[Python] How to override returning type hint of inheritance class?

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

  1. Stack

    Stack Membro Participativo

    I'm able to overload a type hint of a method in subclass without its implementing. I tried something like this:

    from typing import Any, overload


    class A:
    def __init__(self, param) -> None:
    self.param = param

    def get(self) -> Any:
    return self.param


    class B(A):
    def __init__(self, param) -> None:
    super().__init__(param)

    @overload
    def get(self) -> str: ... # Changed a returning type hint here


    test = B('something')
    test.get() # NotImplementedError: You should not call an overloaded function. A series of @overload-decorated functions outside a stub module should always be followed by an implementation that is not @overload-ed.


    But it doesn't work.

    All I want to do is just specify the types without writing any code for the method itself. What should I do in this case?

    Continue reading...

Compartilhe esta Página