diff --git a/mockup/mockup.py b/mockup/mockup.py index 19eef9d..ba9ff89 100644 --- a/mockup/mockup.py +++ b/mockup/mockup.py @@ -6,12 +6,31 @@ # Simple example: Concrete types def add_one(number: int) -> int: + """ + Add 1 to an `int`, returning the sum. + + >>> add_one(9) + 10 + >>> add_one(-11) + -10 + >>> add_one(2**63-1) + 9223372036854775808 + """ return number + 1 # Union types. # Optional[T] == Union[T, None] def reciprocal(number: Union[int, float]) -> Optional[float]: + """ + Return the reciprocal of the given number. If the number is zero, return `None`. + + >>> reciprocal(5) + 0.2 + >>> reciprocal(-20) + -0.05 + >>> reciprocal(-0.0) # returns None + """ if number == 0: return None return 1 / number @@ -19,6 +38,15 @@ def reciprocal(number: Union[int, float]) -> Optional[float]: # Abstract types def flatten_ints(its: Iterable[Iterable[int]]) -> Iterable[int]: + """ + Given an iterable of iterables of ints, return an iterable of all the ints + in the inner iterables. + + >>> list(flatten_ints([[9, 11], [12], [4, 5]])) + [9, 11, 12, 4, 5] + >>> list(flatten_ints([[], (), set()])) + [] + """ for it in its: for i in it: yield i @@ -26,13 +54,32 @@ def flatten_ints(its: Iterable[Iterable[int]]) -> Iterable[int]: # Types generic over a TypeVar def flatten_generic(its: Iterable[Iterable[T]]) -> Iterable[T]: + """ + Given an iterable of iterables, return an iterable of all the inner + elements in the inner iterables. + + >>> list(flatten_ints(["hi", (4, 2.54)])) + ['h', 'i', 4, 2.54] + """ for it in its: for i in it: yield i class Circle: + """ + Circle(radius) -> Self + + A `Circle` represents the abstract geometric shape. + + >>> c = Circle(2.21); c.diameter + 4.42 + >>> c2 = Circle.from_circumference(100); round(c2.radius, 3) + 15.916 + """ + PI = 3.14159 + __slots__ = ["radius"] def __init__(self, radius: Union[int, float]): diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..df3eb51 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +addopts = --doctest-modules