When I try to type hint my closure, I get worse suggestions than without:
from typing import Callable, Protocol
class _FormatterProtocol(Protocol):
def __call__(self, *values: object, sep: str = " ") -> str:
"""Apply ansi formatting to values."""
def _make_formatter(start: int, end: int):
def formatter(*values: object, sep: str = " ") -> str:
"""Apply ansi formatting to values."""
return f"\x1b[{start}m{sep.join(map(str, values))}\x1b[{end}m"
return formatter
yellow1 = _make_formatter(33, 39)
yellow2: Callable[..., str] = _make_formatter(33, 39)
yellow3: _FormatterProtocol = _make_formatter(33, 39)
Possible solutions:
def _formatter(*values: object, sep: str = " ") -> str:
"""Apply ansi formatting to values."""
raise NotImplementedError
class _FormatterCallable(Callable):
@abstractmethod
def __call__(self, *values: object, sep: str = " ") -> str:
"""Apply ansi formatting to values."""
class _FormatterCallableProtocol(CallableProtocol):
def __call__(self, *values: object, sep: str = " ") -> str:
"""Apply ansi formatting to values."""
yellow4: ((*values: object, sep: str = " ") -> str) = _make_formatter(33, 39)
yellow5: _formatter = _make_formatter(33, 39)
yellow6: _FormatterCallable = _make_formatter(33, 39)
yellow7: _FormatterCallableProtocol = _make_formatter(33, 39)
Or just: special case protocols with only a __call__()
method.