Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit19f6c2a

Browse files
committed
mymax: step 05
1 parent8151a4f commit19f6c2a

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
3+
fromfractionsimportFraction
4+
fromtypingimportTYPE_CHECKING,List,Optional
5+
6+
importmymaxasmy
7+
8+
defdemo_args_float()->None:
9+
a,b=2.5,3.5
10+
expected=b
11+
result=my.max(a,b)
12+
assertresult==expected
13+
print((a,b),'->',result)
14+
15+
16+
defdemo_args_fraction()->None:
17+
a,b=Fraction(5,8),Fraction(7,12)
18+
expected=a
19+
result=my.max(a,b)
20+
assertresult==expected
21+
print((a,b),'->',result)
22+
print(result.numerator,result.denominator,sep='/')
23+
ifTYPE_CHECKING:
24+
reveal_type(a)
25+
reveal_type(b)
26+
reveal_type(result)
27+
28+
29+
defdemo_args_str()->None:
30+
a,b='apple','banana'
31+
expected=b
32+
result=my.max(a,b)
33+
assertresult==expected
34+
print((a,b),'->',result)
35+
print(result.upper())
36+
ifTYPE_CHECKING:
37+
reveal_type(a)
38+
reveal_type(b)
39+
reveal_type(result)
40+
41+
###################################### intentional type errors
42+
43+
44+
45+
defmain():
46+
importsys
47+
forname,valinglobals().items():
48+
ifname.startswith('demo')or ('-e'insys.argvandname.startswith('error')):
49+
print('_'*20,name)
50+
val()
51+
52+
if__name__=='__main__':
53+
main()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
fromtypingimportTypeVar,Protocol,Any
2+
3+
4+
classSupportsLessThan(Protocol):
5+
def__lt__(self,other:Any)->bool: ...
6+
7+
8+
LessT=TypeVar('LessT',bound=SupportsLessThan)
9+
10+
11+
defmax(a:LessT,b:LessT)->LessT:
12+
ifb<a:# refactored to use <
13+
returna
14+
returnb
15+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
fromdecimalimportDecimal
2+
fromfractionsimportFraction
3+
fromtypingimportTypeVar
4+
5+
importmymax
6+
frommymaximportLessT
7+
8+
importpytest
9+
10+
11+
NumberT=TypeVar('NumberT',float,Decimal,Fraction)
12+
13+
14+
@pytest.mark.parametrize(
15+
'a, b, expected',
16+
[
17+
(1,2,2),
18+
(-3,-1,-1),
19+
(4,4,4),
20+
],
21+
)
22+
deftest_two_ints(a:int,b:int,expected:int)->None:
23+
result=mymax.max(a,b)
24+
assertresult==expected
25+
26+
27+
deftest_two_numbers()->None:
28+
result=mymax.max(Fraction(1,3),Fraction(1,4))
29+
assertresult==Fraction(1,3)
30+
31+
32+
@pytest.mark.parametrize(
33+
'a, b, expected',
34+
[
35+
(1,2,2),
36+
(0.1,0.01,0.1),
37+
(Fraction(1,3),Fraction(1,2),Fraction(1,2)),
38+
(Decimal('-1.3'),Decimal('-1.2'),Decimal('-1.2')),
39+
],
40+
)
41+
deftest_two_numbers_params(a:NumberT,b:NumberT,expected:NumberT)->None:
42+
result=mymax.max(a,b)
43+
assertresult==expected
44+
45+
46+
deftest_two_comparables()->None:
47+
result=mymax.max('a','B')
48+
assertresult=='a'
49+
50+
51+
@pytest.mark.parametrize(
52+
'a, b, expected',
53+
[
54+
('ab','ac','ac'),
55+
([1,2,3], [1,3], [1,3]),
56+
({1,2,3}, {1,3}, {1,2,3}),
57+
],
58+
)
59+
deftest_two_comparables_params(a:LessT,b:LessT,expected:LessT)->None:
60+
result=mymax.max(a,b)
61+
assertresult==expected
62+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[mypy]
2+
python_version = 3.9
3+
warn_unused_configs = True
4+
disallow_incomplete_defs = True
5+
[mypy-pytest]
6+
ignore_missing_imports = True

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp