Skip to content

A Python library for finding unresolved symbols in Python code, and the corresponding imports

License

Notifications You must be signed in to change notification settings

kassick-ifood/importmagic

This branch is up to date with alecthomas/importmagic:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

a6cde18 · Oct 23, 2020
Mar 12, 2018
Jan 10, 2019
Nov 9, 2014
Oct 17, 2015
Oct 23, 2020
Feb 11, 2015
Sep 12, 2015
Sep 26, 2016
Jul 2, 2018
Mar 10, 2014
Mar 9, 2014
Oct 17, 2015
Mar 12, 2018

Repository files navigation

Import Magic image

The goal of this package is to be able to automatically manage imports in Python. To that end it can:

  • Build an index of all known symbols in all packages.
  • Find unresolved references in source, and resolve them against the index, effectively automating imports.
  • Automatically arrange imports according to PEP8.

It was originally written for the Sublime Text 2 Python Import Magic plugin.

Example of use in Sublime Text 2 plugin

Example of Import Magic at work

Using the library

Getting index from cache:

index = importmagic.SymbolIndex()
index.get_or_create_index(name='foo', paths=sys.path)

Build an index:

index = importmagic.SymbolIndex()
index.build_index(sys.path)
with open('index.json') as fd:
    index.serialize(fd)

Load an existing index:

with open('index.json') as fd:
    index = SymbolIndex.deserialize(fd)

Find unresolved and unreferenced symbols:

scope = importmagic.Scope.from_source(python_source)
unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()

Print new import block:

start_line, end_line, import_block = importmagic.get_update(python_source, index, unresolved, unreferenced)

Update source code with new import blocks:

python_source = importmagic.update_imports(python_source, index, unresolved, unreferenced)

For more fine-grained control over what symbols are imported, the index can be queried directly:

imports = importmagic.Imports(index, python_source)
imports.remove(unreferenced)

for symbol in unresolved:
    for score, module, variable in index.symbol_scores(symbol):
        if variable is None:
            imports.add_import(module)
        else:
            imports.add_import_from(module, variable)
        break

python_source = imports.update_source()

Configuration

Configuring import styles

  1. Using importmagic.Imports.
imports = importmagic.Imports.set_style(multiline='backslash', max_columns=80, indent_with_tabs=True)

multiline takes backlslash or parentheses.

  1. From setup.cfg

Add configuration to setup.cfg

[importmagic]
multiline = 'parentheses'
max_columns = 120
indent_with_tabs = 1

and pass root directory to importmagic

imports = importmagic.Imports(root_dir='/foo/bar/')

About

A Python library for finding unresolved symbols in Python code, and the corresponding imports

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%