[Python] Type hinting of subclass

Discussão em 'Python' iniciado por Stack, Fevereiro 12, 2025.

  1. Stack

    Stack Membro Participativo

    Given a subclass that overrides attributes and methods, is there a convention in Python related to type hinting explicit params (in the case you forgo using *args and/or **kwargs) and return types that are already type hinted in a base class? Meaning is there the expectation that subclasses redundantly type hint overridden attributes and methods?

    For example:

    class Base:
    def __init__(self, name: str) -> None:
    self.name = name

    def get_greeting(self) -> str:
    raise NotImplementedError


    class Derived(Base):
    # is there a convention related to including the return type hint of
    # this overriden method since it's already included in the base class?
    def get_greeting(self) -> str:
    return f"My name is {self.name}.

    Continue reading...

Compartilhe esta Página