PlasmaPy/PlasmaPy

Improve error message for `gyroradius` when given wrong number of arguments

Aberta

#2.065 aberto em 5 de abr. de 2023

 (0 comentário) (0 reação) (0 responsável)Python (367 forks)auto 404
Plasma Lv1 | BeginnerPython Lv2 | Intermediategood first issueplasmapy.formularypriority: low

Métricas do repositório

Stars
 (698 estrelas)
Métricas de merge de PR
 (Métricas PR pendentes)

Description

When I just tried using gyroradius and forgot to specify the Vperp keyword argument, I ended up getting a quite unhelpful error message:

>>> gyroradius(B = [1, 2] * u.T, particle=["p+", "e-"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/namurphy/Projects/PlasmaPy/plasmapy/utils/decorators/validators.py", line 206, in wrapper
    _return = f(**bound_args.arguments)
  File "/home/namurphy/Projects/PlasmaPy/plasmapy/particles/decorators.py", line 897, in wrapper
    return callable__(**new_kwargs)
  File "/home/namurphy/Projects/PlasmaPy/plasmapy/formulary/lengths.py", line 333, in gyroradius
    Vperp = _calculate_vperp_from_lorentzfactor(
  File "/home/namurphy/Projects/PlasmaPy/plasmapy/formulary/lengths.py", line 242, in _calculate_vperp_from_lorentzfactor
    Vperp[~isfinite_Vperp] = rbody.velocity
  File "/home/namurphy/miniconda3/envs/pldev/lib/python3.10/site-packages/astropy/units/quantity.py", line 1301, in __setitem__
    self.view(np.ndarray).__setitem__(i, self._to_own_unit(value))
ValueError: NumPy boolean array indexing assignment cannot assign 2 input values to the 1 output values where the mask is true

The following line is the source of the error:

Vperp[~isfinite_Vperp] = rbody.velocity

We might also want to add something to @validate_quantities for a situation where one of multiple optional keyword-only arguments must be provided.

Guia do colaborador