zero323 commented on a change in pull request #34363:
URL: https://github.com/apache/spark/pull/34363#discussion_r760622451



##########
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:
       It seems that the problem is somehow related to combination of 
`__iadd__` and `TypeVar`. Further reducing the example ‒ this still fails 
   
   ```python
   # main.py
   
   from __future__ import annotations
   from typing import Any, TypeVar              
   
   
   U = TypeVar("U", bound="SupportsIAdd")        
   
   
   class SupportsIAdd:          
       def __iadd__(self: U, other: Any) -> U: ...                 
   
   
   class AddingAccumulatorParam:            
       def addInPlace(self, value1: U, value2: U) -> U: 
           value1 += value2
           return value1
   ````
   
   but substituting `U` with `SupportsIAdd`:
   
   
   ```python
   # main.py
   
   from __future__ import annotations
   from typing import Any, TypeVar              
   
   
   class SupportsIAdd:          
       def __iadd__(self, other: Any) -> SupportsIAdd: ...         
   
   
   class AddingAccumulatorParam:            
       def addInPlace(self, value1: SupportsIAdd, value2: SupportsIAdd) -> 
SupportsIAdd:            
           value1 += value2
           return value1
   
   ```
   
   makes mypy happy. It don't think it helps us to resolve it here, but narrows 
down the scope, in  case we want to escalate this to mypy folks.




-- 
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]

Reply via email to