Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Validates python type of ObjectGraph.provide() arg.
Browse files Browse the repository at this point in the history
  • Loading branch information
KurtSteinkraus committed Jul 8, 2013
1 parent e98a6fa commit 4575132
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
5 changes: 2 additions & 3 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
TODO:
- validate python types of all input
- allow binding specs to declare required bindings
- standard tests for scopes (reentrant? thread-safe?), annotations (eq?
hash?), etc.
- describe features specifically omitted, e.g.,
- field injection
- circular injection
- scope annotations on classes
- figure out whether/how Pinject works with inheritance

Maybe TODO:
- standard tests for scopes (reentrant? thread-safe?), annotations (eq?
hash?), etc.
- change default scope back to prototype?
- eager singletons
- allow field injection
Expand Down
7 changes: 7 additions & 0 deletions pinject/object_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ def new_object_graph(
use_short_stack_traces)


def _verify_type(elt, required_type, arg_name):
if type(elt) != required_type:
raise errors.WrongArgTypeError(
arg_name, required_type.__name__, type(elt).__name__)


def _verify_types(seq, required_type, arg_name):
if not isinstance(seq, collections.Sequence):
raise errors.WrongArgTypeError(
Expand Down Expand Up @@ -199,6 +205,7 @@ def provide(self, cls):
Raises:
Error: an instance of cls is not providable
"""
_verify_type(cls, types.TypeType, 'cls')
if not self._is_injectable_fn(cls):
provide_loc = locations.get_back_frame_loc()
raise errors.NonExplicitlyBoundClassError(provide_loc, cls)
Expand Down
17 changes: 17 additions & 0 deletions pinject/object_graph_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ def test_raises_exception_if_is_scope_usable_from_scope_is_wrong_type(self):
is_scope_usable_from_scope=42)


class VerifyTypeTest(unittest.TestCase):

def test_verifies_correct_type_ok(self):
object_graph._verify_type(types, types.ModuleType, 'unused')

def test_raises_exception_if_incorrect_type(self):
self.assertRaises(errors.WrongArgTypeError, object_graph._verify_type,
'not-a-module', types.ModuleType, 'an-arg-name')


class VerifyTypesTest(unittest.TestCase):

def test_verifies_empty_sequence_ok(self):
Expand Down Expand Up @@ -540,6 +550,13 @@ def provide_foo(self):
self.assertRaises(errors.InjectingNoneDisallowedError,
obj_graph.provide, SomeClass)

def test_raises_exception_if_trying_to_provide_nonclass(self):
class SomeClass(object):
pass
obj_graph = object_graph.new_object_graph(
modules=None, classes=[SomeClass])
self.assertRaises(errors.WrongArgTypeError, obj_graph.provide, 42)


class ObjectGraphWrapTest(unittest.TestCase):

Expand Down

0 comments on commit 4575132

Please sign in to comment.