Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Improper Method Call: Replaced NotImplementedError with NotImplemented #7900

Merged
merged 6 commits into from
Oct 17, 2023

Conversation

fazledyn-or
Copy link
Contributor

Details

  1. In file: _csc.py, class: csc_matrix, there is a special method __mul__ that raises a NotImplementedError. If a special method supporting a binary operation is not implemented it should return NotImplemented. On the other hand, NotImplementedError should be raised from abstract methods inside user defined base classes to indicate that derived classes should override those methods. iCR suggested that the special method __mul__ should return NotImplemented instead of raising an exception.

  2. In file: _csr.py, class: csr_matrix, there is a special method __truediv__ that raises a NotImplementedError. If a special method supporting a binary operation is not implemented it should return NotImplemented. On the other hand, NotImplementedError should be raised from abstract methods inside user defined base classes to indicate that derived classes should override those methods. iCR suggested that the special method __truediv__ should return NotImplemented instead of raising an exception.

An example of how NotImplemented helps the interpreter support a binary operation is shown here.

Notes

  • Both the class csr_matrix and csc_matrix inherits the following classes as below. Among them, a binary operation method, such as __mul__ is already defined and implemented in class _base.spmatrix. As a result, it's suggested that NotImplemented should be used instead of raising an exception.

    - csr_matrix
        - _compressed._compressed_sparse_matrix
      	  - sparse_data._data_matrix
      		  - _base.spmatrix
      			  ```
      		    	    def __mul__(self, other):
          			        return self.tocsr().__mul__(other)
      			  ```
                    - sparse_data._minmax_mixin
                  	  - object
                    - _index.IndexMixin
                  	  - object
    
    - csc_matrix
        - _compressed._compressed_sparse_matrix (SAME as above)
    
  • However, for special custom methods, one can opt-in to use raise NotImplementedError.

CLA Requirements:

This section is only relevant if your project requires contributors to sign a Contributor License Agreement (CLA) for external contributions.

All contributed commits are already automatically signed off.

The meaning of a signoff depends on the project, but it typically certifies that committer has the rights to submit this work under the same license and agrees to a Developer Certificate of Origin (see https://developercertificate.org/ for more information).

Sponsorship and Support:

This work is done by the security researchers from OpenRefactory and is supported by the Open Source Security Foundation (OpenSSF): Project Alpha-Omega. Alpha-Omega is a project partnering with open source software project maintainers to systematically find new, as-yet-undiscovered vulnerabilities in open source code - and get them fixed – to improve global software supply chain security.

The bug is found by running the Intelligent Code Repair (iCR) tool by OpenRefactory and then manually triaging the results.

@asi1024 asi1024 self-assigned this Oct 4, 2023
@asi1024
Copy link
Member

asi1024 commented Oct 4, 2023

/test mini

@asi1024 asi1024 added this to the v13.0.0rc1 milestone Oct 4, 2023
@kmaehashi
Copy link
Member

As per the definition of NotImplemented:

A special value which should be returned by the binary special methods (e.g. eq(), lt(), add(), rsub(), etc.) to indicate that the operation is not implemented with respect to the other type;

I think

                else:
                    raise NotImplementedError

should be replaced by raise AssertionError, not return NotImplemented. These else clauses are tests against cuSPARSE's feature availability (not testing "with respect to the other type"), and never expected to reach.

@fazledyn-or
Copy link
Contributor Author

fazledyn-or commented Oct 5, 2023

@kmaehashi In that case, let's use AssertionError instead of NotImplemented in the mentioned code. Since I have no prior knowledge regarding the Cupy codebase, I had no idea. Also, what should be the message inside the error ?

Let me add new commits to this PR.

@kmaehashi
Copy link
Member

I guess just doing raise AssertionError is fine here, this is the case where users should never see.

@fazledyn-or
Copy link
Contributor Author

I've replaced return NotImplemented with raise AssertionError except at the following method -

def __rtruediv__(self, other):
    return NotImplemented

in _csr.py since the method isn't even implemented at all.

Copy link
Member

@kmaehashi kmaehashi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@asi1024
Copy link
Member

asi1024 commented Oct 14, 2023

/test mini

@asi1024
Copy link
Member

asi1024 commented Oct 14, 2023

/test mini

@asi1024
Copy link
Member

asi1024 commented Oct 17, 2023

@fazledyn-or LGTM. Thanks for your contribution!

@asi1024 asi1024 merged commit a6e8cee into cupy:main Oct 17, 2023
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants