Skip to content

Commit

Permalink
Enable old Boltzmann key with DeprecationWarning (#4230)
Browse files Browse the repository at this point in the history
* Enable old Boltzmann key with warning
  • Loading branch information
xhgchen authored Aug 10, 2023
1 parent d33cc3a commit bd3c14c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Changes
(PR #4174, Issue #3819)

Deprecations
* Incorrectly spelt Boltzmann unit entry will be removed in version 2.8.0
(PR #4230, Issue #4229)
* The hole2 module is deprecated in favour of the the MDAKit:
https://github.com/MDAnalysis/hole2-mdakit (Issue #4179, PR #4200)
* MDAnalysis no longer officially supports 32 bit installations (they are
Expand Down
21 changes: 19 additions & 2 deletions package/MDAnalysis/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,23 @@
"""

import warnings


# Remove in 2.8.0
class DeprecatedKeyAccessDict(dict):
deprecated_kB = 'Boltzman_constant'

def __getitem__(self, key):
if key == self.deprecated_kB:
wmsg = ("Please use 'Boltzmann_constant' henceforth. The key "
"'Boltzman_constant' was a typo and will be removed "
"in MDAnalysis 2.8.0.")
warnings.warn(wmsg, DeprecationWarning)
key = 'Boltzmann_constant'
return super().__getitem__(key)


#
# NOTE: Whenever a constant is added to the constants dict, you also
# MUST add an appropriate entry to
Expand All @@ -189,13 +206,13 @@
#: http://physics.nist.gov/Pubs/SP811/appenB8.html#C
#:
#: .. versionadded:: 0.9.0
constants = {
constants = DeprecatedKeyAccessDict({
'N_Avogadro': 6.02214129e+23, # mol**-1
'elementary_charge': 1.602176565e-19, # As
'calorie': 4.184, # J
'Boltzmann_constant': 8.314462159e-3, # KJ (mol K)**-1
'electric_constant': 5.526350e-3, # As (Angstroms Volts)**-1
}
})

#: The basic unit of *length* in MDAnalysis is the Angstrom.
#: Conversion factors between the base unit and other lengthUnits *x* are stored.
Expand Down
8 changes: 8 additions & 0 deletions testsuite/MDAnalysisTests/utils/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,21 @@ class TestConstants(object):
('elementary_charge', 1.602176565e-19), # As
('calorie', 4.184), # J
('Boltzmann_constant', 8.314462159e-3), # KJ (mol K)**-1
('Boltzman_constant', 8.314462159e-3), # remove in 2.8.0
('electric_constant', 5.526350e-3), # As (Angstroms Volts)**-1
)

@pytest.mark.parametrize('name, value', constants_reference)
def test_constant(self, name, value):
assert_almost_equal(units.constants[name], value)

def test_boltzmann_typo_deprecation(self):
wmsg = ("Please use 'Boltzmann_constant' henceforth. The key "
"'Boltzman_constant' was a typo and will be removed "
"in MDAnalysis 2.8.0.")
with pytest.warns(DeprecationWarning, match=wmsg):
units.constants['Boltzman_constant']


class TestConversion(object):
@staticmethod
Expand Down

0 comments on commit bd3c14c

Please sign in to comment.