It seems to be doing fine on its own.
As a bonus this restores running doctests from the injector source code
which I broke when I made it a package (code in injector/__init__.py)
instead of a simple module. *But* we need to start skipping running
doctests for get_bindings on Python versions that don't support
Annotated (or don't support it well enough).
The example of an error this regression caused:
1302 A BoundKey provides a key to a type with pre-injected arguments.
1303
1304 >>> class A:
1305 ... def __init__(self, a, b):
1306 ... self.a = a
1307 ... self.b = b
1308 >>> InjectedA = BoundKey(A, a=InstanceProvider(1), b=InstanceProvider(2))
1309 >>> injector = Injector()
1310 >>> a = injector.get(InjectedA)
UNEXPECTED EXCEPTION: UnknownProvider("couldn't determine provider for InstanceProvider(1) to None")
Traceback (most recent call last):
File "/Users/user/projects/injector/injector/__init__.py", line 583, in get_binding
return self._get_binding(interface, only_this_binder=is_scope)
File "/Users/user/projects/injector/injector/__init__.py", line 578, in _get_binding
raise KeyError
KeyError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/doctest.py", line 1329, in __run
compileflags, 1), test.globs)
File "<doctest injector.BoundKey[3]>", line 1, in <module>
File "/Users/user/projects/injector/injector/__init__.py", line 864, in get
result = scope_instance.get(interface, binding.provider).get(self)
File "/Users/user/projects/injector/injector/__init__.py", line 289, in get
return injector.call_with_injection(self._callable)
File "/Users/user/projects/injector/injector/__init__.py", line 918, in call_with_injection
owner_key=self_.__class__ if self_ is not None else callable.__module__,
File "/Users/user/projects/injector/injector/__init__.py", line 80, in wrapper
return function(*args, **kwargs)
File "/Users/user/projects/injector/injector/__init__.py", line 959, in args_to_inject
instance = self.get(interface)
File "/Users/user/projects/injector/injector/__init__.py", line 853, in get
binding, binder = self.binder.get_binding(interface)
File "/Users/user/projects/injector/injector/__init__.py", line 592, in get_binding
binding = self.create_binding(interface)
File "/Users/user/projects/injector/injector/__init__.py", line 511, in create_binding
provider = self.provider_for(interface, to)
File "/Users/user/projects/injector/injector/__init__.py", line 569, in provider_for
raise UnknownProvider('couldn\'t determine provider for %r to %r' % (interface, to))
injector.UnknownProvider: couldn't determine provider for InstanceProvider(1) to None
It's been reported in GH-125. We haven't seen this because until
recently doctests weren't running.
Closes GH-125.
Since I introduced the function in eb5344cd8a
the doctest inside had invalid outputs. It hasn't been caught as due to
an unrelated bug we haven't been actually executing doctests (I'm
working on fixing it).
Firstly, integers can be cached and reused by the implementation so the
result of one injector.get(key) call may return the same int as the
other.
Secondly, NewType was unnecessarily introduced here so let's replace the
NewType+int tandem with a single class.
The issue helpfully described and solution suggested by eugenhu.
UnsatisfiedRequirement's __str__ depends on there being two arguments
(the first one optionally None).
Closes GH-129.
The errors in question:
% mypy injector
injector/__init__.py:34: error: Incompatible types in assignment (expression has type "None", variable has type "_SpecialForm")
injector/__init__.py:37: error: All conditional function variants must have identical signatures
injector/__init__.py:1067: error: Argument 1 to "pop" of "MutableMapping" has incompatible type "Optional[str]"; expected "str"
injector/__init__.py:1068: error: Argument 1 to "pop" of "MutableMapping" has incompatible type "Optional[str]"; expected "str"
Coincidentally this commit fixes an issue [1] discovered by eugenhu.
Although the solution to the issue is different than eugenhu suggests
the analysis of the underlying cause is helpful and appreciated.
[1] "Cannot inject dependency which is a subclass of a subscripted type", GH-128
Closes GH-128