Description
The attached program has three methods (C.m
, C.m_bad1
, C.m_bad2
) that compute the same value; only one of them is accepted by mypy (and by sheer coincidence, C.m
is the style that Guido prefers and the others are a style he doesn't). I looked at the typeshed definition of functools.reduce
,and it appears to be correct.
Issue 4226 suggests that the lambda
might be the problem; but that seems to only shift the problem (C.m_bad
1 uses a lambda
and C.m_bad2
has the lambda
rewritten as a function.
The error messages go away when I change result = ...
to result: List[B] = ...
, but that type annotation shouldn't be necessary, from looking at the definition of reduce
) (In my original program, I got additional error messages, so I might have over-simplified this example.)
Overloading of reduce
in functools.pyi
doesn't seem to be the problem -- I tried using my own definition of reduce
but without the overloading, and got the same error messages.
(For background: this example is a simplified version of some code that converts objects of class C
to objects of class B
, with both classes having many subclasses.)
Here are the error messages I got (program is attached as a TXT file):
reduce_bug.py: note: In member "m_bad1" of class "C":
reduce_bug.py:37: error: Need type annotation for 'result'
reduce_bug.py:37: error: Unsupported operand types for + ("List[<nothing>]" and "List[B]")
reduce_bug.py: note: In member "m_bad2" of class "C":
reduce_bug.py:42: error: Need type annotation for 'result'
reduce_bug.py:42: error: Argument 1 to "reduce" has incompatible type "Callable[[List[B], C], List[B]]"; expected "Callable[[List[<nothing>], C], List[<nothing>]]"
This was run with mypy --python-version=3.6 --strict-optional --check-untyped-defs --warn-incomplete-stub --warn-no-return --no-incremental --disallow-any-unimported --show-error-context --implicit-optional --strict --disallow-incomplete-defs /tmp/reduce_bug