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

Commit1e4def5

Browse files
committed
mymax: mysterious mypy error with optional callable arg
1 parent3bf9fb0 commit1e4def5

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

‎less_than/max_steps/04_multi_args_key/mymax.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ def __lt__(self, other: Any) -> bool:
1919

2020
T=TypeVar('T')
2121
LessT=TypeVar('LessT',bound=SupportsLessThan)
22-
KeyFunc=Callable[[T],LessT]
2322

2423
@overload
2524
defmax(first:LessT,*rest:LessT,key:None= ...)->LessT:
2625
...
2726
@overload
28-
defmax(first:T,*rest:T,key:KeyFunc)->T:
27+
defmax(first:T,*rest:T,key:Callable[[T],LessT])->T:
2928
...
3029
defmax(first,*rest,key=None):
3130
candidate=first

‎less_than/max_steps/04_multi_args_key/mymax_test.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
fromtypingimportTypeVar,Iterable,Optional,Callable
44

55
importmymax
6-
frommymaximportLessT,KeyFunc
7-
6+
frommymaximportLessT
87
importpytest
98

109

@@ -77,6 +76,13 @@ def test_multi_params(
7776
assertresult==expected
7877

7978

79+
deftest_two_complex():
80+
a=5+4j
81+
b=3+5j
82+
result=mymax.max(a,b,key=abs)
83+
assertresult==a
84+
85+
8086
@pytest.mark.parametrize(
8187
'first, rest, key, expected',
8288
[
@@ -89,15 +95,12 @@ def test_multi_params(
8995
deftest_multi_with_key_params(
9096
first:LessT,
9197
rest:Iterable[LessT],
92-
key:Optional[KeyFunc],
98+
key:Optional[Callable[[T],LessT]],
9399
expected:LessT,
94100
)->None:
95101
result=mymax.max(first,*rest,key=key)
96102
assertresult==expected
97103

98-
99-
deftest_two_complex():
100-
a=5+4j
101-
b=3+5j
102-
result=mymax.max(a,b,key=abs)
103-
assertresult==a
104+
# mysterious mypy error:
105+
# mymax_test.py:101: error: Argument "key" to "max" has incompatible
106+
# type "Optional[Callable[[T], LessT]]"; expected "None"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp