Skip to main content

Expand wildcard imports in Python code

Project description

Dewildcard

A not-very-sophisticated script to replace Python wildcard import statements.

Background

In Python, wildcard import statements, such as:

from foo import *
from bar import *

can be very convenient, but are now usually considered bad practice. If, later in your code, you encounter a symbol you don't recognise, how do you know whether it came from foo or bar? And if you install an updated version of bar, it may define a new name that clashes with one in foo that you were using in your code.

It's much better, therefore, to say:

from foo import item1, item2
from bar import item3, item4, item5

even though it's more verbose.

Tools like pylint of pyflakes can also let you know if you can safely delete, say, 'item5', because it isn't used in your code. If you have a good text editor, it may have a plugin which can highlight this fact.

This little script reads some python code on stdin and, when it finds a wildcard import statement, does the import and replaces the line with a full multi-line import statement:

from bar import (item3,
                 item4,
                 item5,
                 item6)

You can then easily go through and delete any items that pylint tells you aren't needed.

If you prefer a single (possibly long) import line, you can use the --single-line option. One advantage is that some tools such as autoflake and autopep8 handle this format better.

The parentheses are there to allow it to span multiple lines, but it shouldn't be too difficult to change the code to make it a single line or to use backslashes for line continuation if you prefer that.

NOTE: This has many limitations, the main one being that dewildcard must actually perform the imports in order to extract the symbol names, so you must run this in an environment where the appropriate modules exist, are on the Python path, and can be imported without unfortunate side-effects.

Installation

pip install dewildcard

Note that dewildcard makes use of importlib, so Python 2.7 or later will be needed.

Example usage

dewildcard my_code.py

This outputs the modified file to stdout. If you wish to modify it in place, add a -w option:

dewildcard -w my_code.py

Acknowledgements

Dewildcard is based on an initial idea from Alexandre Fayolle - thanks, Alexandre!

Other contributors include Jan Bieroń and Jakub Wilk - thanks!

To Do

Lots of room for improvements here, including:

  • Options to change the output format

Licence

Such a simple script is barely worth a licence, but, for what it's worth, it's released under GNU General Public Licence v2. Use at your own risk, etc.

(c) 2015 Quentin Stafford-Fraser

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dewildcard-0.2.1.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

dewildcard-0.2.1-py2-none-any.whl (3.7 kB view details)

Uploaded Python 2

File details

Details for the file dewildcard-0.2.1.tar.gz.

File metadata

  • Download URL: dewildcard-0.2.1.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.10

File hashes

Hashes for dewildcard-0.2.1.tar.gz
Algorithm Hash digest
SHA256 ec74af9121fe8dd5a17ac1a61104c1f351fcd0c77a7ff3c3512b290a2c9b43de
MD5 cc24b23a444ee718a7553f813c2bd904
BLAKE2b-256 0d0a21255d0477cfc9f885941484c041cd1b9d98e1763773dee72afab354991b

See more details on using hashes here.

File details

Details for the file dewildcard-0.2.1-py2-none-any.whl.

File metadata

  • Download URL: dewildcard-0.2.1-py2-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.10

File hashes

Hashes for dewildcard-0.2.1-py2-none-any.whl
Algorithm Hash digest
SHA256 ea4bb9a7a186c7ff11a4de972afb92920c6fbd8918de0165b1a4d32382997c0d
MD5 2093bef1043545534a6eb93f0c12f864
BLAKE2b-256 e1b82f3fbef31359f8fd68b0e22ad64c4c289118c4b59a65d9ce18d4708cc571

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page