Closed o-fedorov closed 5 years ago
I'm about these lines of code:
class Injector(object):
...
def get_instance(self, cls):
...
if not callable(cls):
raise InjectorException(
'Cannot create a runtime binding, the key is not callable, key=%s' % cls)
instance = cls()
self._bindings[cls] = lambda: instance
logger.debug('Created a runtime binding for key=%s, instance=%s', cls, instance)
return instance
If a user forgets to bind a class to an instance, in the best case she gets an exception like
TypeError
for abstract classes, typing variables, callables that require at least one argument and so on. Such messages are usually not helpful and sometimes obscuring.In the worst case the singleton is instantiated with default parameters while it was not intended. It may lead to hard-to-debug issues.
I've recently encountered one of such issues. Also, "explicit is better than implicit".
At minimum, I would add an argument to the
configure
functions to disable implicit classes instantiation.And for the future I would suggest to gradually make implicit instantiation disabled by default, or even deprecate and then remove this functionality.