Skip to content

Added context manager condition to attr function(Fix #94) #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 24, 2024

Conversation

fhdufhdu
Copy link
Contributor

@fhdufhdu fhdufhdu commented Mar 22, 2024

class SyncCM:
    def __init__(self, i):
        self.i = i
    def __enter__(self):
        print('enter')
        return self
    def __exit__(self, *args):
        print('exit')
        return self
    def test(self, j: int):
        return j

@contextmanager
def get_cm() -> Iterator[object]:
    with SyncCM(10) as cm:
        yield cm

class Repo:
    _cm:SyncCM = inject.attr(SyncCM)
    
    def cm_test(self):
        result = self._cm.test(20)
        return result
  

def configure(binder:inject.Binder):
    binder.bind_to_provider(SyncCM, get_cm)

inject.configure(configure)

r:Repo = inject.instance(Repo)
result = r.cm_test()
print(result)

Resolved the error occurring(#94) when using @contextmanager in the parameter function of .bind_to_provider function

@contextmanager: Resolved
@asynccontextmanager: Replaced with an Exception since a way to use Descriptors asynchronously could not be found.

@ivankorobkov ivankorobkov merged commit e6bc868 into ivankorobkov:master Mar 24, 2024
@ivankorobkov
Copy link
Owner

Thanks.

I have just released https://pypi.org/project/inject/5.2.1/

@fhdufhdu
Copy link
Contributor Author

@ivankorobkov
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants