Skip to content

Commit

Permalink
Add Optional type hint for arguments with a default value of None (#556)
Browse files Browse the repository at this point in the history
* Add Optional type hints

* Add Optional code style documentation
  • Loading branch information
lgoenner authored and StanczakDominik committed Oct 7, 2018
1 parent 07eb8d8 commit f0df71f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
3 changes: 3 additions & 0 deletions docs/development/code_guide.rst
Expand Up @@ -49,6 +49,9 @@ Coding Style
* For multiline imports, instead of using ``\`` to break lines, wrap the
imported names in ``()`` parentheses and use consistent whitespace.

* Use ``Optional[type]`` for type hinted keyword arguments with a default value of
``None``.

* There should be at most one pun per 1284 lines of code.

Branches, commits, and pull requests
Expand Down
6 changes: 3 additions & 3 deletions plasmapy/atomic/atomic.py
Expand Up @@ -260,7 +260,7 @@ def particle_mass(


@particle_input
def isotopic_abundance(isotope: Particle, mass_numb: numbers.Integral = None) -> numbers.Real:
def isotopic_abundance(isotope: Particle, mass_numb: Optional[numbers.Integral] = None) -> numbers.Real:
"""
Return the isotopic abundances if known, and otherwise zero.
Expand Down Expand Up @@ -422,7 +422,7 @@ def electric_charge(particle: Particle) -> u.Quantity:


@particle_input
def is_stable(particle: Particle, mass_numb: numbers.Integral = None) -> bool:
def is_stable(particle: Particle, mass_numb: Optional[numbers.Integral] = None) -> bool:
"""
Return `True` for stable isotopes and particles and `False` for
unstable isotopes.
Expand Down Expand Up @@ -476,7 +476,7 @@ def is_stable(particle: Particle, mass_numb: numbers.Integral = None) -> bool:


@particle_input(any_of={'stable', 'unstable', 'isotope'})
def half_life(particle: Particle, mass_numb: numbers.Integral = None) -> u.Quantity:
def half_life(particle: Particle, mass_numb: Optional[numbers.Integral] = None) -> u.Quantity:
"""
Return the half-life in seconds for unstable isotopes and particles,
and numpy.inf in seconds for stable isotopes and particles.
Expand Down
6 changes: 3 additions & 3 deletions plasmapy/atomic/nuclear.py
@@ -1,7 +1,7 @@
"""Functions that are related to nuclear reactions."""

from astropy import units as u
from typing import List, Union
from typing import List, Union, Optional
import re

from plasmapy.utils import (
Expand All @@ -20,7 +20,7 @@


@particle_input(any_of={'isotope', 'baryon'})
def nuclear_binding_energy(particle: Particle, mass_numb: int = None) -> u.Quantity:
def nuclear_binding_energy(particle: Particle, mass_numb: Optional[int] = None) -> u.Quantity:
"""
Return the nuclear binding energy associated with an isotope.
Expand Down Expand Up @@ -79,7 +79,7 @@ def nuclear_binding_energy(particle: Particle, mass_numb: int = None) -> u.Quant


@particle_input
def mass_energy(particle: Particle, mass_numb: int = None) -> u.Quantity:
def mass_energy(particle: Particle, mass_numb: Optional[int] = None) -> u.Quantity:
"""
Return a particle's mass energy. If the particle is an isotope or
nuclide, return the nuclear mass energy only.
Expand Down
4 changes: 3 additions & 1 deletion plasmapy/atomic/symbols.py
Expand Up @@ -3,6 +3,8 @@
and numbers.
"""

from typing import Optional

from .particle_class import Particle
from .particle_input import particle_input
import numbers
Expand Down Expand Up @@ -94,7 +96,7 @@ def atomic_symbol(element: Particle) -> str:


@particle_input
def isotope_symbol(isotope: Particle, mass_numb: numbers.Integral = None) -> str:
def isotope_symbol(isotope: Particle, mass_numb: Optional[numbers.Integral] = None) -> str:
"""
Return the symbol representing an isotope.
Expand Down
4 changes: 3 additions & 1 deletion plasmapy/physics/parameters.py
Expand Up @@ -3,6 +3,8 @@
plasma frequency or Debye length.
"""

from typing import Optional

__all__ = [
"mass_density",
"Alfven_speed",
Expand Down Expand Up @@ -64,7 +66,7 @@ def _grab_charge(ion, z_mean=None):
return Z


def mass_density(density, particle: str = None, z_mean: numbers.Real = None) -> u.kg / u.m ** 3:
def mass_density(density, particle: Optional[str] = None, z_mean: Optional[numbers.Real] = None) -> u.kg / u.m ** 3:
"""Utility function to merge two possible inputs for particle charge.
Parameters
Expand Down

0 comments on commit f0df71f

Please sign in to comment.