Narrowing down type hints in children


I was wondering if putting tighter constraints on the type hints in child classes is bad practice or not. Consider the following example

from typing import List, Tuplefrom abc import abstractmethodclass Foo:    @abstractmethod    def print_stuff(self, items: List[str]):        """print some list."""class Bar(Foo):    def print_stuff(self, items: Tuple[str, str]):        print(f"Items: {items}, always 2.")        class Baz(Foo):    def print_stuff(self, items: Tuple[str, str, str]):            print(f"Items: {items}, always 3.")

Here the base class expects some list of strings. In Bar the method only allows exactly two strings, and in Baz exactly three. Is this increased restriction bad practice?
Thank you!



Source link

Post a Comment

Previous Post Next Post