Skip to content

Commit

Permalink
change TypedDatatypeInstanceConstructionError to subclass TypeError
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed May 3, 2018
1 parent 9ab7a30 commit 4f9ba6c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/python/pants/util/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def __init__(self, type_name, msg, *args, **kwargs):
full_msg, *args, **kwargs)


class TypedDatatypeInstanceConstructionError(Exception):
class TypedDatatypeInstanceConstructionError(TypeError):

def __init__(self, type_name, msg, *args, **kwargs):
full_msg = "error: in constructor of type {}: {}".format(type_name, msg)
Expand Down
21 changes: 21 additions & 0 deletions tests/python/pants_test/option/test_global_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# coding=utf-8
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

from pants.option.global_options import GlobMatchErrorBehavior
from pants_test.base_test import BaseTest


class GlobalOptionsTest(BaseTest):

def test_exception_glob_match_constructor(self):
# NB: 'allow' is not a valid value for GlobMatchErrorBehavior.
with self.assertRaises(TypeError) as cm:
GlobMatchErrorBehavior(str('allow'))
expected_msg = (
"""error: in constructor of type GlobMatchErrorBehavior: type check error:
Value 'allow' for failure_behavior must be one of: [u'ignore', u'warn', u'error'].""")
self.assertEqual(str(cm.exception), expected_msg)
15 changes: 7 additions & 8 deletions tests/python/pants_test/util/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import pickle
from abc import abstractmethod

from pants.util.objects import (Exactly, SubclassesOf, SuperclassesOf, TypeCheckError,
TypedDatatypeInstanceConstructionError, datatype)
from pants.util.objects import Exactly, SubclassesOf, SuperclassesOf, datatype
from pants_test.base_test import BaseTest


Expand Down Expand Up @@ -415,7 +414,7 @@ def test_instance_construction_errors(self):
expected_msg = "__new__() takes exactly 2 arguments (3 given)"
self.assertEqual(str(cm.exception), str(expected_msg))

with self.assertRaises(TypedDatatypeInstanceConstructionError) as cm:
with self.assertRaises(TypeError) as cm:
CamelCaseWrapper(nonneg_int=3)
expected_msg = (
"""error: in constructor of type CamelCaseWrapper: type check error:
Expand All @@ -430,38 +429,38 @@ def test_instance_construction_errors(self):

def test_type_check_errors(self):
# single type checking failure
with self.assertRaises(TypeCheckError) as cm:
with self.assertRaises(TypeError) as cm:
SomeTypedDatatype([])
expected_msg = (
"""error: in constructor of type SomeTypedDatatype: type check error:
field 'val' was invalid: value [] (with type 'list') must satisfy this type constraint: Exactly(int).""")
self.assertEqual(str(cm.exception), str(expected_msg))

# type checking failure with multiple arguments (one is correct)
with self.assertRaises(TypeCheckError) as cm:
with self.assertRaises(TypeError) as cm:
AnotherTypedDatatype(str('correct'), str('should be list'))
expected_msg = (
"""error: in constructor of type AnotherTypedDatatype: type check error:
field 'elements' was invalid: value 'should be list' (with type 'str') must satisfy this type constraint: Exactly(list).""")
self.assertEqual(str(cm.exception), str(expected_msg))

# type checking failure on both arguments
with self.assertRaises(TypeCheckError) as cm:
with self.assertRaises(TypeError) as cm:
AnotherTypedDatatype(3, str('should be list'))
expected_msg = (
"""error: in constructor of type AnotherTypedDatatype: type check error:
field 'string' was invalid: value 3 (with type 'int') must satisfy this type constraint: Exactly(str).
field 'elements' was invalid: value 'should be list' (with type 'str') must satisfy this type constraint: Exactly(list).""")
self.assertEqual(str(cm.exception), str(expected_msg))

with self.assertRaises(TypeCheckError) as cm:
with self.assertRaises(TypeError) as cm:
NonNegativeInt(str('asdf'))
expected_msg = (
"""error: in constructor of type NonNegativeInt: type check error:
field 'an_int' was invalid: value 'asdf' (with type 'str') must satisfy this type constraint: Exactly(int).""")
self.assertEqual(str(cm.exception), str(expected_msg))

with self.assertRaises(TypeCheckError) as cm:
with self.assertRaises(TypeError) as cm:
NonNegativeInt(-3)
expected_msg = (
"""error: in constructor of type NonNegativeInt: type check error:
Expand Down

0 comments on commit 4f9ba6c

Please sign in to comment.