_importclns module

exception pyflyby._importclns.ConflictingImportsError
class pyflyby._importclns.ImportMap(arg)

A map from import fullname identifier to fullname identifier.

>>> ImportMap({'a.b': 'aa.bb', 'a.b.c': 'aa.bb.cc'})
ImportMap({'a.b': 'aa.bb', 'a.b.c': 'aa.bb.cc'})

An ImportMap is an immutable data structure.

_EMPTY: ClassVar[ImportSet] = ImportMap({})
_data: Dict
classmethod _from_map(arg)
classmethod _merge(maps)
items()
iteritems()
iterkeys()
keys()
values()
without_imports(removals)

Return a copy of self without the given imports. Matches both keys and values.

class pyflyby._importclns.ImportSet(arg, ignore_nonimports=False, ignore_shadowed=False)

Representation of a set of imports organized into import statements.

>>> ImportSet('''
...     from m1 import f1
...     from m2 import f1
...     from m1 import f2
...     import m3.m4 as m34
... ''')
ImportSet('''
  from m1 import f1, f2
  from m2 import f1
  from m3 import m4 as m34
''')

An ImportSet is an immutable data structure.

_EMPTY: ClassVar[ImportSet] = ImportSet(''' ''')
property _by_module_name

return: (mapping from name to __future__ imports, mapping from name to non-‘from’ imports, mapping from name to ‘from’ imports)

classmethod _from_args(args, ignore_nonimports=False, ignore_shadowed=False)
Parameters:
  • ignore_nonimports (bool) – If False, complain about non-imports. If True, ignore non-imports.

  • ignore_shadowed – See ImportSet.__new__.

Return type:

ImportSet

classmethod _from_imports(imports, ignore_shadowed=False)
Parameters:

ignore_shadowed (bool) – See ImportSet.__new__.

Return type:

ImportSet

_importset: frozenset[Import]
property by_import_as

Map from import_as to Import.

>>> ImportSet('from aa.bb import cc as dd').by_import_as
{'dd': (Import('from aa.bb import cc as dd'),)}
Return type:

dict mapping from str to tuple of Import s

property conflicting_imports

Returns imports that conflict with each other.

>>> ImportSet('import b\nfrom f import a as b\n').conflicting_imports
('b',)
>>> ImportSet('import b\nfrom f import a\n').conflicting_imports
()
Return type:

bool

property flags

If this contains __future__ imports, then the bitwise-ORed of the compiler_flag values associated with the features. Otherwise, 0.

get_statements(separate_from_imports=True)

Canonicalized ImportStatement s. These have been merged by module and sorted.

>>> importset = ImportSet('''
...     import a, b as B, c, d.dd as DD
...     from __future__ import division
...     from _hello import there
...     from _hello import *
...     from _hello import world
... ''')
>>> for s in importset.get_statements(): print(s)
from __future__ import division
import a
import b as B
import c
from _hello import *
from _hello import there, world
from d import dd as DD
Return type:

tuple of ImportStatement s

property imports

Canonicalized imports, in the same order as self.statements.

Return type:

tuple of Import s

property member_names

Map from parent module/package fullname to known member names.

>>> impset = ImportSet("import numpy.linalg.info\nfrom sys import exit as EXIT")
>>> import pprint
>>> pprint.pprint(impset.member_names)
{'': ('EXIT', 'numpy', 'sys'),
 'numpy': ('linalg',),
 'numpy.linalg': ('info',),
 'sys': ('exit',)}

This is used by the autoimporter module for implementing tab completion.

Return type:

dict mapping from str to tuple of str

pretty_print(params=None, allow_conflicts=False)

Pretty-print a block of import statements into a single string.

Return type:

str

property statements

Canonicalized ImportStatement s. These have been merged by module and sorted.

Return type:

tuple of ImportStatement s

with_imports(other)

Return a new ImportSet that is the union of self and new_imports.

>>> impset = ImportSet('from m import t1, t2, t3')
>>> impset.with_imports('import m.t2a as t2b')
ImportSet('''
  from m import t1, t2, t2a as t2b, t3
''')
Return type:

ImportSet

without_imports(removals)

Return a copy of self without the given imports.

>>> imports = ImportSet('from m import t1, t2, t3, t4')
>>> imports.without_imports(['from m import t3'])
ImportSet('''
  from m import t1, t2, t4
''')
Return type:

ImportSet

exception pyflyby._importclns.NoSuchImportError