CARVIEW |
Navigation Menu
-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Added implementation of IndexError for non int-indices in row_insert() and col_insert() #15155
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
Conversation
âś… Hi, I am the SymPy bot (v130). I'm here to help you write a release notes entry. Please read the guide on how to write release notes. Your release notes are in good order. Here is what the release notes will look like:
This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.2.1. Note: This comment will be updated with the latest check if you edit the pull request. You need to reload the page to see it. Click here to see the pull request description that was parsed.
Update The release notes on the wiki have been updated. |
@@ -2328,19 +2328,47 @@ def test_col_join(): | |||
|
|||
def test_row_insert(): | |||
r4 = Matrix([[4, 4, 4]]) | |||
for i in range(-4, 5): | |||
for i in range(-3, 3): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it needed to change prior tests? Were they incorrect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@moorepants Yes, they were incorrect. Since earlier, even if the index went out of bound (like in the above case, -4
, 4
, and 5
were out of bounds) or was a non integer, it didn't result in an error (which it should've). But that's been corrected by this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the existing tests are correct, as the indices are being pivoted to the appropriate range. You would want integer casts for pos
using as_int
for an exception be raised. (I think that is what Aaron was pointing out, that current approach won't affect float inputs.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sidhantnagpal I have corrected the floats part (which @asmeurer had asked) but I still don't understand why is the range (-4, 5) valid? I think the entire purpose of using this statement :
if pos < 0:
pos = self.rows + pos
was
- to make the negative indices
>=0
and pos
when exceedsself.rows
, doesn't make sense to insert another row in place of a row which doesn't exist. This is why I think-4
,4
, and5
are out of bounds.
Am I wrong here?
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sidhantnagpal ping
I don't see how this affects float inputs. |
This is the output on branch master :
While this is the output on my branch (i.e. after changes have been made)
This Index error is shown now for non integer inputs. |
Oh! I couldn't understand what you exactly meant earlier. Thanks @sidhantnagpal for making that clear. I have made the changes now.
I hope this is what you were asking right @asmeurer ? |
sympy/matrices/common.py
Outdated
if pos < 0: | ||
pos = 0 | ||
elif pos > self.cols: | ||
pos = self.cols |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these lines should be left as-is (and exception should not be raised).
This behaviour is consistent with the way indexing works in Python.
The only change could be i = as_int(pos)
(with the necessary import).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only change could be i = as_int(pos) (with the necessary import).
If I am not raising an error (IndexError), what good will that do?
I think these lines should be left as-is (and exception should not be raised).
Why do you think a float input should not result in an error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those lines don't handle float inputs specially.
The conditional checks could use i
instead of pos
.
The exception will then be raised in as_int
(and not anywhere else).
@sidhantnagpal Please take a look. Does this look correct now? |
sympy/matrices/common.py
Outdated
as_int(pos) | ||
except ValueError: | ||
raise IndexError("Index is not an integer: 'pos = %s', valid : %s must be an integer" % ( | ||
pos, pos)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IndexError
is for out-of-bound errors, TypeError
should be used here (that too if an exception has to be raised explicitly).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sidhantnagpal Okay, will do.
sympy/matrices/common.py
Outdated
except ValueError: | ||
raise IndexError("Index is not an integer: 'pos = %s', valid : %s must be an integer" % ( | ||
pos, pos)) | ||
else: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This else seems redundant and also causes an extra indentation level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sidhantnagpal Okay, will do.
@@ -189,14 +189,14 @@ def test_col_join(): | |||
|
|||
def test_row_insert(): | |||
r4 = Matrix([[4, 4, 4]]) | |||
for i in range(-4, 5): | |||
for i in range(-3, 3): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is not required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sidhantnagpal Okay, will do.
@@ -2341,6 +2341,37 @@ def test_col_insert(): | |||
l.insert(i, 4) | |||
assert flatten(zeros(3).col_insert(i, c4).row(0).tolist()) == l | |||
|
|||
def test_row_and_column_insert_for_indices(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the code is well-tested viz. test_row_inserts()
and test_col_inserts()
. The error tests could probably go in test_errors()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sidhantnagpal Okay, will do.
@sidhantnagpal Please take a look. |
sympy/matrices/common.py
Outdated
@@ -203,6 +203,12 @@ def col_insert(self, pos, other): | |||
if not self: | |||
return type(self)(other) | |||
|
|||
try: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to prefer that TypeError instead of ValueError be raised? If not, this could just be pos = as_int(pos)
and the standard ValueError would be raised...and it should give enough of a clue as to what is wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@smichr the purpose of using the TypeError
was to clarify to the user what exactly has he done wrong and what is the correct format. ValueError
only shows pos is not an integer
while TypeError
also shows what is the correct format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@smichr What do you think? If you insist, I'l remove the try:
statement and just keep it as pos = as_int(pos)
in the beginning. What do you say?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should not create custom error messages if a standard response is sufficient. e.g.
>>> factorint(3.5)
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/base/data/home/apps/s~sympy-live-hrd/55.412087474737415211/sympy/sympy/ntheory/factor_.py", line 1029, in factorint
n = as_int(n)
File "/base/data/home/apps/s~sympy-live-hrd/55.412087474737415211/sympy/sympy/core/compatibility.py", line 377, in as_int
raise ValueError('%s is not an integer' % (n,))
ValueError: 3.5 is not an integer
The user learns from this that 3.5 is not an integer. They also know that this is happening in factorint which they just called. So saying that "factorint expects an integer" is redundant. My feeling is that the same applies here. Any routine that expects to process integers and needs to raise an error for non-integer intput can safely put n = as_int(n)
at the top of their routine and know that no non-int will pass and the user will know of the issue.
@sidhantnagpal @smichr Please take a look. |
Does this look good to merge now? |
Issue before :
M.row_insert()
andM.col_insert()
does not raise error for non-int indicesThis has been fixed in this PR. Now such a non-int input results in an
IndexError
.Please take a look and suggest improvements.
Thanks.
Release Notes
IndexError
for non int-indices inrow_insert()
andcol_insert()