_importclns module

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

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[ImportMap] = ImportMap({})
_data: Dict
classmethod _from_map(arg)
Return type:

ImportMap

classmethod _merge(maps)
Return type:

ImportMap

items()
Return type:

Any

iteritems()
Return type:

Any

iterkeys()
Return type:

Iterator[str]

keys()
Return type:

Any

values()
Return type:

Any

without_imports(removals)

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

Return type:

ImportMap

class pyflyby._importclns.ImportSet(arg: Any, ignore_nonimports: bool = False, ignore_shadowed: bool = 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: Tuple[Dict[str, FrozenSet[Import]], ...]

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 (bool) – See ImportSet.__new__.

Return type:

Self ImportSet

classmethod _from_imports(imports, ignore_shadowed=False)
Parameters:

ignore_shadowed (bool) – See ImportSet.__new__.

Return type:

Self ImportSet

_importset: FrozenSet[Import]
property by_import_as: Dict[str, Tuple[Import, ...]]

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: Tuple[str, ...]

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: CompilerFlags

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[ImportStatement, ...] tuple of ImportStatement s

property imports: Tuple[Import, ...]

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

Return type:

tuple of Import s

property member_names: Dict[str, Tuple[str, ...]]

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 str

property statements: Tuple[ImportStatement, ...]

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 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 ImportSet

exception pyflyby._importclns.NoSuchImportError