Skip to content

Commit a481743

Browse files
Jac0bDealStanczakDominik
authored andcommitted
Update Type Hint Annotations for Consistency (PlasmaPy#586)
* Update plasmapy.atomic type hints for consistency * update plasmapy.utils type hints for consistency * add Jacob Deal to contributors
1 parent b3ecbf4 commit a481743

8 files changed

Lines changed: 65 additions & 60 deletions

File tree

‎docs/about/credits.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ in parentheses are `ORCID author identifiers <https://orcid.org>`__.
3030
* `Sean Chambers <https://github.com/schambers>`__
3131
* `Apoorv Choubey <https://github.com/apooravc>`__
3232
* `cclauss <https://github.com/cclauss>`__
33+
* `Jacob Deal <https://github.com/Jac0bDeal>`__
3334
* `Leah Einhorn <https://github.com/leahein>`__
3435
* `Thomas Fan <https://github.com/thomasjpfan>`__
3536
* `Samaiyah I. Farid <https://github.com/samaiyahfarid>`__

‎plasmapy/atomic/atomic.py‎

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
MissingAtomicDataError,
2525
)
2626

27-
import numbers
27+
from numbers import (
28+
Integral,
29+
Real,
30+
)
2831

2932
__all__ = [
3033
"atomic_number",
@@ -48,7 +51,7 @@
4851

4952

5053
@particle_input
51-
def atomic_number(element: Particle) -> numbers.Integral:
54+
def atomic_number(element: Particle) -> Integral:
5255
"""
5356
Return the number of protons in an atom, isotope, or ion.
5457
@@ -95,7 +98,7 @@ def atomic_number(element: Particle) -> numbers.Integral:
9598

9699

97100
@particle_input
98-
def mass_number(isotope: Particle) -> numbers.Integral:
101+
def mass_number(isotope: Particle) -> Integral:
99102
"""Get the mass number (the number of protons and neutrons) of an
100103
isotope.
101104
@@ -207,8 +210,8 @@ def standard_atomic_weight(element: Particle) -> u.Quantity:
207210
def particle_mass(
208211
particle: Particle,
209212
*,
210-
Z: numbers.Integral = None,
211-
mass_numb: numbers.Integral = None) -> u.Quantity:
213+
Z: Integral = None,
214+
mass_numb: Integral = None) -> u.Quantity:
212215
"""
213216
Return the mass of a particle.
214217
@@ -261,7 +264,7 @@ def particle_mass(
261264

262265

263266
@particle_input
264-
def isotopic_abundance(isotope: Particle, mass_numb: Optional[numbers.Integral] = None) -> numbers.Real:
267+
def isotopic_abundance(isotope: Particle, mass_numb: Optional[Integral] = None) -> Real:
265268
"""
266269
Return the isotopic abundances if known, and otherwise zero.
267270
@@ -313,7 +316,7 @@ def isotopic_abundance(isotope: Particle, mass_numb: Optional[numbers.Integral]
313316

314317

315318
@particle_input(any_of={'charged', 'uncharged'})
316-
def integer_charge(particle: Particle) -> numbers.Integral:
319+
def integer_charge(particle: Particle) -> Integral:
317320
"""Return the integer charge of a particle.
318321
319322
Parameters
@@ -423,7 +426,7 @@ def electric_charge(particle: Particle) -> u.Quantity:
423426

424427

425428
@particle_input
426-
def is_stable(particle: Particle, mass_numb: Optional[numbers.Integral] = None) -> bool:
429+
def is_stable(particle: Particle, mass_numb: Optional[Integral] = None) -> bool:
427430
"""
428431
Return `True` for stable isotopes and particles and `False` for
429432
unstable isotopes.
@@ -477,7 +480,7 @@ def is_stable(particle: Particle, mass_numb: Optional[numbers.Integral] = None)
477480

478481

479482
@particle_input(any_of={'stable', 'unstable', 'isotope'})
480-
def half_life(particle: Particle, mass_numb: Optional[numbers.Integral] = None) -> u.Quantity:
483+
def half_life(particle: Particle, mass_numb: Optional[Integral] = None) -> u.Quantity:
481484
"""
482485
Return the half-life in seconds for unstable isotopes and particles,
483486
and numpy.inf in seconds for stable isotopes and particles.
@@ -532,7 +535,7 @@ def half_life(particle: Particle, mass_numb: Optional[numbers.Integral] = None)
532535
return particle.half_life
533536

534537

535-
def known_isotopes(argument: Union[str, numbers.Integral] = None) -> List[str]:
538+
def known_isotopes(argument: Union[str, Integral] = None) -> List[str]:
536539
"""Return a list of all known isotopes of an element, or a list
537540
of all known isotopes of every element if no input is provided.
538541
@@ -622,7 +625,7 @@ def known_isotopes_for_element(argument):
622625

623626

624627
def common_isotopes(
625-
argument: Union[str, numbers.Integral] = None,
628+
argument: Union[str, Integral] = None,
626629
most_common_only: bool = False) -> List[str]:
627630
"""
628631
Return a list of isotopes of an element with an isotopic abundances
@@ -741,7 +744,7 @@ def common_isotopes_for_element(
741744
return isotopes_list
742745

743746

744-
def stable_isotopes(argument: Union[str, numbers.Integral] = None,
747+
def stable_isotopes(argument: Union[str, Integral] = None,
745748
unstable: bool = False) -> List[str]:
746749
"""
747750
Return a list of all stable isotopes of an element, or if no input is
@@ -915,7 +918,7 @@ def get_particle_mass(particle) -> u.Quantity:
915918
return (test_mass * target_mass) / (test_mass + target_mass)
916919

917920

918-
def periodic_table_period(argument: Union[str, numbers.Integral]) -> numbers.Integral:
921+
def periodic_table_period(argument: Union[str, Integral]) -> Integral:
919922
"""
920923
Return the periodic table period.
921924
@@ -956,7 +959,7 @@ def periodic_table_period(argument: Union[str, numbers.Integral]) -> numbers.Int
956959
957960
"""
958961
# TODO: Implement @particle_input
959-
if not isinstance(argument, (str, numbers.Integral)):
962+
if not isinstance(argument, (str, Integral)):
960963
raise TypeError(
961964
"The argument to periodic_table_period must be either a "
962965
"string representing the element or its symbol, or an "
@@ -966,7 +969,7 @@ def periodic_table_period(argument: Union[str, numbers.Integral]) -> numbers.Int
966969
return period
967970

968971

969-
def periodic_table_group(argument: Union[str, numbers.Integral]) -> numbers.Integral:
972+
def periodic_table_group(argument: Union[str, Integral]) -> Integral:
970973
"""
971974
Return the periodic table group.
972975
@@ -1013,7 +1016,7 @@ def periodic_table_group(argument: Union[str, numbers.Integral]) -> numbers.Inte
10131016
10141017
"""
10151018
# TODO: Implement @particle_input
1016-
if not isinstance(argument, (str, numbers.Integral)):
1019+
if not isinstance(argument, (str, Integral)):
10171020
raise TypeError(
10181021
"The argument to periodic_table_group must be "
10191022
"either a string representing the element or its "
@@ -1023,7 +1026,7 @@ def periodic_table_group(argument: Union[str, numbers.Integral]) -> numbers.Inte
10231026
return group
10241027

10251028

1026-
def periodic_table_block(argument: Union[str, numbers.Integral]) -> str:
1029+
def periodic_table_block(argument: Union[str, Integral]) -> str:
10271030
"""
10281031
Return the periodic table block.
10291032
@@ -1070,7 +1073,7 @@ def periodic_table_block(argument: Union[str, numbers.Integral]) -> str:
10701073
10711074
"""
10721075
# TODO: Implement @particle_input
1073-
if not isinstance(argument, (str, numbers.Integral)):
1076+
if not isinstance(argument, (str, Integral)):
10741077
raise TypeError(
10751078
"The argument to periodic_table_block must be "
10761079
"either a string representing the element or its "
@@ -1080,7 +1083,7 @@ def periodic_table_block(argument: Union[str, numbers.Integral]) -> str:
10801083
return block
10811084

10821085

1083-
def periodic_table_category(argument: Union[str, numbers.Integral]) -> str:
1086+
def periodic_table_category(argument: Union[str, Integral]) -> str:
10841087
"""
10851088
Return the periodic table category.
10861089
@@ -1125,7 +1128,7 @@ def periodic_table_category(argument: Union[str, numbers.Integral]) -> str:
11251128
11261129
"""
11271130
# TODO: Implement @particle_input
1128-
if not isinstance(argument, (str, numbers.Integral)):
1131+
if not isinstance(argument, (str, Integral)):
11291132
raise TypeError(
11301133
"The argument to periodic_table_category must be "
11311134
"either a string representing the element or its "

‎plasmapy/atomic/parsing.py‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import re
55
import warnings
66
from typing import (Union, Dict)
7-
import numbers
7+
from numbers import Integral
88

99
from plasmapy.atomic.elements import (
1010
_atomic_numbers_to_symbols,
@@ -18,7 +18,8 @@
1818
AtomicWarning,
1919
InvalidElementError,
2020
InvalidParticleError,
21-
roman)
21+
roman
22+
)
2223

2324

2425
def _create_alias_dicts(Particles: dict) -> (Dict[str, str], Dict[str, str]):
@@ -86,7 +87,7 @@ def _create_alias_dicts(Particles: dict) -> (Dict[str, str], Dict[str, str]):
8687
_case_sensitive_aliases, _case_insensitive_aliases = _create_alias_dicts(_Particles)
8788

8889

89-
def _dealias_particle_aliases(alias: Union[str, numbers.Integral]) -> str:
90+
def _dealias_particle_aliases(alias: Union[str, Integral]) -> str:
9091
"""
9192
Return the standard symbol for a particle or antiparticle
9293
when the argument is a valid alias. If the argument is not a
@@ -127,9 +128,9 @@ def _invalid_particle_errmsg(argument, mass_numb=None, Z=None):
127128

128129

129130
def _parse_and_check_atomic_input(
130-
argument: Union[str, numbers.Integral],
131-
mass_numb: numbers.Integral = None,
132-
Z: numbers.Integral = None):
131+
argument: Union[str, Integral],
132+
mass_numb: Integral = None,
133+
Z: Integral = None):
133134
"""
134135
Parse information about a particle into a dictionary of standard
135136
symbols, and check the validity of the particle.
@@ -175,7 +176,7 @@ def _parse_and_check_atomic_input(
175176
176177
"""
177178

178-
def _atomic_number_to_symbol(atomic_numb: numbers.Integral):
179+
def _atomic_number_to_symbol(atomic_numb: Integral):
179180
"""
180181
Return the atomic symbol associated with an integer
181182
representing an atomic number, or raises an
@@ -295,7 +296,7 @@ def _get_element(element_info: str) -> str:
295296

296297
return element
297298

298-
def _reconstruct_isotope_symbol(element: str, mass_numb: numbers.Integral) -> str:
299+
def _reconstruct_isotope_symbol(element: str, mass_numb: Integral) -> str:
299300
"""
300301
Receive a `str` representing an atomic symbol and an
301302
`int` representing a mass number. Return the isotope symbol
@@ -322,8 +323,7 @@ def _reconstruct_isotope_symbol(element: str, mass_numb: numbers.Integral) -> st
322323

323324
return isotope
324325

325-
def _reconstruct_ion_symbol(
326-
element: str, isotope: numbers.Integral = None, Z: numbers.Integral = None):
326+
def _reconstruct_ion_symbol(element: str, isotope: Integral = None, Z: Integral = None):
327327
"""
328328
Receive a `str` representing an atomic symbol and/or a
329329
string representing an isotope, and an `int` representing the
@@ -351,7 +351,7 @@ def _reconstruct_ion_symbol(
351351

352352
return ion
353353

354-
if not isinstance(argument, (str, numbers.Integral)): # coverage: ignore
354+
if not isinstance(argument, (str, Integral)): # coverage: ignore
355355
raise TypeError(f"The argument {argument} is not an integer or string.")
356356

357357
arg = _dealias_particle_aliases(argument)
@@ -367,7 +367,7 @@ def _reconstruct_ion_symbol(
367367
if isinstance(arg, str) and arg.isdigit():
368368
arg = int(arg)
369369

370-
if isinstance(arg, numbers.Integral):
370+
if isinstance(arg, Integral):
371371
element = _atomic_number_to_symbol(arg)
372372
Z_from_arg = None
373373
mass_numb_from_arg = None
@@ -403,7 +403,7 @@ def _reconstruct_ion_symbol(
403403
if Z_from_arg is not None:
404404
Z = Z_from_arg
405405

406-
if isinstance(Z, numbers.Integral):
406+
if isinstance(Z, Integral):
407407
if Z > _Elements[element]['atomic number']:
408408
raise InvalidParticleError(
409409
f"The integer charge Z = {Z} cannot exceed the atomic number "
@@ -435,7 +435,7 @@ def _reconstruct_ion_symbol(
435435
return nomenclature_dict
436436

437437

438-
def _call_string(arg: Union[str, numbers.Integral], kwargs: Dict = {}) -> str:
438+
def _call_string(arg: Union[str, Integral], kwargs: Dict = {}) -> str:
439439
"""
440440
Return a `str` that recreates the call to create a particular
441441
particle from the input.

‎plasmapy/atomic/particle_class.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import warnings
44
from typing import (Union, Set, Tuple, List, Optional)
5-
import collections
5+
from collections import defaultdict, namedtuple
66
from numbers import Integral, Real
77

88
import numpy as np
@@ -302,7 +302,7 @@ def __init__(
302302
if Z is not None and not isinstance(Z, Integral):
303303
raise TypeError("Z is not an integer.")
304304

305-
self._attributes = collections.defaultdict(lambda: None)
305+
self._attributes = defaultdict(lambda: None)
306306
attributes = self._attributes
307307

308308
# Use this set to keep track of particle categories such as
@@ -1264,7 +1264,7 @@ def spin(self) -> Real:
12641264
return self._attributes['spin']
12651265

12661266
@property
1267-
def periodic_table(self) -> collections.namedtuple:
1267+
def periodic_table(self) -> namedtuple:
12681268
"""
12691269
Return a `~collections.namedtuple` to access category, period,
12701270
group, and block information about an element.

‎plasmapy/atomic/symbols.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
from .particle_class import Particle
99
from .particle_input import particle_input
10-
import numbers
10+
11+
from numbers import Integral
1112

1213
# The @particle_input decorator takes the inputs for a function or
1314
# method and passes through the corresponding instance of the Particle
@@ -96,7 +97,7 @@ def atomic_symbol(element: Particle) -> str:
9697

9798

9899
@particle_input
99-
def isotope_symbol(isotope: Particle, mass_numb: Optional[numbers.Integral] = None) -> str:
100+
def isotope_symbol(isotope: Particle, mass_numb: Optional[Integral] = None) -> str:
100101
"""
101102
Return the symbol representing an isotope.
102103
@@ -157,8 +158,8 @@ def isotope_symbol(isotope: Particle, mass_numb: Optional[numbers.Integral] = No
157158
@particle_input(require="element", any_of=('charged', 'uncharged'))
158159
def ionic_symbol(
159160
particle: Particle,
160-
mass_numb: numbers.Integral = None,
161-
Z: numbers.Integral = None) -> str:
161+
mass_numb: Integral = None,
162+
Z: Integral = None) -> str:
162163
"""
163164
Return the ionic symbol of an ion or neutral atom.
164165
@@ -225,8 +226,8 @@ def ionic_symbol(
225226
@particle_input
226227
def particle_symbol(
227228
particle: Particle,
228-
mass_numb: numbers.Integral = None,
229-
Z: numbers.Integral = None) -> str:
229+
mass_numb: Integral = None,
230+
Z: Integral = None) -> str:
230231
"""
231232
Return the symbol of a particle.
232233

‎plasmapy/utils/checks.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import functools
22
import inspect
3-
import typing
3+
from typing import Dict
44

55
import numpy as np
66
from astropy import units as u
@@ -17,7 +17,7 @@
1717
]
1818

1919

20-
def check_quantity(**validations: typing.Dict[str, bool]):
20+
def check_quantity(**validations: Dict[str, bool]):
2121
"""
2222
Verify that the function's arguments have correct units.
2323

0 commit comments

Comments
 (0)