adding sentinel metaclass example
This commit is contained in:
60
15-more-types/mymax/mymax.py
Normal file
60
15-more-types/mymax/mymax.py
Normal file
@@ -0,0 +1,60 @@
|
||||
# tag::MYMAX_TYPES[]
|
||||
from typing import Protocol, Any, TypeVar, overload, Callable, Iterable, Union
|
||||
|
||||
class SupportsLessThan(Protocol):
|
||||
def __lt__(self, other: Any) -> bool: ...
|
||||
|
||||
T = TypeVar('T')
|
||||
LT = TypeVar('LT', bound=SupportsLessThan)
|
||||
DT = TypeVar('DT')
|
||||
|
||||
MISSING = object()
|
||||
EMPTY_MSG = 'max() arg is an empty sequence'
|
||||
|
||||
@overload
|
||||
def max(__arg1: LT, __arg2: LT, *_args: LT, key: None = ...) -> LT:
|
||||
...
|
||||
@overload
|
||||
def max(__arg1: T, __arg2: T, *_args: T, key: Callable[[T], LT]) -> T:
|
||||
...
|
||||
@overload
|
||||
def max(__iterable: Iterable[LT], *, key: None = ...) -> LT:
|
||||
...
|
||||
@overload
|
||||
def max(__iterable: Iterable[T], *, key: Callable[[T], LT]) -> T:
|
||||
...
|
||||
@overload
|
||||
def max(__iterable: Iterable[LT], *, key: None = ...,
|
||||
default: DT) -> Union[LT, DT]:
|
||||
...
|
||||
@overload
|
||||
def max(__iterable: Iterable[T], *, key: Callable[[T], LT],
|
||||
default: DT) -> Union[T, DT]:
|
||||
...
|
||||
# end::MYMAX_TYPES[]
|
||||
# tag::MYMAX[]
|
||||
def max(first, *args, key=None, default=MISSING):
|
||||
if args:
|
||||
series = args
|
||||
candidate = first
|
||||
else:
|
||||
series = iter(first)
|
||||
try:
|
||||
candidate = next(series)
|
||||
except StopIteration:
|
||||
if default is not MISSING:
|
||||
return default
|
||||
raise ValueError(EMPTY_MSG) from None
|
||||
if key is None:
|
||||
for current in series:
|
||||
if candidate < current:
|
||||
candidate = current
|
||||
else:
|
||||
candidate_key = key(candidate)
|
||||
for current in series:
|
||||
current_key = key(current)
|
||||
if candidate_key < current_key:
|
||||
candidate = current
|
||||
candidate_key = current_key
|
||||
return candidate
|
||||
# end::MYMAX[]
|
||||
128
15-more-types/mymax/mymax_demo.py
Normal file
128
15-more-types/mymax/mymax_demo.py
Normal file
@@ -0,0 +1,128 @@
|
||||
from typing import TYPE_CHECKING, List, Optional
|
||||
|
||||
import mymax as my
|
||||
|
||||
def demo_args_list_float() -> None:
|
||||
args = [2.5, 3.5, 1.5]
|
||||
expected = 3.5
|
||||
result = my.max(*args)
|
||||
print(args, expected, result, sep='\n')
|
||||
assert result == expected
|
||||
if TYPE_CHECKING:
|
||||
reveal_type(args)
|
||||
reveal_type(expected)
|
||||
reveal_type(result)
|
||||
|
||||
def demo_args_iter_int() -> None:
|
||||
args = [30, 10, 20]
|
||||
expected = 30
|
||||
result = my.max(args)
|
||||
print(args, expected, result, sep='\n')
|
||||
assert result == expected
|
||||
if TYPE_CHECKING:
|
||||
reveal_type(args)
|
||||
reveal_type(expected)
|
||||
reveal_type(result)
|
||||
|
||||
|
||||
def demo_args_iter_str() -> None:
|
||||
args = iter('banana kiwi mango apple'.split())
|
||||
expected = 'mango'
|
||||
result = my.max(args)
|
||||
print(args, expected, result, sep='\n')
|
||||
assert result == expected
|
||||
if TYPE_CHECKING:
|
||||
reveal_type(args)
|
||||
reveal_type(expected)
|
||||
reveal_type(result)
|
||||
|
||||
|
||||
def demo_args_iter_not_comparable_with_key() -> None:
|
||||
args = [object(), object(), object()]
|
||||
key = id
|
||||
expected = max(args, key=id)
|
||||
result = my.max(args, key=key)
|
||||
print(args, key, expected, result, sep='\n')
|
||||
assert result == expected
|
||||
if TYPE_CHECKING:
|
||||
reveal_type(args)
|
||||
reveal_type(key)
|
||||
reveal_type(expected)
|
||||
reveal_type(result)
|
||||
|
||||
|
||||
def demo_empty_iterable_with_default() -> None:
|
||||
args: List[float] = []
|
||||
default = None
|
||||
expected = None
|
||||
result = my.max(args, default=default)
|
||||
print(args, default, expected, result, sep='\n')
|
||||
assert result == expected
|
||||
if TYPE_CHECKING:
|
||||
reveal_type(args)
|
||||
reveal_type(default)
|
||||
reveal_type(expected)
|
||||
reveal_type(result)
|
||||
|
||||
|
||||
def demo_different_key_return_type() -> None:
|
||||
args = iter('banana kiwi mango apple'.split())
|
||||
key = len
|
||||
expected = 'banana'
|
||||
result = my.max(args, key=key)
|
||||
print(args, key, expected, result, sep='\n')
|
||||
assert result == expected
|
||||
if TYPE_CHECKING:
|
||||
reveal_type(args)
|
||||
reveal_type(key)
|
||||
reveal_type(expected)
|
||||
reveal_type(result)
|
||||
|
||||
|
||||
def demo_different_key_none() -> None:
|
||||
args = iter('banana kiwi mango apple'.split())
|
||||
key = None
|
||||
expected = 'mango'
|
||||
result = my.max(args, key=key)
|
||||
print(args, key, expected, result, sep='\n')
|
||||
assert result == expected
|
||||
if TYPE_CHECKING:
|
||||
reveal_type(args)
|
||||
reveal_type(key)
|
||||
reveal_type(expected)
|
||||
reveal_type(result)
|
||||
|
||||
###################################### intentional type errors
|
||||
|
||||
def error_reported_bug() -> None:
|
||||
# example from https://github.com/python/typeshed/issues/4051
|
||||
top: Optional[int] = None
|
||||
try:
|
||||
my.max(5, top)
|
||||
except TypeError as exc:
|
||||
print(exc)
|
||||
|
||||
|
||||
def error_args_iter_not_comparable() -> None:
|
||||
try:
|
||||
my.max([None, None])
|
||||
except TypeError as exc:
|
||||
print(exc)
|
||||
|
||||
|
||||
def error_single_arg_not_iterable() -> None:
|
||||
try:
|
||||
my.max(1)
|
||||
except TypeError as exc:
|
||||
print(exc)
|
||||
|
||||
###################################### run demo and error functions
|
||||
|
||||
def main():
|
||||
for name, val in globals().items():
|
||||
if name.startswith('demo') or name.startswith('error'):
|
||||
print('_' * 20, name)
|
||||
val()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
69
15-more-types/mymax/mymax_test.py
Normal file
69
15-more-types/mymax/mymax_test.py
Normal file
@@ -0,0 +1,69 @@
|
||||
from typing import List, Callable
|
||||
|
||||
import pytest # type: ignore
|
||||
|
||||
import mymax as my
|
||||
|
||||
@pytest.fixture
|
||||
def fruits():
|
||||
return 'banana kiwi mango apple'.split()
|
||||
|
||||
@pytest.mark.parametrize('args, expected', [
|
||||
([1, 3], 3),
|
||||
([3, 1], 3),
|
||||
([30, 10, 20], 30),
|
||||
])
|
||||
def test_max_args(args, expected):
|
||||
result = my.max(*args)
|
||||
assert result == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('iterable, expected', [
|
||||
([7], 7),
|
||||
([1, 3], 3),
|
||||
([3, 1], 3),
|
||||
([30, 10, 20], 30),
|
||||
])
|
||||
def test_max_iterable(iterable, expected):
|
||||
result = my.max(iterable)
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_max_single_arg_not_iterable():
|
||||
msg = "'int' object is not iterable"
|
||||
with pytest.raises(TypeError) as exc:
|
||||
my.max(1)
|
||||
assert exc.value.args[0] == msg
|
||||
|
||||
|
||||
def test_max_empty_iterable_no_default():
|
||||
with pytest.raises(ValueError) as exc:
|
||||
my.max([])
|
||||
assert exc.value.args[0] == my.EMPTY_MSG
|
||||
|
||||
|
||||
@pytest.mark.parametrize('iterable, default, expected', [
|
||||
([7], -1, 7),
|
||||
([], -1, -1),
|
||||
([], None, None),
|
||||
])
|
||||
def test_max_empty_iterable_with_default(iterable, default, expected):
|
||||
result = my.max(iterable, default=default)
|
||||
assert result == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key, expected', [
|
||||
(None, 'mango'),
|
||||
(lambda x: x, 'mango'),
|
||||
(len, 'banana'),
|
||||
(lambda s: -len(s), 'kiwi'),
|
||||
(lambda s: -ord(s[0]), 'apple'),
|
||||
(lambda s: ord(s[-1]), 'mango'),
|
||||
])
|
||||
def test_max_iterable_with_key(
|
||||
fruits: List[str],
|
||||
key: Callable[[str], str],
|
||||
expected: str
|
||||
) -> None:
|
||||
result = my.max(fruits, key=key)
|
||||
assert result == expected
|
||||
Reference in New Issue
Block a user