currently lots of code manages contexts incorrectly by doing:

```
@dataclasses.dataclass
class WrapCmgr:
    _cmgr: ContextManager[T]

    def __enter__(self) -> Wrapped[T]:
        return wrap(_cmgr.__enter__())

    def __exit__(self, \, t: Type[BaseException] | None, v: BaseException, tb: 
types.TracebackType) -> bool:
        return _cmgr.__exit__(t, v, tb)
```


since https://bugs.python.org/issue44471 using `with WrapCmgr(cmgr()):` 
incorrectly raises an AttributeError rather than a TypeError

and https://www.python.org/dev/peps/pep-0343/ still includes this bug, raising 
an AttributeError

```
mgr = (EXPR)
exit = type(mgr).__exit__  # Not calling it yet
value = type(mgr).__enter__(mgr)
```

contextlib.ExitStack also had this bug https://bugs.python.org/issue12022

given that it's hard to implement this correctly I think there should be a 
builtins.enter and builtins.aenter for use like this:


```
@dataclasses.dataclass
class WrapCmgr:
    _cmgr: ContextManager[T]

    def __enter__(self) -> Wrapped[T]:
        exit, value = enter(self._cmgr)
        self.__exit = exit
        return wrap(value)

    def __exit__(self, \, t: Type[BaseException] | None, v: BaseException, tb: 
types.TracebackType) -> bool:
        return self.__exit(t, v, tb)
```
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/GV5ZCOT3UHLDEMPOCWWDTSDARIB7SYYN/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to