Skip to content

adrienverge/context_unnester

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

context_unnester

contextlib.nested is deprecated since Python 2.7 and incompatible with Python 3. Still, it is used in a wide number of Python projects.

context_unnester helps removing its usage, by automatically fixing the code.

Features

  • Detects contextlib.nested occurences and replaces them by appropriate with syntax.
  • Removes imports of contextlib when they are not needed anymore.
  • Tries to make the replaced code PEP8-compatible.

Usage

usage: context_unnester.py [-h] PATH [PATH ...]

positional arguments:
  PATH        Path to source that needs to be fixed. If PATH is a dir, apply
              to all Python files in it.

optional arguments:
  -h, --help  show this help message and exit

Examples of code rewriting

# before:
import contextlib
with contextlib.nested(A(), B(), C()):
    do_stuff()
# after:
with A(), B(), C():
    do_stuff()
# before:
import contextlib
with contextlib.nested(A(), B(), C()) as (a, _, c):
    do_stuff(1, a, c)
# after:
with A() as a, B(), C() as c:
    do_stuff(1, a, c)
# before:
import contextlib
with contextlib.nested(A(), B(), C()) as abc:
    do_stuff(42, abc)
# after:
with A() as a, B() as b, C() as c:
    abc = (a, b, c)
    do_stuff(42, abc)

About

Fixes Python source code that use contextlib.nested.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages