From c15a5701395a2fdd6359786a5a7d800b6dba8b91 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 14 Jan 2022 04:48:58 -0500 Subject: [PATCH] feat: drop Python 2 (#57) * feat: drop Python 2 Fixes static typing support from outer layers - only full paths were supported before. Since this doesn't change often, listing things out is the best static solution. Added tests to make sure nothing gets missed and everything is included and remembered when anything gets changed or added. Added noxfile for easy running. Extended static checks. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .github/workflows/main.yml | 4 +- .gitignore | 1 + .pre-commit-config.yaml | 47 ++- MANIFEST.in | 2 +- README.rst | 4 +- noxfile.py | 19 ++ pyproject.toml | 29 +- setup.cfg | 81 ++--- setup.py | 1 - src/hepunits/__init__.py | 495 +++++++++++++++++++++++++++- src/hepunits/_version.pyi | 2 + src/hepunits/constants/__init__.py | 57 +++- src/hepunits/constants/constants.py | 34 +- src/hepunits/constants/py.typed | 0 src/hepunits/units/__init__.py | 453 ++++++++++++++++++++++++- src/hepunits/units/prefixes.py | 40 ++- src/hepunits/units/py.typed | 0 src/hepunits/units/units.py | 202 +++++++++++- tests/constants/test_constants.py | 3 +- tests/test_missing_all.py | 53 +++ tests/units/test_prefixes.py | 7 +- tests/units/test_units.py | 1 - 22 files changed, 1438 insertions(+), 97 deletions(-) create mode 100644 noxfile.py create mode 100644 src/hepunits/_version.pyi delete mode 100644 src/hepunits/constants/py.typed delete mode 100644 src/hepunits/units/py.typed create mode 100644 tests/test_missing_all.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2fa22d6..9f9ff15 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -24,8 +24,7 @@ jobs: fail-fast: false matrix: python-version: - - "2.7" - - "3.5" + - "3.6" - "3.8" - "3.9" - "3.10" @@ -46,5 +45,4 @@ jobs: run: python -m pytest --doctest-modules --cov=src/hepunits --cov-report=xml - name: Test coverage with Codecov - if: matrix.python-version != '3.5' && matrix.python-version != '3.8' uses: codecov/codecov-action@v2 diff --git a/.gitignore b/.gitignore index 9b72556..f102fcc 100644 --- a/.gitignore +++ b/.gitignore @@ -71,3 +71,4 @@ docs/_build /.mypy_cache/* /pip-wheel-metadata/* /src/hepunits/version.py +/src/hepunits/_version.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d7c8343..67d1102 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,16 +1,10 @@ repos: -- repo: https://github.com/psf/black - rev: 21.12b0 - hooks: - - id: black - - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.1.0 hooks: - id: debug-statements - id: end-of-file-fixer - - id: fix-encoding-pragma - id: mixed-line-ending - id: requirements-txt-fixer - id: trailing-whitespace @@ -20,6 +14,17 @@ repos: - id: check-symlinks - id: check-yaml +- repo: https://github.com/asottile/pyupgrade + rev: v2.31.0 + hooks: + - id: pyupgrade + args: [--py36-plus] + +- repo: https://github.com/psf/black + rev: 21.12b0 + hooks: + - id: black-jupyter + - repo: https://github.com/mgedmin/check-manifest rev: "0.47" hooks: @@ -36,10 +41,32 @@ repos: rev: v0.931 hooks: - id: mypy - args: [--strict] files: src + args: [--show-error-codes] -- repo: https://github.com/asottile/pyupgrade - rev: v2.31.0 +- repo: https://github.com/asottile/setup-cfg-fmt + rev: v1.20.0 hooks: - - id: pyupgrade + - id: setup-cfg-fmt + +- repo: https://github.com/PyCQA/isort + rev: 5.10.1 + hooks: + - id: isort + +- repo: https://github.com/codespell-project/codespell + rev: v2.1.0 + hooks: + - id: codespell + +- repo: https://github.com/pre-commit/pygrep-hooks + rev: v1.9.0 + hooks: + - id: python-check-blanket-noqa + - id: python-check-blanket-type-ignore + - id: python-no-log-warn + - id: python-no-eval + - id: python-use-type-annotations + - id: rst-backticks + - id: rst-directive-colons + - id: rst-inline-touching-normal diff --git a/MANIFEST.in b/MANIFEST.in index 57bb7b9..939fe5e 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -5,6 +5,6 @@ include pyproject.toml recursive-include tests *.py # Type support -recursive-include src py.typed +recursive-include src py.typed *.pyi exclude .pre-commit-config.yaml codecov.yml diff --git a/README.rst b/README.rst index 9e65bb2..bba62ef 100644 --- a/README.rst +++ b/README.rst @@ -136,10 +136,10 @@ These simple rules are enough - exemplified in the code below: - Dimensioned quantities in the "data stores" abide to the HEP system of units. - All definitions of dimensioned quantities are dimensioned by multiplying by the units, - as in `mass_window = 500 * keV`. + as in ``mass_window = 500 * keV``. - All output of dimensioned quantities is converted to the required units - by dividing by the units, as in `energy_resolution() / GeV`. + by dividing by the units, as in ``energy_resolution() / GeV``. For the sake of argument, let's consider below a function returning a dimensioned quantity. the function below stores a dimensioned quantity defined in keV diff --git a/noxfile.py b/noxfile.py new file mode 100644 index 0000000..0219f0d --- /dev/null +++ b/noxfile.py @@ -0,0 +1,19 @@ +import nox + + +@nox.session +def lint(session: nox.Session) -> None: + """ + Run the linter. + """ + session.install("pre-commit") + session.run("pre-commit", "run", "--all-files", *session.posargs) + + +@nox.session(python=["3.6", "3.7", "3.8", "3.9", "3.10"]) +def tests(session: nox.Session) -> None: + """ + Run the unit and regular tests. + """ + session.install(".[test]") + session.run("pytest", *session.posargs) diff --git a/pyproject.toml b/pyproject.toml index 3392c13..47e06f5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,10 +6,35 @@ requires = [ ] build-backend = "setuptools.build_meta" + [tool.setuptools_scm] -write_to = "src/hepunits/version.py" +write_to = "src/hepunits/_version.py" + + +[tool.isort] +profile = "black" + + +[tool.pytest.ini_options] +minversion = "6.0" +junit_family = "xunit2" +testpaths = ["tests"] +filterwarnings = ["error"] +addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config"] +xfail_strict = true +log_cli_level = "DEBUG" + [tool.check-manifest] ignore = [ - "src/hepunits/version.py", + "src/hepunits/_version.py", + ".pre-commit-config.yaml", + "noxfile.py", ] + + +[tool.mypy] +warn_unused_configs = true +python_version = "3.6" +files = ["src"] +strict = true diff --git a/setup.cfg b/setup.cfg index 2e2c899..5c52b28 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,90 +1,63 @@ -[bdist_wheel] -universal=1 - - [metadata] name = hepunits -author = Eduardo Rodrigues -author_email = eduardo.rodrigues@cern.ch -maintainer = Eduardo Rodrigues -maintainer_email = eduardo.rodrigues@cern.ch description = Units and constants in the HEP system of units long_description = file: README.rst long_description_content_type = text/x-rst url = https://github.com/scikit-hep/hepunits +author = Eduardo Rodrigues +author_email = eduardo.rodrigues@cern.ch +maintainer = Eduardo Rodrigues +maintainer_email = eduardo.rodrigues@cern.ch license = BSD-3-Clause -classifier = - Topic :: Scientific/Engineering - Intended Audience :: Science/Research +license_file = LICENSE +classifiers = + Development Status :: 5 - Production/Stable Intended Audience :: Developers - Operating System :: OS Independent + Intended Audience :: Science/Research License :: OSI Approved :: BSD License + Operating System :: OS Independent Programming Language :: Python - Programming Language :: Python :: 2 - Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 - Programming Language :: Python :: 3.5 + Programming Language :: Python :: 3 :: Only Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.9 Programming Language :: Python :: 3.10 - Development Status :: 5 - Production/Stable + Topic :: Scientific/Engineering keywords = HEP HEP system of units Units Constants - [options] -python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.* +packages = find: +python_requires = >=3.6 include_package_data = True -zip_safe = False -packages: find: package_dir = =src - - +zip_safe = False [options.packages.find] where = src - -[options.package_data] -* = py.typed - -[tool:pytest] -junit_family=xunit2 -testpaths = - tests -addopts = -Wd - - [options.extras_require] -test = - pytest >=4.6 - pytest-cov >=2.8.0 -dev = - pytest >=4.6 - pytest-cov >=2.8.0 all = - pytest >=4.6 - pytest-cov >=2.8.0 - -[check-manifest] -ignore = - src/hepunits/version.py - .pre-commit-config.yaml - -[mypy] -strict=True - -[mypy-hepunits.version] -ignore_missing_imports = True + pytest>=6 + pytest-cov>=2.8.0 +dev = + pytest>=6 + pytest-cov>=2.8.0 +test = + pytest>=6 + pytest-cov>=2.8.0 +[options.package_data] +* = py.typed [flake8] -max-complexity = 12 -ignore = E203, E231, E501, E722, W503, F401, F403, F405 +extend-ignore = B950, E501 select = C,E,F,W,B,B9,T +per-file-ignores = + tests/*: T, F405, F403 diff --git a/setup.py b/setup.py index ced15ef..d1c1fd0 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # Licensed under a 3-clause BSD style license, see LICENSE. from setuptools import setup diff --git a/src/hepunits/__init__.py b/src/hepunits/__init__.py index d42f95f..9a7b6c3 100644 --- a/src/hepunits/__init__.py +++ b/src/hepunits/__init__.py @@ -1,9 +1,496 @@ -# -*- coding: utf-8 -*- # Licensed under a 3-clause BSD style license, see LICENSE. # Convenient access to the version number -from .version import version as __version__ +from typing import List + +from . import constants, units +from ._version import version as __version__ +from .constants.constants import ( + Avogadro, + c_light, + c_light_sq, + e_sq, + eminus, + h_Planck, + half_pi, + hbar, + hbar_Planck, + hbarc, + hbarc_sq, + k_Boltzmann, + pi, + pi_sq, + two_pi, +) +from .units.prefixes import ( + atto, + centi, + deca, + deci, + exa, + exbi, + femto, + gibi, + giga, + googol, + hecto, + kibi, + kilo, + mebi, + mega, + micro, + milli, + nano, + pebi, + peta, + pico, + tebi, + tera, + yobi, + yocto, + yotta, + zebi, + zepto, + zetta, +) +from .units.units import ( + GW, + MW, + A, + Bq, + Ci, + EeV, + GBq, + GeV, + GHz, + Gy, + Hz, + J, + MBq, + MeV, + MGy, + MHz, + N, + Pa, + PeV, + Sv, + TeV, + THz, + W, + ZeV, + ab, + ampere, + angstrom, + atmosphere, + attobarn, + attosecond, + bar, + barn, + becquerel, + candela, + cd, + centimeter, + centimeter2, + centimeter3, + cm, + cm2, + cm3, + coulomb, + curie, + d, + day, + deg, + degree, + dyne, + e_SI, + electronvolt, + eplus, + erg, + eV, + exaelectronvolt, + farad, + fb, + femtobarn, + femtometer, + femtosecond, + fermi, + fm, + fm2, + fm3, + fs, + g, + gauss, + gigabecquerel, + gigaelectronvolt, + gigahertz, + gram, + gray, + h, + henry, + hertz, + hour, + invab, + invfb, + invmb, + invnb, + invpb, + invub, + joule, + kBq, + kelvin, + keV, + kg, + kGy, + kHz, + kilobecquerel, + kiloelectronvolt, + kilogauss, + kilogram, + kilogray, + kilohertz, + kilometer, + kilometer2, + kilometer3, + kilovolt, + km, + km2, + km3, + kW, + lumen, + lux, + m, + m2, + m3, + mb, + mCi, + megabecquerel, + megaelectronvolt, + megagray, + megahertz, + megavolt, + meter, + meter2, + meter3, + mg, + mGy, + microampere, + microbarn, + microcurie, + microfarad, + microgray, + micrometer, + micron, + microsecond, + milliampere, + millibarn, + millicurie, + millifarad, + milligram, + milligray, + millimeter, + millimeter2, + millimeter3, + milliradian, + millisecond, + minute, + mm, + mm2, + mm3, + mol, + mole, + mrad, + ms, + nanoampere, + nanobarn, + nanocurie, + nanofarad, + nanometer, + nanosecond, + nb, + nCi, + newton, + ns, + ohm, + pascal, + pb, + petaelectronvolt, + picobarn, + picofarad, + picosecond, + ps, + rad, + radian, + s, + second, + sievert, + sr, + steradian, + teraelectronvolt, + terahertz, + tesla, + ub, + uCi, + uGy, + us, + volt, + watt, + weber, + y, + year, + yoctosecond, + ys, + zeptosecond, + zettaelectronvolt, + zs, +) # Units and constants directly available -from .units import * -from .constants import * + + +__all__ = ( + "units", + "constants", + "atto", + "centi", + "deca", + "deci", + "exa", + "exbi", + "femto", + "gibi", + "giga", + "googol", + "hecto", + "kibi", + "kilo", + "mebi", + "mega", + "micro", + "milli", + "nano", + "pebi", + "peta", + "pico", + "tebi", + "tera", + "yobi", + "yocto", + "yotta", + "zebi", + "zepto", + "zetta", + "A", + "Bq", + "Ci", + "EeV", + "GBq", + "GHz", + "GW", + "GeV", + "Gy", + "Hz", + "J", + "MBq", + "MGy", + "MHz", + "MW", + "MeV", + "N", + "Pa", + "PeV", + "Sv", + "THz", + "TeV", + "W", + "ZeV", + "ab", + "ampere", + "angstrom", + "atmosphere", + "attobarn", + "attosecond", + "bar", + "barn", + "becquerel", + "candela", + "cd", + "centimeter", + "centimeter2", + "centimeter3", + "cm", + "cm2", + "cm3", + "coulomb", + "curie", + "d", + "day", + "deg", + "degree", + "dyne", + "eV", + "e_SI", + "electronvolt", + "eplus", + "erg", + "exaelectronvolt", + "farad", + "fb", + "femtobarn", + "femtometer", + "femtosecond", + "fermi", + "fm", + "fm2", + "fm3", + "fs", + "g", + "gauss", + "gigabecquerel", + "gigaelectronvolt", + "gigahertz", + "gram", + "gray", + "h", + "henry", + "hertz", + "hour", + "invab", + "invfb", + "invmb", + "invnb", + "invpb", + "invub", + "joule", + "kBq", + "kGy", + "kHz", + "kW", + "keV", + "kelvin", + "kg", + "kilobecquerel", + "kiloelectronvolt", + "kilogauss", + "kilogram", + "kilogray", + "kilohertz", + "kilometer", + "kilometer2", + "kilometer3", + "kilovolt", + "km", + "km2", + "km3", + "lumen", + "lux", + "m", + "m2", + "m3", + "mCi", + "mGy", + "mb", + "megabecquerel", + "megaelectronvolt", + "megagray", + "megahertz", + "megavolt", + "meter", + "meter2", + "meter3", + "mg", + "microampere", + "microbarn", + "microcurie", + "microfarad", + "microgray", + "micrometer", + "micron", + "microsecond", + "milliampere", + "millibarn", + "millicurie", + "millifarad", + "milligram", + "milligray", + "millimeter", + "millimeter2", + "millimeter3", + "milliradian", + "millisecond", + "minute", + "mm", + "mm2", + "mm3", + "mol", + "mole", + "mrad", + "ms", + "nCi", + "nanoampere", + "nanobarn", + "nanocurie", + "nanofarad", + "nanometer", + "nanosecond", + "nb", + "newton", + "ns", + "ohm", + "pascal", + "pb", + "petaelectronvolt", + "picobarn", + "picofarad", + "picosecond", + "ps", + "rad", + "radian", + "s", + "second", + "sievert", + "sr", + "steradian", + "teraelectronvolt", + "terahertz", + "tesla", + "uCi", + "uGy", + "ub", + "us", + "volt", + "watt", + "weber", + "y", + "year", + "yoctosecond", + "ys", + "zeptosecond", + "zettaelectronvolt", + "zs", + "m", + "s", + "eplus", + "mole", + "joule", + "kelvin", + "c_light", + "c_light_sq", + "pi", + "two_pi", + "half_pi", + "pi_sq", + "eminus", + "e_sq", + "Avogadro", + "h_Planck", + "hbar_Planck", + "hbar", + "hbarc", + "hbarc_sq", + "k_Boltzmann", + "__version__", +) + + +def __dir__() -> List[str]: + return list(__all__) diff --git a/src/hepunits/_version.pyi b/src/hepunits/_version.pyi new file mode 100644 index 0000000..5bb2b22 --- /dev/null +++ b/src/hepunits/_version.pyi @@ -0,0 +1,2 @@ +version: str +version_tuple: tuple[int, int, int] | tuple[int, int, int, str, str] diff --git a/src/hepunits/constants/__init__.py b/src/hepunits/constants/__init__.py index 2a5d84c..1c3957a 100644 --- a/src/hepunits/constants/__init__.py +++ b/src/hepunits/constants/__init__.py @@ -1,4 +1,57 @@ -# -*- coding: utf-8 -*- # Licensed under a 3-clause BSD style license, see LICENSE. -from .constants import * +from typing import List + +from . import constants +from .constants import ( + Avogadro, + c_light, + c_light_sq, + e_sq, + eminus, + eplus, + h_Planck, + half_pi, + hbar, + hbar_Planck, + hbarc, + hbarc_sq, + joule, + k_Boltzmann, + kelvin, + m, + mole, + pi, + pi_sq, + s, + two_pi, +) + +__all__ = ( + "constants", + "m", + "s", + "eplus", + "mole", + "joule", + "kelvin", + "c_light", + "c_light_sq", + "pi", + "two_pi", + "half_pi", + "pi_sq", + "eminus", + "e_sq", + "Avogadro", + "h_Planck", + "hbar_Planck", + "hbar", + "hbarc", + "hbarc_sq", + "k_Boltzmann", +) + + +def __dir__() -> List[str]: + return list(__all__) diff --git a/src/hepunits/constants/constants.py b/src/hepunits/constants/constants.py index 8d082f1..b52f06f 100644 --- a/src/hepunits/constants/constants.py +++ b/src/hepunits/constants/constants.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Licensed under a 3-clause BSD style license, see LICENSE. """ Physical and other handy constants @@ -28,11 +27,36 @@ # ----------------------------------------------------------------------------- # Import statements # ----------------------------------------------------------------------------- -from __future__ import absolute_import from math import pi +from typing import List + +from ..units.units import eplus, joule, kelvin, m, mole, s + +__all__ = ( + "m", + "s", + "eplus", + "mole", + "joule", + "kelvin", + "c_light", + "c_light_sq", + "pi", + "two_pi", + "half_pi", + "pi_sq", + "eminus", + "e_sq", + "Avogadro", + "h_Planck", + "hbar_Planck", + "hbar", + "hbarc", + "hbarc_sq", + "k_Boltzmann", +) -from ..units.units import m, s, eplus, mole, joule, kelvin # ----------------------------------------------------------------------------- # Mathematical constants @@ -69,3 +93,7 @@ # Boltzmann constant (exact value, taken from PDG 2020) k_Boltzmann = 1.380649e-23 * joule / kelvin + + +def __dir__() -> List[str]: + return list(__all__) diff --git a/src/hepunits/constants/py.typed b/src/hepunits/constants/py.typed deleted file mode 100644 index e69de29..0000000 diff --git a/src/hepunits/units/__init__.py b/src/hepunits/units/__init__.py index b47a232..b8b11ef 100644 --- a/src/hepunits/units/__init__.py +++ b/src/hepunits/units/__init__.py @@ -1,5 +1,452 @@ -# -*- coding: utf-8 -*- # Licensed under a 3-clause BSD style license, see LICENSE. -from .prefixes import * -from .units import * +from typing import List + +from . import prefixes, units +from .prefixes import ( + atto, + centi, + deca, + deci, + exa, + exbi, + femto, + gibi, + giga, + googol, + hecto, + kibi, + kilo, + mebi, + mega, + micro, + milli, + nano, + pebi, + peta, + pico, + tebi, + tera, + yobi, + yocto, + yotta, + zebi, + zepto, + zetta, +) +from .units import ( + GW, + MW, + A, + Bq, + Ci, + EeV, + GBq, + GeV, + GHz, + Gy, + Hz, + J, + MBq, + MeV, + MGy, + MHz, + N, + Pa, + PeV, + Sv, + TeV, + THz, + W, + ZeV, + ab, + ampere, + angstrom, + atmosphere, + attobarn, + attosecond, + bar, + barn, + becquerel, + candela, + cd, + centimeter, + centimeter2, + centimeter3, + cm, + cm2, + cm3, + coulomb, + curie, + d, + day, + deg, + degree, + dyne, + e_SI, + electronvolt, + eplus, + erg, + eV, + exaelectronvolt, + farad, + fb, + femtobarn, + femtometer, + femtosecond, + fermi, + fm, + fm2, + fm3, + fs, + g, + gauss, + gigabecquerel, + gigaelectronvolt, + gigahertz, + gram, + gray, + h, + henry, + hertz, + hour, + invab, + invfb, + invmb, + invnb, + invpb, + invub, + joule, + kBq, + kelvin, + keV, + kg, + kGy, + kHz, + kilobecquerel, + kiloelectronvolt, + kilogauss, + kilogram, + kilogray, + kilohertz, + kilometer, + kilometer2, + kilometer3, + kilovolt, + km, + km2, + km3, + kW, + lumen, + lux, + m, + m2, + m3, + mb, + mCi, + megabecquerel, + megaelectronvolt, + megagray, + megahertz, + megavolt, + meter, + meter2, + meter3, + mg, + mGy, + microampere, + microbarn, + microcurie, + microfarad, + microgray, + micrometer, + micron, + microsecond, + milliampere, + millibarn, + millicurie, + millifarad, + milligram, + milligray, + millimeter, + millimeter2, + millimeter3, + milliradian, + millisecond, + minute, + mm, + mm2, + mm3, + mol, + mole, + mrad, + ms, + nanoampere, + nanobarn, + nanocurie, + nanofarad, + nanometer, + nanosecond, + nb, + nCi, + newton, + ns, + ohm, + pascal, + pb, + petaelectronvolt, + picobarn, + picofarad, + picosecond, + ps, + rad, + radian, + s, + second, + sievert, + sr, + steradian, + teraelectronvolt, + terahertz, + tesla, + ub, + uCi, + uGy, + us, + volt, + watt, + weber, + y, + year, + yoctosecond, + ys, + zeptosecond, + zettaelectronvolt, + zs, +) + +__all__ = ( + "prefixes", + "units", + "atto", + "centi", + "deca", + "deci", + "exa", + "exbi", + "femto", + "gibi", + "giga", + "googol", + "hecto", + "kibi", + "kilo", + "mebi", + "mega", + "micro", + "milli", + "nano", + "pebi", + "peta", + "pico", + "tebi", + "tera", + "yobi", + "yocto", + "yotta", + "zebi", + "zepto", + "zetta", + "A", + "Bq", + "Ci", + "EeV", + "GBq", + "GHz", + "GW", + "GeV", + "Gy", + "Hz", + "J", + "MBq", + "MGy", + "MHz", + "MW", + "MeV", + "N", + "Pa", + "PeV", + "Sv", + "THz", + "TeV", + "W", + "ZeV", + "ab", + "ampere", + "angstrom", + "atmosphere", + "attobarn", + "attosecond", + "bar", + "barn", + "becquerel", + "candela", + "cd", + "centimeter", + "centimeter2", + "centimeter3", + "cm", + "cm2", + "cm3", + "coulomb", + "curie", + "d", + "day", + "deg", + "degree", + "dyne", + "eV", + "e_SI", + "electronvolt", + "eplus", + "erg", + "exaelectronvolt", + "farad", + "fb", + "femtobarn", + "femtometer", + "femtosecond", + "fermi", + "fm", + "fm2", + "fm3", + "fs", + "g", + "gauss", + "gigabecquerel", + "gigaelectronvolt", + "gigahertz", + "gram", + "gray", + "h", + "henry", + "hertz", + "hour", + "invab", + "invfb", + "invmb", + "invnb", + "invpb", + "invub", + "joule", + "kBq", + "kGy", + "kHz", + "kW", + "keV", + "kelvin", + "kg", + "kilobecquerel", + "kiloelectronvolt", + "kilogauss", + "kilogram", + "kilogray", + "kilohertz", + "kilometer", + "kilometer2", + "kilometer3", + "kilovolt", + "km", + "km2", + "km3", + "lumen", + "lux", + "m", + "m2", + "m3", + "mCi", + "mGy", + "mb", + "megabecquerel", + "megaelectronvolt", + "megagray", + "megahertz", + "megavolt", + "meter", + "meter2", + "meter3", + "mg", + "microampere", + "microbarn", + "microcurie", + "microfarad", + "microgray", + "micrometer", + "micron", + "microsecond", + "milliampere", + "millibarn", + "millicurie", + "millifarad", + "milligram", + "milligray", + "millimeter", + "millimeter2", + "millimeter3", + "milliradian", + "millisecond", + "minute", + "mm", + "mm2", + "mm3", + "mol", + "mole", + "mrad", + "ms", + "nCi", + "nanoampere", + "nanobarn", + "nanocurie", + "nanofarad", + "nanometer", + "nanosecond", + "nb", + "newton", + "ns", + "ohm", + "pascal", + "pb", + "petaelectronvolt", + "picobarn", + "picofarad", + "picosecond", + "ps", + "rad", + "radian", + "s", + "second", + "sievert", + "sr", + "steradian", + "teraelectronvolt", + "terahertz", + "tesla", + "uCi", + "uGy", + "ub", + "us", + "volt", + "watt", + "weber", + "y", + "year", + "yoctosecond", + "ys", + "zeptosecond", + "zettaelectronvolt", + "zs", +) + + +def __dir__() -> List[str]: + return list(__all__) diff --git a/src/hepunits/units/prefixes.py b/src/hepunits/units/prefixes.py index a52933c..c85b346 100644 --- a/src/hepunits/units/prefixes.py +++ b/src/hepunits/units/prefixes.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Licensed under a 3-clause BSD style license, see LICENSE. """ Common unit prefixes @@ -15,9 +14,44 @@ .. [Binary_prefixes] https://en.wikipedia.org/wiki/Unit_prefix#Binary_prefixes. """ +__all__ = ( + "atto", + "centi", + "deca", + "deci", + "exa", + "exbi", + "femto", + "gibi", + "giga", + "googol", + "hecto", + "kibi", + "kilo", + "mebi", + "mega", + "micro", + "milli", + "nano", + "pebi", + "peta", + "pico", + "tebi", + "tera", + "yobi", + "yocto", + "yotta", + "zebi", + "zepto", + "zetta", +) + + # ----------------------------------------------------------------------------- # SI prefixes # ----------------------------------------------------------------------------- +from typing import List + yotta = 1.0e24 zetta = 1.0e21 exa = 1.0e18 @@ -55,3 +89,7 @@ # Miscellaneous prefixes # ----------------------------------------------------------------------------- googol = 1.0e100 + + +def __dir__() -> List[str]: + return list(__all__) diff --git a/src/hepunits/units/py.typed b/src/hepunits/units/py.typed deleted file mode 100644 index e69de29..0000000 diff --git a/src/hepunits/units/units.py b/src/hepunits/units/units.py index fd44b66..d8aa4f7 100644 --- a/src/hepunits/units/units.py +++ b/src/hepunits/units/units.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Licensed under a 3-clause BSD style license, see LICENSE. """ @@ -67,9 +66,9 @@ .. [SI] http://www.physics.nist.gov/cuu/Units/index.html. """ -from __future__ import absolute_import -from math import pi +from math import pi as _pi +from typing import List from . import prefixes as _pre @@ -250,7 +249,7 @@ milliradian = _pre.milli * radian mrad = milliradian -degree = (pi / 180.0) * radian +degree = (_pi / 180.0) * radian deg = degree @@ -405,3 +404,198 @@ sievert = joule / kilogram Sv = sievert + +__all__ = ( + "A", + "Bq", + "Ci", + "EeV", + "GBq", + "GHz", + "GW", + "GeV", + "Gy", + "Hz", + "J", + "MBq", + "MGy", + "MHz", + "MW", + "MeV", + "N", + "Pa", + "PeV", + "Sv", + "THz", + "TeV", + "W", + "ZeV", + "ab", + "ampere", + "angstrom", + "atmosphere", + "attobarn", + "attosecond", + "bar", + "barn", + "becquerel", + "candela", + "cd", + "centimeter", + "centimeter2", + "centimeter3", + "cm", + "cm2", + "cm3", + "coulomb", + "curie", + "d", + "day", + "deg", + "degree", + "dyne", + "eV", + "e_SI", + "electronvolt", + "eplus", + "erg", + "exaelectronvolt", + "farad", + "fb", + "femtobarn", + "femtometer", + "femtosecond", + "fermi", + "fm", + "fm2", + "fm3", + "fs", + "g", + "gauss", + "gigabecquerel", + "gigaelectronvolt", + "gigahertz", + "gram", + "gray", + "h", + "henry", + "hertz", + "hour", + "invab", + "invfb", + "invmb", + "invnb", + "invpb", + "invub", + "joule", + "kBq", + "kGy", + "kHz", + "kW", + "keV", + "kelvin", + "kg", + "kilobecquerel", + "kiloelectronvolt", + "kilogauss", + "kilogram", + "kilogray", + "kilohertz", + "kilometer", + "kilometer2", + "kilometer3", + "kilovolt", + "km", + "km2", + "km3", + "lumen", + "lux", + "m", + "m2", + "m3", + "mCi", + "mGy", + "mb", + "megabecquerel", + "megaelectronvolt", + "megagray", + "megahertz", + "megavolt", + "meter", + "meter2", + "meter3", + "mg", + "microampere", + "microbarn", + "microcurie", + "microfarad", + "microgray", + "micrometer", + "micron", + "microsecond", + "milliampere", + "millibarn", + "millicurie", + "millifarad", + "milligram", + "milligray", + "millimeter", + "millimeter2", + "millimeter3", + "milliradian", + "millisecond", + "minute", + "mm", + "mm2", + "mm3", + "mol", + "mole", + "mrad", + "ms", + "nCi", + "nanoampere", + "nanobarn", + "nanocurie", + "nanofarad", + "nanometer", + "nanosecond", + "nb", + "newton", + "ns", + "ohm", + "pascal", + "pb", + "petaelectronvolt", + "picobarn", + "picofarad", + "picosecond", + "ps", + "rad", + "radian", + "s", + "second", + "sievert", + "sr", + "steradian", + "teraelectronvolt", + "terahertz", + "tesla", + "uCi", + "uGy", + "ub", + "us", + "volt", + "watt", + "weber", + "y", + "year", + "yoctosecond", + "ys", + "zeptosecond", + "zettaelectronvolt", + "zs", +) + + +def __dir__() -> List[str]: + return list(__all__) diff --git a/tests/constants/test_constants.py b/tests/constants/test_constants.py index 9b78310..b9d7bb9 100644 --- a/tests/constants/test_constants.py +++ b/tests/constants/test_constants.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # Licensed under a 3-clause BSD style license, see LICENSE. """ Tests for the hepunits.constants.constants module. @@ -7,8 +6,8 @@ from pytest import approx -from hepunits import eV, nanometer, s, THz from hepunits import * +from hepunits import THz, eV, nanometer, s def test_constants(): diff --git a/tests/test_missing_all.py b/tests/test_missing_all.py new file mode 100644 index 0000000..8d68208 --- /dev/null +++ b/tests/test_missing_all.py @@ -0,0 +1,53 @@ +import sys + +import pytest + +import hepunits + + +def filter_module(item: str) -> bool: + if item == "__version__": + return True + if item.startswith("_"): + return False + if item in {"List"}: + return False + return True + + +@pytest.mark.skipif( + sys.version_info < (3, 7), reason="Python 3.7+ added __dir__ support" +) +@pytest.mark.parametrize( + "module", + ( + hepunits, + hepunits.units, + hepunits.constants, + hepunits.constants.constants, + hepunits.units.prefixes, + hepunits.units.units, + ), +) +def test_missing_all(module): + assert set(dir(module)) == set(module.__all__) + + full_module = {it for it in module.__dict__ if filter_module(it)} + assert full_module == set(module.__all__) + + +def test_exported(): + expr = set(hepunits.units.__all__) - (set(hepunits.__all__) | {"prefixes"}) + assert not expr + + expr = set(hepunits.constants.__all__) - set(hepunits.__all__) + assert not expr + + expr = set(hepunits.constants.constants.__all__) - set(hepunits.constants.__all__) + assert not expr + + expr = set(hepunits.units.prefixes.__all__) - set(hepunits.units.__all__) + assert not expr + + expr = set(hepunits.units.units.__all__) - set(hepunits.units.__all__) + assert not expr diff --git a/tests/units/test_prefixes.py b/tests/units/test_prefixes.py index e2bdb6b..a5e253f 100644 --- a/tests/units/test_prefixes.py +++ b/tests/units/test_prefixes.py @@ -1,15 +1,14 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # Licensed under a 3-clause BSD style license, see LICENSE. """ Tests for the hepunits.units.prefixes module. """ -from pytest import approx - from math import log -from hepunits import mega, micro, yotta, yocto, kibi, tebi +from pytest import approx + +from hepunits import kibi, mega, micro, tebi, yocto, yotta def test_prefixes_e6(): diff --git a/tests/units/test_units.py b/tests/units/test_units.py index cb0cb85..e4b6b82 100644 --- a/tests/units/test_units.py +++ b/tests/units/test_units.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # Licensed under a 3-clause BSD style license, see LICENSE. """ Tests for the hepunits.units.units module.