Skip to content

MAINT: Chain exceptions and use NameError in np.bmat - #16215

Merged
seberg merged 4 commits into
numpy:masterfrom
bartosz-grabowski:chain-exceptions
May 14, 2020
Merged

seberg merged 4 commits into
numpy:masterfrom
bartosz-grabowski:chain-exceptions

Conversation

@bartosz-grabowski

Copy link
Copy Markdown
Contributor

This solution is related to the issue #15986. I also made a change to the newer string formatting.

@bartosz-grabowski
bartosz-grabowski marked this pull request as ready for review May 12, 2020 16:42
Comment thread numpy/matrixlib/defmatrix.py Outdated
@eric-wieser

eric-wieser commented May 12, 2020 •

Copy link
Copy Markdown
Member

For context, here's what the traceback is on master:

>>> import numpy as np
>>> np.bmat("A")
Traceback (most recent call last):
  File "C:\Users\wiese\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\matrixlib\defmatrix.py", line 1025, in _from_string
    thismat = ldict[col]
KeyError: 'A'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\wiese\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\matrixlib\defmatrix.py", line 1028, in _from_string
    thismat = gdict[col]
KeyError: 'A'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\wiese\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\matrixlib\defmatrix.py", line 1101, in bmat
    return matrix(_from_string(obj, glob_dict, loc_dict))
  File "C:\Users\wiese\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\matrixlib\defmatrix.py", line 1030, in _from_string
    raise KeyError("%s not found" % (col,))
KeyError: 'A not found'

With this PR, only the last traceback is shown, which is definitely an improvement.

Note that the misuse of KeyError leads to ugly extra quotes, hence my suggestion of using NameError instead.

@charris charris changed the title MAINT: chain exceptions in defmatrix.py MAINT: Chain exceptions in defmatrix.py May 13, 2020

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM aside from one style nit (80-char line limit)

Comment thread numpy/matrixlib/defmatrix.py Outdated
Make a switch to f-string formatting to avoid 80-char line limit.

Co-authored-by: Ross Barnowski <[email protected]>
@seberg

seberg commented May 14, 2020

Copy link
Copy Markdown
Member

Thanks all, lets put this in!

@seberg seberg changed the title MAINT: Chain exceptions in defmatrix.py MAINT: Chain exceptions and use NameError in defmatrix.py May 14, 2020
@seberg seberg changed the title MAINT: Chain exceptions and use NameError in defmatrix.py MAINT: Chain exceptions and use NameError in np.bmat May 14, 2020
@seberg
seberg merged commit a813a5f into numpy:master May 14, 2020
@bartosz-grabowski
bartosz-grabowski deleted the chain-exceptions branch February 22, 2025 20:36
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.

5 participants