dry-python/classes

Special case for `tuple`

sobolevn opened this issue · 0 comments

When working with tuple, we need to be sure that it has the porper size. For example:

from classes import typeclass
from typing import Tuple, TypeVar

X = TypeVar('X')

@typeclass
def some(instance) -> str:
    ...

@some.instance(tuple)
def _some_tuple(instance: Tuple[X, X]) -> X:
    ...

This code should not be allowed, because Tuple[X, X] says that the size of instance will be 2. But, we cannot guarantee it.

Instead, this should be used:

from classes import typeclass
from typing import Tuple, TypeVar

X = TypeVar('X')

@typeclass
def some(instance) -> str:
    ...

@some.instance(tuple)
def _some_tuple(instance: Tuple[X, ...]) -> X:
    ...