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

Add ModificationContext ABC to contextlib. #128574

Closed
jb2170 opened this issue Jan 7, 2025 · 2 comments
Closed

Add ModificationContext ABC to contextlib. #128574

jb2170 opened this issue Jan 7, 2025 · 2 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@jb2170
Copy link
Contributor

jb2170 commented Jan 7, 2025

Feature or enhancement

Proposal:

We add an ABC ModificationContext to contextlib:

ModificationContext specializes the functionality of AbstractContextManager, for context managers designed to change the value of a state or attribute on entry, and revert it on exit. We have noticed that this style is used by many existing and proposed context managers.

The following classes already present in contextlib are to be refactored to inherit from ModificationContext (with no compatibility issues)

  • contextlib._RedirectStream, parent of redirect_stdout and redirect_stderr
  • contextlib.chdir

After this addition, the following issues are rebased, so that their additions inherit from ModificationContext:

  • gh-128432: Add shutil.umask
  • TBC: Add shutil.seteuid and shutil.setegid
  • gh-128432: Move contextlib.chdir to shutil.chdir

This addition makes those first two PRs trivial, and contextlib.chdir is refactored whether it is moved to shutil or not.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

Thread started by @ncoghlan

https://discuss.python.org/t/adding-a-contextlib-modificationcontext-base-class/76322/2

@jb2170 jb2170 added the type-feature A feature request or enhancement label Jan 7, 2025
@picnixz picnixz added the stdlib Python modules in the Lib dir label Jan 7, 2025
@ncoghlan
Copy link
Contributor

ncoghlan commented Jan 7, 2025

It's worth noting that adding this weakens the case for adding the additional more specific subclasses in shutil (which in turn weakens the case for moving chdir), since it makes it easier for folks with a use case for that context manager to write it themselves. However, I'll discuss that more on gh-128432

@jb2170
Copy link
Contributor Author

jb2170 commented Jan 8, 2025

Suggested cancelling in the discussion due to the unnecessary / confusing number of ABCs it introduces.

The code is still available on my fork here if desired.

I'll close this issue for now 🙂

@jb2170 jb2170 closed this as completed Jan 8, 2025
@erlend-aasland erlend-aasland closed this as not planned Won't fix, can't repro, duplicate, stale Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants