zero323 commented on a change in pull request #34363:
URL: https://github.com/apache/spark/pull/34363#discussion_r760208707
##########
File path: python/pyspark/accumulators.py
##########
@@ -176,44 +193,44 @@ class AccumulatorParam(object):
[7.0, 8.0, 9.0]
"""
- def zero(self, value):
+ def zero(self, value: T) -> T:
"""
Provide a "zero value" for the type, compatible in dimensions with the
provided `value` (e.g., a zero vector)
"""
raise NotImplementedError
- def addInPlace(self, value1, value2):
+ def addInPlace(self, value1: T, value2: T) -> T:
"""
Add two values of the accumulator's data type, returning a new value;
for efficiency, can also update `value1` in place and return it.
"""
raise NotImplementedError
-class AddingAccumulatorParam(AccumulatorParam):
+class AddingAccumulatorParam(AccumulatorParam[U]):
"""
An AccumulatorParam that uses the + operators to add values. Designed for
simple types
such as integers, floats, and lists. Requires the zero value for the
underlying type
as a parameter.
"""
- def __init__(self, zero_value):
+ def __init__(self, zero_value: U):
self.zero_value = zero_value
- def zero(self, value):
+ def zero(self, value: U) -> U:
return self.zero_value
- def addInPlace(self, value1, value2):
+ def addInPlace(self, value1: U, value2: U) -> U:
value1 += value2
Review comment:
OK, so reducing this a bit:
```python
# main.py
from __future__ import annotations
from typing import Generic, Protocol, TypeVar
T = TypeVar("T")
U = TypeVar("U", bound=SupportsIAdd)
class SupportsIAdd(Protocol):
def __iadd__(self: T, other: T) -> T: ...
class AddingAccumulatorParam(Generic[U]):
def addInPlace(self, value1: U, value2: U) -> U:
value1 += value2
return value1
```
```
main.py:17: error: Unsupported left operand type for + ("U")
Found 1 error in 1 file (checked 1 source file)
```
but after replacing `__iadd__` with `__add__` passes
```python
# main.py
from __future__ import annotations
from typing import Generic, Protocol, TypeVar
T = TypeVar("T")
U = TypeVar("U", bound=SupportsIAdd)
class SupportsIAdd(Protocol):
def __add__(self: T, other: T) -> T: ...
class AddingAccumulatorParam(Generic[U]):
def addInPlace(self, value1: U, value2: U) -> U:
value1 += value2
return value1
```
I honestly don't see why, but there are all kinds of subtleties around
`__iadd__`, `__radd__` and `__add__`. Anyone?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]