-
Notifications
You must be signed in to change notification settings - Fork 85
Description
#96 added special handling for _GeneratorContextManagers to inject.attr.
I think the logic is faulty and/or is not very useful since it "exits" the context manager before even returning it.
The example that the author provided with version 5.3.0 outputs:
enter
exit
20
and thus demonstrates that the context manager returned by inject.attr is already exited before usage.
Looking at the implementation, this is not a surprise:
python-inject/src/inject/__init__.py
Lines 288 to 290 in 6ff646b
| elif isinstance(inst, contextlib._GeneratorContextManager): | |
| with contextlib.ExitStack() as sync_stack: | |
| inst = sync_stack.enter_context(inst) |
A new ExitStack is created, used with with which implicitly class __enter__/__exit__ before returning the new instance. Potentially the author thought that this is about exiting the Python program because of the potentially misleading name?
Anyways, that would also not be a good option since that would hold onto the instance and prevent its garbage collection.
After a quick brainstorming, I could think of the following options:
- 🥇 remove the special logic for
AbstractContextManager-- the caller ofinject.attrmust call enter/exit the context appropriately. - Instead of
contextlib.ExitStack()useinject.instance(ExitStack). That way one can manage a "global"ExitStackand exit at the appropriate time. I don't like that too much, because again, one would have to close and remove the ExitStack to get rid of the instance.
What do you think? In the meantime, I'll work around this in wrapping the context managers so that they are not recognized as context managers anymore.