Skip to content

Commit

Permalink
Allowed __repr__ of a poorly constructed Unit. (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
pelson authored and bjlittle committed Jan 22, 2019
1 parent 1836f32 commit 18b72bc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cf_units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ def is_unknown(self):
False
"""
return self.category == _CATEGORY_UNKNOWN
return self.category == _CATEGORY_UNKNOWN or self.ut_unit is None

def is_no_unit(self):
"""
Expand Down
8 changes: 7 additions & 1 deletion cf_units/tests/test_unit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) British Crown Copyright 2010 - 2018, Met Office
# (C) British Crown Copyright 2010 - 2019, Met Office
#
# This file is part of cf-units.
#
Expand Down Expand Up @@ -747,6 +747,12 @@ def test_known_unit(self):
u = Unit('meters')
self.assertFalse(u.is_unknown())

def test_no_ut_pointer(self):
# Test that a unit that was poorly constructed has a
# degree of tolerance by making it unknown.
# https://github.com/SciTools/cf-units/issues/133 refers.
self.assertTrue(Unit.__new__(Unit).is_unknown())


class Test_is_no_unit(unittest.TestCase):

Expand Down
11 changes: 10 additions & 1 deletion cf_units/tests/unit/unit/test_Unit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) British Crown Copyright 2015 - 2018, Met Office
# (C) British Crown Copyright 2015 - 2019, Met Office
#
# This file is part of cf-units.
#
Expand Down Expand Up @@ -249,5 +249,14 @@ def test_not_time_unit(self):
self.assertFalse(result)


class Test_format(unittest.TestCase):
def test_invalid_ut_unit(self):
# https://github.com/SciTools/cf-units/issues/133 flagged up that
# format should be a little more tolerant of a Unit that has not been
# constructed correctly when using pytest.
unit = Unit.__new__(Unit)
self.assertEqual(unit.format(), 'unknown')


if __name__ == '__main__':
unittest.main()

0 comments on commit 18b72bc

Please sign in to comment.