Skip to content
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

bpo-40816 Add AsyncContextDecorator class #20516

Merged
merged 12 commits into from
Nov 5, 2020

Conversation

heckad
Copy link
Contributor

@heckad heckad commented May 29, 2020

Copy link
Contributor

@eamanu eamanu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, we need test here, docs.. I thing this need more discussion?

@heckad
Copy link
Contributor Author

heckad commented May 30, 2020

Do you know you can write something like

@contextmanager
def safe_space(is_on=True):
    token = IS_SAFE_SPACE_VALUE.set(is_on)
    try:
        yield
    finally:
        IS_SAFE_SPACE_VALUE.reset(token)

And use it as

class MyClass:
    @safe_space()
    def safe_method(self):
        pass

but you can't write

@asynccontextmanager
async def async_safe_space(is_on=True):
    token = IS_SAFE_SPACE_VALUE.set(is_on)
    try:
        yield
    finally:
        IS_SAFE_SPACE_VALUE.reset(token)

because you get the error TypeError: '_AsyncGeneratorContextManager' object is not callable. This pull request makes '_AsyncGeneratorContextManager' callable. This is an implementation of lost functionality.

@heckad
Copy link
Contributor Author

heckad commented Jun 2, 2020

@1st1?

@njsmith
Copy link
Contributor

njsmith commented Jul 18, 2020

@belm0 asked me to look at this, but tbh I'm not real comfortable with adding new API surface to contextlib without input from the module owners, which I think is @ncoghlan and @1st1.

The idea seems straightforward enough. @eamanu is right though that it definitely needs docs and tests.

@heckad
Copy link
Contributor Author

heckad commented Jul 18, 2020

@njsmith, I will add tests if this pull request is interesting. I was just waiting for a response from @1st1.

@belm0
Copy link
Contributor

belm0 commented Jul 20, 2020

@heckad since core review attention is a scarce resource, I suggest having the PR ready to approve. Since the tests and docs can mostly be copied from ContextDecorator, not too much work?

This functionality is needed-- let's assume the maintainers will agree.

@heckad
Copy link
Contributor Author

heckad commented Jul 20, 2020

@belm0 How to improve the documentation?

@belm0
Copy link
Contributor

belm0 commented Jul 20, 2020

@belm0 How to improve the documentation?

See context manager docs:

contextmanager() uses ContextDecorator so the context managers it creates can be used as decorators as well as in with statements. When used as a decorator, a new generator instance is implicitly created on each function call (this allows the otherwise “one-shot” context managers created by contextmanager() to meet the requirement that context managers support multiple invocations in order to be used as decorators).

(it would also need "Changed in version X.X" annotation)

Also see the ContextDecorator docs. Perhaps it's enough to say "Similar to ContextDecorator(), but works with async context managers" and give a short example.

https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager
https://docs.python.org/3/library/contextlib.html#contextlib.ContextDecorator

Lib/contextlib.py Outdated Show resolved Hide resolved
Copy link
Member

@1st1 1st1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait! The code/tests look great! But we also need to update the docs before we merge this.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@heckad
Copy link
Contributor Author

heckad commented Jul 23, 2020

Need to add a version. Which version of python will be added this feature?

@heckad heckad force-pushed the fix-issue-40816 branch from 3d7e06a to a1735c5 Compare July 23, 2020 19:44
@1st1
Copy link
Member

1st1 commented Jul 23, 2020

Need to add a version. Which version of python will be added this feature?

3.10.

@heckad
Copy link
Contributor Author

heckad commented Jul 23, 2020

Need to add a version. Which version of python will be added this feature?

3.10.

What about 3.9?

@1st1
Copy link
Member

1st1 commented Jul 23, 2020

We missed that train, unfortunately. 3.9 is frozen for new features.

Doc/library/contextlib.rst Outdated Show resolved Hide resolved
@heckad
Copy link
Contributor Author

heckad commented Oct 31, 2020

@1st1, all changes were applied.

@heckad heckad requested a review from 1st1 November 3, 2020 20:29
@asvetlov
Copy link
Contributor

asvetlov commented Nov 5, 2020

Thanks @heckad

@heckad heckad deleted the fix-issue-40816 branch November 5, 2020 09:00
shihai1991 added a commit to shihai1991/cpython that referenced this pull request Nov 6, 2020
* origin/master:
  bpo-42179: Doc/tutorial: Remove mention of __cause__ (pythonGH-23162)
  bpo-26389: Allow passing an exception object in the traceback module (pythonGH-22610)
  bpo-42260: PyConfig_Read() only parses argv once (pythonGH-23168)
  bpo-42260: Add _PyConfig_FromDict() (pythonGH-23167)
  bpo-41877 Check for asert, aseert, assrt in mocks (pythonGH-23165)
  [docs] fix wrongly named AsyncContextDecorator (pythonGH-23164)
  bpo-42262: Add Py_NewRef() and Py_XNewRef() (pythonGH-23152)
  bpo-42266: Handle monkey-patching descriptors in LOAD_ATTR cache (pythonGH-23157)
  bpo-40816 Add AsyncContextDecorator class (pythonGH-20516)
shihai1991 added a commit to shihai1991/cpython that referenced this pull request Nov 7, 2020
…lots1

* origin/master: (80 commits)
  bpo-42282: Fold constants inside named expressions (pythonGH-23190)
  bpo-41028: Doc: Move switchers to docsbuild-scripts. (pythonGH-20969)
  bpo-42133: update parts of the stdlib to fall back to `__spec__.loader` when `__loader__` is missing (python#22929)
  Remove outdated reference to pywin32 from platform module (pythonGH-22005)
  bpo-41832: PyType_FromModuleAndSpec() now accepts NULL tp_doc (pythonGH-23123)
  Minor grammar edits for the descriptor howto guide (GH-python#23175)
  bpo-42179: Doc/tutorial: Remove mention of __cause__ (pythonGH-23162)
  bpo-26389: Allow passing an exception object in the traceback module (pythonGH-22610)
  bpo-42260: PyConfig_Read() only parses argv once (pythonGH-23168)
  bpo-42260: Add _PyConfig_FromDict() (pythonGH-23167)
  bpo-41877 Check for asert, aseert, assrt in mocks (pythonGH-23165)
  [docs] fix wrongly named AsyncContextDecorator (pythonGH-23164)
  bpo-42262: Add Py_NewRef() and Py_XNewRef() (pythonGH-23152)
  bpo-42266: Handle monkey-patching descriptors in LOAD_ATTR cache (pythonGH-23157)
  bpo-40816 Add AsyncContextDecorator class (pythonGH-20516)
  bpo-42260: Add _PyInterpreterState_SetConfig() (pythonGH-23158)
  Disable peg generator tests when building with PGO (pythonGH-23141)
  bpo-1635741: _sqlite3 uses PyModule_AddObjectRef() (pythonGH-23148)
  bpo-1635741: Fix PyInit_pyexpat() error handling (pythonGH-22489)
  bpo-42260: Main init modify sys.flags in-place (pythonGH-23150)
  ...
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 13, 2021
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.

8 participants