Skip to content

Commit 319bffe

Browse files
authored
Make Z and mass_numb parameters for Particle keyword-only (#1456)
* Make Z and mass_numb keyword-only in Particle * Add changelog entry * Raise explicit error * Update explicit error message * Allow multiple positional args in Particle error tests * Test when Particle is provided with >1 positional arg ...since Z and mass_numb should be keyword-only. * Remove coverage: ignore comment
1 parent 190f358 commit 319bffe

File tree

3 files changed

+56
-41
lines changed

3 files changed

+56
-41
lines changed

changelog/1456.breaking.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The parameters ``Z`` and ``mass_numb`` to |Particle| are now
2+
:term:`keyword-only`.

plasmapy/particles/particle_class.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ class Particle(AbstractPhysicalParticle):
234234
integer representing the atomic number of an element; or a
235235
|Particle| instance.
236236
237-
mass_numb : `int`, optional
237+
mass_numb : `int`, optional, keyword-only
238238
The mass number of an isotope or nuclide.
239239
240-
Z : `int`, optional
240+
Z : `int`, optional, keyword-only
241241
The charge number of the particle.
242242
243243
Raises
@@ -413,10 +413,19 @@ class Particle(AbstractPhysicalParticle):
413413
def __init__(
414414
self,
415415
argument: ParticleLike,
416+
*_,
416417
mass_numb: Integral = None,
417418
Z: Integral = None,
418419
):
419420

421+
# TODO: Remove the following block during or after the 0.9.0 release
422+
423+
if _:
424+
raise TypeError(
425+
"The parameters mass_numb and Z to Particle are now "
426+
"keyword-only [e.g., Particle('H', mass_numb=2, Z=1)]."
427+
)
428+
420429
# If argument is a Particle instance, then construct a new
421430
# Particle instance for the same particle.
422431

plasmapy/particles/tests/test_particle_class.py

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -584,56 +584,60 @@ def test_Particle_equivalent_cases(equivalent_particles):
584584
run_test_equivalent_calls(Particle, *equivalent_particles)
585585

586586

587-
# arg, kwargs, attribute, exception
587+
# args, kwargs, attribute, exception
588588
test_Particle_error_table = [
589-
("a", {}, "", InvalidParticleError),
590-
("d+", {"mass_numb": 9}, "", InvalidParticleError),
591-
("H", {"mass_numb": 99}, "", InvalidParticleError),
592-
("Au-818", {}, "", InvalidParticleError),
593-
("Au-12", {}, "", InvalidParticleError),
594-
("Au", {"mass_numb": 13}, "", InvalidParticleError),
595-
("Au", {"mass_numb": 921}, "", InvalidParticleError),
596-
("e-", {"Z": -1}, "", InvalidParticleError),
597-
("e-", {}, ".atomic_number", InvalidElementError),
598-
("alpha", {}, ".standard_atomic_weight", InvalidElementError),
599-
("Fe-56", {}, ".standard_atomic_weight", InvalidElementError),
600-
("e-", {}, ".standard_atomic_weight", InvalidElementError),
601-
("tau-", {}, ".element_name", InvalidElementError),
602-
("tau+", {}, ".atomic_number", InvalidElementError),
603-
("neutron", {}, ".atomic_number", InvalidElementError),
604-
("H", {"Z": 0}, ".mass_number", InvalidIsotopeError),
605-
("neutron", {}, ".mass_number", InvalidIsotopeError),
606-
("He", {"mass_numb": 4}, ".charge", ChargeError),
607-
("He", {"mass_numb": 4}, ".charge_number", ChargeError),
608-
("Fe", {}, ".spin", MissingParticleDataError),
609-
("nu_e", {}, ".mass", MissingParticleDataError),
610-
("Og", {}, ".standard_atomic_weight", MissingParticleDataError),
611-
(Particle("C-14"), {"mass_numb": 13}, "", InvalidParticleError),
612-
(Particle("Au 1+"), {"Z": 2}, "", InvalidParticleError),
613-
([], {}, "", TypeError),
614-
("Fe", {}, ".ionize()", ChargeError),
615-
("D", {}, ".recombine()", ChargeError),
616-
("Fe 26+", {}, ".ionize()", InvalidIonError),
617-
("Fe 6+", {}, ".ionize(-1)", ValueError),
618-
("Fe 25+", {}, ".recombine(0)", ValueError),
619-
("Fe 6+", {}, ".ionize(4.6)", TypeError),
620-
("Fe 25+", {}, ".recombine(8.2)", TypeError),
621-
("e-", {}, ".ionize()", InvalidElementError),
622-
("e+", {}, ".recombine()", InvalidElementError),
589+
(["a"], {}, "", InvalidParticleError),
590+
(["d+"], {"mass_numb": 9}, "", InvalidParticleError),
591+
(["H"], {"mass_numb": 99}, "", InvalidParticleError),
592+
(["Au-818"], {}, "", InvalidParticleError),
593+
(["Au-12"], {}, "", InvalidParticleError),
594+
(["Au"], {"mass_numb": 13}, "", InvalidParticleError),
595+
(["Au"], {"mass_numb": 921}, "", InvalidParticleError),
596+
(["e-"], {"Z": -1}, "", InvalidParticleError),
597+
(["e-"], {}, ".atomic_number", InvalidElementError),
598+
(["alpha"], {}, ".standard_atomic_weight", InvalidElementError),
599+
(["Fe-56"], {}, ".standard_atomic_weight", InvalidElementError),
600+
(["e-"], {}, ".standard_atomic_weight", InvalidElementError),
601+
(["tau-"], {}, ".element_name", InvalidElementError),
602+
(["tau+"], {}, ".atomic_number", InvalidElementError),
603+
(["neutron"], {}, ".atomic_number", InvalidElementError),
604+
(["H"], {"Z": 0}, ".mass_number", InvalidIsotopeError),
605+
(["neutron"], {}, ".mass_number", InvalidIsotopeError),
606+
(["He"], {"mass_numb": 4}, ".charge", ChargeError),
607+
(["He"], {"mass_numb": 4}, ".charge_number", ChargeError),
608+
(["Fe"], {}, ".spin", MissingParticleDataError),
609+
(["nu_e"], {}, ".mass", MissingParticleDataError),
610+
(["Og"], {}, ".standard_atomic_weight", MissingParticleDataError),
611+
([Particle("C-14")], {"mass_numb": 13}, "", InvalidParticleError),
612+
([Particle("Au 1+")], {"Z": 2}, "", InvalidParticleError),
613+
([[]], {}, "", TypeError),
614+
(["Fe"], {}, ".ionize()", ChargeError),
615+
(["D"], {}, ".recombine()", ChargeError),
616+
(["Fe 26+"], {}, ".ionize()", InvalidIonError),
617+
(["Fe 6+"], {}, ".ionize(-1)", ValueError),
618+
(["Fe 25+"], {}, ".recombine(0)", ValueError),
619+
(["Fe 6+"], {}, ".ionize(4.6)", TypeError),
620+
(["Fe 25+"], {}, ".recombine(8.2)", TypeError),
621+
(["e-"], {}, ".ionize()", InvalidElementError),
622+
(["e+"], {}, ".recombine()", InvalidElementError),
623+
(["H", 1], {}, "", TypeError),
624+
(["H", 1, 1], {}, "", TypeError),
623625
]
624626

625627

626-
@pytest.mark.parametrize("arg, kwargs, attribute, exception", test_Particle_error_table)
627-
def test_Particle_errors(arg, kwargs, attribute, exception):
628+
@pytest.mark.parametrize(
629+
"args, kwargs, attribute, exception", test_Particle_error_table
630+
)
631+
def test_Particle_errors(args, kwargs, attribute, exception):
628632
"""
629633
Test that the appropriate exceptions are raised during the creation
630634
and use of a `~plasmapy.particles.Particle` object.
631635
"""
632636
with pytest.raises(exception):
633-
exec(f"Particle(arg, **kwargs){attribute}")
637+
exec(f"Particle(*args, **kwargs){attribute}")
634638
pytest.fail(
635639
f"The following command: "
636-
f"\n\n {call_string(Particle, arg, kwargs)}{attribute}\n\n"
640+
f"\n\n {call_string(Particle, args, kwargs)}{attribute}\n\n"
637641
f"did not raise a {exception.__name__} as expected"
638642
)
639643

0 commit comments

Comments
 (0)