Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed __repr__ of a poorly constructed Unit. #134

Merged
merged 1 commit into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pelson Could self.ut_unit also be _ud.NULL_UNIT? If so, does this need to be included here in this check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only way it can be NULL_UNIT is if the constructor succeeded and it is one of _NO_UNIT or _UNKNOWN_UNIT.

We shouldn't consider "no unit" unknown, so the only case that it can be NULL_UNIT is if self.category == _CATEGORY_UNKNOWN.

In summary, I don't think so.


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()