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

MAINT: random: Use 'from exc' when raising a ValueError in choice. #18439

Merged
merged 2 commits into from
Mar 7, 2021

Conversation

WarrenWeckesser
Copy link
Member

@WarrenWeckesser WarrenWeckesser commented Feb 18, 2021

Use 'from exc' when the ValueError is raised when the first argument to
choice is not an array and not an integer.

Also fix the error message for the ValueError.

@@ -700,7 +700,7 @@ cdef class Generator:
# __index__ must return an integer by python rules.
pop_size = operator.index(a.item())
except TypeError:
raise ValueError("a must an array or an integer")
raise ValueError("a must be an array or an integer") from None
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to add the type?

Suggested change
raise ValueError("a must be an array or an integer") from None
raise ValueError(f"a must be an array or an integer not '{type(a)}'") from None

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, but at this point, a has already been converted to a numpy array, so type(a) will always be <class 'numpy.ndarray'>. I pushed a change to avoid that problem. (I also fixed a couple more error messages that were missing a space.)

Copy link
Member

Choose a reason for hiding this comment

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

This is still confusing though, because choice(np.array(1)) will give a message about ndarray not being an array

Copy link
Member Author

Choose a reason for hiding this comment

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

I changed the message to say that a must be a sequence or an integer (i.e. replace "array" with "sequence"). Suggestions for better message are welcome!

Use 'from None' when the ValueError is raised when the first argument to
`choice` is not an array and not an integer.

Also fix the error message for the ValueError, and add a missing space
to two other error messages.
Comment on lines 707 to 708
raise ValueError("a must be a positive integer unless no"
"samples are taken")
" samples are taken")
Copy link
Member

Choose a reason for hiding this comment

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

I think we normally format this with the space at the end of the prevous line

Copy link
Member Author

Choose a reason for hiding this comment

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

Done. If this is important enough to comment on in a PR, perhaps it is important enough to have in a style guide somewhere?

a = np.array(a, copy=False)
if a.ndim == 0:
try:
# __index__ must return an integer by python rules.
pop_size = operator.index(a.item())
except TypeError:
raise ValueError("a must an array or an integer")
raise ValueError("a must be an array or an integer, "
f"not {type(a_original)}") from None
Copy link
Member

Choose a reason for hiding this comment

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

I'm not convinced the from None is a good idea. Consider:

class MyObject:
    def __index__(self):
        if some_complex_condition:
            raise TypeError("with useful information")
        return 42

or

class MyObject:
    def __index__(self):
        return 1 + "2"  # oops, TypeError

With this PR, the traceback of choice(MyObject()) no longer shows me error information. I think from None should be used very sparingly (eg, for KeyError where you really know exactly what the cause was).

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you recommend using except TypeError as exc and then raise ValueError(...) from exc, or just leave it as it is in master?

Copy link
Member

Choose a reason for hiding this comment

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

from exc sounds good to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@WarrenWeckesser WarrenWeckesser changed the title MAINT: random: Use 'from None' when raising a ValueError in choice. MAINT: random: Use 'from exc' when raising a ValueError in choice. Feb 20, 2021
@WarrenWeckesser
Copy link
Member Author

I'm not sure why the pypy test for the exception raised by choice(3., 3) failed. It reports this error:

E   TypeError: __format__() missing 1 required positional argument: 'spec'

Wild speculation: Is that a pypy bug, where it loses the format spec when concatenating a regular string and an f-string?

@mattip
Copy link
Member

mattip commented Feb 21, 2021

Wild speculation: Is that a pypy bug, where it loses the format spec when concatenating a regular string and an f-string?

It seems PyPy does not like cythonized f-strings where the format is {type(a)}. I opened an issue. "msg {}".format(type(a)) does work. :(

Edit: the issue was fixed and it will be part of the next release.

Base automatically changed from master to main March 4, 2021 02:05
@charris charris merged commit 4af35a7 into numpy:main Mar 7, 2021
@charris
Copy link
Member

charris commented Mar 7, 2021

Thanks Warren.

@WarrenWeckesser WarrenWeckesser deleted the random-choice-exc branch March 8, 2021 12:22
@charris
Copy link
Member

charris commented Mar 8, 2021

@mattip Any idea how close the next PyPy release is? If it is a ways off I will probably fix the failure.

@mattip
Copy link
Member

mattip commented Mar 8, 2021

It might take till April. In the mean time I submitted a PR to use the nightly builds.

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

4 participants