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

Suggestion: Mention contextlib.ExitStack in "Use backslashes for with statements"? #2560

Open
joshua-cannon-techlabs opened this issue Oct 22, 2021 · 1 comment
Labels
good first issue T: documentation

Comments

@joshua-cannon-techlabs
Copy link

@joshua-cannon-techlabs joshua-cannon-techlabs commented Oct 22, 2021

Is this related to a problem? Please describe.

On Black's future style doc it talks about black handling a with with multiple context managers in a few ways (Python-version-dependent).

Describe the solution you'd like

Black should suggest to the user (in documentation only) that if they don't like Black's current or future behavior, that constructing a contextlib.ExitStack can help the issue while maintaining 95%-ish of the code readability and has minimal overhead.

As an example (feel free to steal), and using the existing code on that page

with contextlib.ExitStack() as exit_stack:
    cm1 = exit_stack.enter_context(make_context_manager(1))
    cm2 = exit_stack.enter_context(make_context_manager(2))
    cm3 = exit_stack.enter_context(make_context_manager(3))
    cm4 = exit_stack.enter_context(make_context_manager(4))
    ...

Describe alternatives you've considered

N/A

Additional context

❤️ black

@joshua-cannon-techlabs joshua-cannon-techlabs added the T: documentation label Oct 22, 2021
@joshua-cannon-techlabs
Copy link
Author

@joshua-cannon-techlabs joshua-cannon-techlabs commented Oct 22, 2021

Related: #2118 #1948

@ichard26 ichard26 added the good first issue label Dec 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue T: documentation
Projects
None yet
Development

No branches or pull requests

2 participants