bearomorphism opened a new pull request, #59348:
URL: https://github.com/apache/spark/pull/59348

   ### What changes were proposed in this pull request?
   
   Declare `size: int` on `pyspark.mllib.linalg.Vector` and remove four 
redundant `type: ignore[attr-defined]` comments in `classification.py`.
   
   Keep all existing `.size` accesses and the concrete dense/sparse 
implementations unchanged. Suppressions for other undeclared methods, such as 
`dot()`, are outside this PR's scope.
   
   ### Why are the changes needed?
   
   Both vector implementations already expose an integer size: `SparseVector` 
stores it directly, and `DenseVector` delegates it to its NumPy array. However, 
the base `Vector` does not declare it, so mypy rejects valid accesses through a 
`Vector` reference:
   
   ```python
   from pyspark.mllib.linalg import Vector
   
   
   def dimension(vector: Vector) -> int:
       return vector.size
   ```
   
   Previously this produced `"Vector" has no attribute "size" [attr-defined]`. 
Declaring the existing attribute on the base class fixes the shared typing 
contract without requiring callers to use `len()` instead.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Static typing only: accessing `.size` through a `Vector` reference is now 
recognized as returning `int`. There is no runtime behavior change or new 
runtime API.
   
   ### How was this patch tested?
   
   Passed:
   
   ```sh
   mypy --config-file python/mypy.ini --follow-imports=silent \
     python/pyspark/mllib/classification.py 
python/pyspark/mllib/linalg/__init__.py
   ruff check python/pyspark/mllib/classification.py 
python/pyspark/mllib/linalg/__init__.py
   ruff format --check python/pyspark/mllib/classification.py 
python/pyspark/mllib/linalg/__init__.py
   git diff --check
   ```
   
   Also verified that the typing example above passes after the change. 
Temporary Python checks passed 40 dense/sparse scenarios covering empty and 
all-zero vectors, trailing zeros, multiclass prediction with and without bias, 
and streaming model initialization. These checks confirmed that the annotation 
does not introduce a runtime `size` attribute on the base class.
   
   No permanent tests were added for this annotation-only cleanup. No 
JVM-backed test suites were run.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Pi 1.1.0
   


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