Skip to content

Commit 2770288

Browse files
authored
Add doctest (#2)
Add doctests
1 parent f23daf7 commit 2770288

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

mockup/mockup.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,80 @@
66

77
# Simple example: Concrete types
88
def add_one(number: int) -> int:
9+
"""
10+
Add 1 to an `int`, returning the sum.
11+
12+
>>> add_one(9)
13+
10
14+
>>> add_one(-11)
15+
-10
16+
>>> add_one(2**63-1)
17+
9223372036854775808
18+
"""
919
return number + 1
1020

1121

1222
# Union types.
1323
# Optional[T] == Union[T, None]
1424
def reciprocal(number: Union[int, float]) -> Optional[float]:
25+
"""
26+
Return the reciprocal of the given number. If the number is zero, return `None`.
27+
28+
>>> reciprocal(5)
29+
0.2
30+
>>> reciprocal(-20)
31+
-0.05
32+
>>> reciprocal(-0.0) # returns None
33+
"""
1534
if number == 0:
1635
return None
1736
return 1 / number
1837

1938

2039
# Abstract types
2140
def flatten_ints(its: Iterable[Iterable[int]]) -> Iterable[int]:
41+
"""
42+
Given an iterable of iterables of ints, return an iterable of all the ints
43+
in the inner iterables.
44+
45+
>>> list(flatten_ints([[9, 11], [12], [4, 5]]))
46+
[9, 11, 12, 4, 5]
47+
>>> list(flatten_ints([[], (), set()]))
48+
[]
49+
"""
2250
for it in its:
2351
for i in it:
2452
yield i
2553

2654

2755
# Types generic over a TypeVar
2856
def flatten_generic(its: Iterable[Iterable[T]]) -> Iterable[T]:
57+
"""
58+
Given an iterable of iterables, return an iterable of all the inner
59+
elements in the inner iterables.
60+
61+
>>> list(flatten_ints(["hi", (4, 2.54)]))
62+
['h', 'i', 4, 2.54]
63+
"""
2964
for it in its:
3065
for i in it:
3166
yield i
3267

3368

3469
class Circle:
70+
"""
71+
Circle(radius) -> Self
72+
73+
A `Circle` represents the abstract geometric shape.
74+
75+
>>> c = Circle(2.21); c.diameter
76+
4.42
77+
>>> c2 = Circle.from_circumference(100); round(c2.radius, 3)
78+
15.916
79+
"""
80+
3581
PI = 3.14159
82+
3683
__slots__ = ["radius"]
3784

3885
def __init__(self, radius: Union[int, float]):

pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
addopts = --doctest-modules

0 commit comments

Comments
 (0)