move-imports
A script to move imports to the top of the module. Suppose a module with inline import like this
import requests
from math import sin
import datetime as dt
BASE_SCENARIO = """
##########
# Accounts
"""
def foo():
from math import (
sin, cos as cos_
)
return sin(1), cos_(0)
def bar():
from math import tan
return tan(1)
Executing
$ move-imports path/to_refactor.py --isort --rewrite
will produce
import datetime as dt
from math import cos as cos_, sin, tan
import requests
BASE_SCENARIO = """
##########
# Accounts
"""
def foo():
return sin(1), cos_(0)
def bar():
return tan(1)
Install
$ pip --user install move-imports
or the development version, directly from the git repo
$ pip --user install git+https://github.com/mgaitan/move-imports
Skip cases
To keep an inline import you could add a comment in the same line or above the stament with "avoid circular import" or "noqa".
def foo():
# avoid circular import
import baz
def foo():
import baz # noqa
Incremental refactoring
Sometimes inline imports statements are there for a reason. Circular imports, optional dependendencies, etc.
To manage this, it's useful to go step by step, checking a changed module is ok before to move to the next one.
With a combination of --start-from-last and --limit-to LIMIT_TO
arguments you could incrementally refactor a whole package.
For instance, calling repeteadly the following command
$ move-imports --isort --rewrite --start-from-last --limit-to=1 tests/billing/**/*.py
will recursively traverse tests/billing/ refactoring one module at a time. Thus, the worflow would be:
- run,
- test,
- optionally revert and skip problematic imports or modify manually
- repeat
Running tests
Clone the repo and install pytest
$ pytest
Command line interface
$ move-imports --help
usage: move-imports [-h] [--start-from-last] [--limit-to LIMIT_TO] [--debug]
[--show-only] [--safe] [--isort]
[paths [paths ...]]
positional arguments:
paths Path/s to refactor. Glob supported enclosed in quotes
optional arguments:
-h, --help show this help message and exit
--start-from-last Incremental refactor
--limit-to LIMIT_TO Stop processing after N files. Use with --start-from-
last
--debug Make verbose output
--show-only write the result to stdin
--safe Only move stdlib or thirdparty imports
--isort Apply isort
Acknowledgement
Thanks to Shiphero for give me the time to do this.
Metadata
Release files for move-imports 0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| move_imports-0.4-py3-none-any.whl | Python 3 | none | any | Details |
Release files / move_imports-0.4-py3-none-any.whl
| Download URL | move_imports-0.4-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8a301648f6cd1dacc72379f029953ea679de3cc2256f40125d90413491d2e8f3
|
|
BLAKE2b-256 checksum How to use checksums |
87779738b69f30fc3e874ac1655f91a7a0816f343181a868615c27d7bd9956b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.20.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.8
|
Security for move-imports 0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.