ensure that both __exit__ and __enter__ are defined

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: python-best-practices/ctx-manager-enter-exit-defined

Language: Python

Severity: Error

Category: Best Practices

Description

Methods __enter__ and __exit__ must be declared together. If one method is missing, we should make sure both are defined.

Learn More

Non-Compliant Code Examples

class Ctx:
    def __exit__(self, *exc):  # the method __enter__ should be defined.
        pass
class Ctx:
    def __enter__(self):  # the method __exit__ should also be defined.
        pass

Compliant Code Examples

class Ctx:
    def __enter__(self):
        pass
    def __exit__(self, *exc):
        pass
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis