def test_Import_import_as_same_1():
    imp = Import("import   foo . bar  as  bar")
    assert imp.fullname == "foo.bar"
    assert imp.import_as == "bar"
    assert imp.split == ImportSplit("foo", "bar", None)
    assert str(imp) == "from foo import bar"
    assert imp == Import("from foo import bar")
def test_ImportStatement_relative_local_1():
    stmt = ImportStatement("from .  import bar , bar2 as baz2")
    assert stmt.fromname == "."
    assert stmt.aliases == (("bar", None), ("bar2", "baz2"))
    assert stmt.imports == (Import(ImportSplit(".", "bar", None)),
                            Import(ImportSplit(".", "bar2", "baz2")))
    assert str(stmt) == "from . import bar, bar2 as baz2"
def test_Import_from_Statement_1():
    imp = Import(ImportStatement("from foo import bar"))
    assert imp.fullname == "foo.bar"
    assert imp.import_as == "bar"
    assert imp.split == ImportSplit("foo", "bar", None)
    assert str(imp) == "from foo import bar"
    assert imp == Import("from foo import bar")
def test_ImportStatement_alias_1():
    stmt = ImportStatement("from foo  import bar  as  bar,   bar as   baz")
    assert stmt.fromname == "foo"
    assert stmt.aliases == (("bar", "bar"), ("bar", "baz"))
    assert stmt.imports == (Import(ImportSplit("foo", "bar", "bar")),
                            Import(ImportSplit("foo", "bar", "baz")))
    assert str(stmt) == "from foo import bar as bar, bar as baz"
Esempio n. 5
0
def test_scan_for_import_issues_dictcomp_unused_1():
    code = dedent("""
        import x1, x2, x3
        {123:x3 for x1,x2 in []}
    """)
    missing, unused = scan_for_import_issues(code)
    assert missing == []
    assert unused == [(2, Import('import x1')), (2, Import('import x2'))]
def test_Import_eqne_2():
    imp1a = Import("from foo import bar")
    imp1b = Import("from foo import bar")
    imp2 = Import("from foo import bar as Bar")
    assert (imp1a == imp1b)
    assert not (imp1a != imp1b)
    assert (imp1a != imp2)
    assert not (imp1a == imp2)
def test_ImportStatement_multi_1():
    stmt = ImportStatement("from foo  import bar, bar2, bar")
    assert stmt.fromname == "foo"
    assert stmt.aliases == (("bar", None), ("bar2", None), ("bar", None))
    assert stmt.imports == (Import(ImportSplit("foo", "bar", None)),
                            Import(ImportSplit("foo", "bar2", None)),
                            Import(ImportSplit("foo", "bar", None)))
    assert str(stmt) == "from foo import bar, bar2, bar"
Esempio n. 8
0
def test_ImportSet_constructor_string_1():
    importset = ImportSet("from m1 import c, b; from m1 import a as a")
    expected = [
        Import("from m1 import a"),
        Import("from m1 import b"),
        Import("from m1 import c")
    ]
    assert list(importset) == expected
Esempio n. 9
0
def test_ImportSet_constructor_importstmt_1():
    importset = ImportSet(ImportStatement("from m1 import a, b, c"))
    expected = [
        Import("from m1 import a"),
        Import("from m1 import b"),
        Import("from m1 import c")
    ]
    assert list(importset) == expected
Esempio n. 10
0
def test_ImportSet_constructor_list_2():
    importset = ImportSet(["from m1 import c, b", "from m1 import a as a"])
    expected = [
        Import("from m1 import a"),
        Import("from m1 import b"),
        Import("from m1 import c")
    ]
    assert list(importset) == expected
Esempio n. 11
0
def test_ImportSet_constructor_list_imports_1():
    expected = [
        Import("from m1 import a"),
        Import("from m1 import b"),
        Import("from m1 import c")
    ]
    importset = ImportSet(expected)
    assert list(importset) == expected
Esempio n. 12
0
def test_ImportDB_by_fullname_or_import_as_1():
    db = ImportDB('from aa.bb import cc as dd')
    result = db.by_fullname_or_import_as
    expected = {
        'aa': (Import('import aa'), ),
        'aa.bb': (Import('import aa.bb'), ),
        'dd': (Import('from aa.bb import cc as dd'), )
    }
    assert result == expected
Esempio n. 13
0
def test_ImportSet_contains_1():
    importset = ImportSet('''
        from m1 import f1
        from m2 import f1
        from m1 import f2
        from m1 import f1, f3
        import m3.m4 as m34
    ''')
    assert Import("from  m1 import f1") in importset
    assert Import("from .m1 import f1") not in importset
    assert Import("from  m2 import f2") not in importset
Esempio n. 14
0
def test_ImportSet_by_import_as_1():
    importset = ImportSet('''
        from a1.b1 import c1 as x
        from a2.b2 import c2 as x
        from a2.b2 import c2 as y
    ''')
    expected = {
        'x': (Import('from a1.b1 import c1 as x'),
              Import('from a2.b2 import c2 as x')),
        'y': (Import('from a2.b2 import c2 as y'), )
    }
    assert importset.by_import_as == expected
Esempio n. 15
0
def test_ImportSet_1():
    importset = ImportSet('''
        from m1 import f1
        from m2 import f1
        from m1 import f2
        from m1 import f1, f3
        import m3.m4 as m34
    ''')
    expected = (Import("from m1 import f1"), Import("from m1 import f2"),
                Import("from m1 import f3"), Import("from m2 import f1"),
                Import("from m3 import m4 as m34"))
    print importset.imports
    assert importset.imports == expected
    assert len(importset) == 5
Esempio n. 16
0
 def without_imports(self, removals):
     """
     Return a copy of self without the given imports.
     Matches both keys and values.
     """
     removals = ImportSet(removals)
     if not removals:
         return self  # Optimization
     cls = type(self)
     result = [(k, v) for k, v in self._data.items()
               if Import(k) not in removals and Import(v) not in removals]
     if len(result) == len(self._data):
         return self  # Space optimization
     return cls(dict(result))
Esempio n. 17
0
 def _from_imports(cls, imports, ignore_shadowed=False):
     """
     @type imports:
       Sequence of L{Import}s
     @param ignore_shadowed:
       See L{ImportSet.__new__}.
     @rtype:
       L{ImportSet}
     """
     # Canonicalize inputs.
     imports = [Import(imp) for imp in imports]
     if ignore_shadowed:
         # Filter by overshadowed imports.  Later imports take precedence.
         by_import_as = {}
         for imp in imports:
             if imp.import_as == "*":
                 # Keep all unique star imports.
                 by_import_as[imp] = imp
             else:
                 by_import_as[imp.import_as] = imp
         filtered_imports = by_import_as.values()
     else:
         filtered_imports = imports
     # Construct and return.
     self = object.__new__(cls)
     self._importset = frozenset(filtered_imports)
     return self
Esempio n. 18
0
    def by_fullname_or_import_as(self):
        """
        Map from C{fullname} and C{import_as} to L{Import}s.

          >>> import pprint
          >>> db = ImportDB('from aa.bb import cc as dd')
          >>> pprint.pprint(db.by_fullname_or_import_as)
          {'aa': (Import('import aa'),),
           'aa.bb': (Import('import aa.bb'),),
           'dd': (Import('from aa.bb import cc as dd'),)}

        @rtype:
          C{dict} mapping from C{str} to tuple of L{Import}s
        """
        # TODO: make known_imports take into account the below forget_imports,
        # then move this function into ImportSet
        d = defaultdict(set)
        for imp in self.known_imports.imports:
            # Given an import like "from foo.bar import quux as QUUX", add the
            # following entries:
            #   - "QUUX"         => "from foo.bar import quux as QUUX"
            #   - "foo.bar"      => "import foo.bar"
            #   - "foo"          => "import foo"
            # We don't include an entry labeled "quux" because the user has
            # implied he doesn't want to pollute the global namespace with
            # "quux", only "QUUX".
            d[imp.import_as].add(imp)
            for prefix in dotted_prefixes(imp.fullname)[:-1]:
                d[prefix].add(Import.from_parts(prefix, prefix))
        return dict((k, tuple(sorted(v - set(self.forget_imports.imports))))
                    for k, v in six.iteritems(d))
Esempio n. 19
0
def _reformat_helper(input_code, imports):
    from pyflyby._imports2s import reformat_import_statements

    if PYFLYBY_START_MSG in input_code:
        before, bmarker, middle = input_code.partition(PYFLYBY_START_MSG)
    else:
        before, bmarker, middle = "", "", input_code

    if PYFLYBY_END_MSG in middle:
        middle, emarker, after = middle.partition(PYFLYBY_END_MSG)
    else:
        middle, emarker, after = middle, "", ""

    if imports is not None:
        transform = SourceToSourceFileImportsTransformation(middle)

        if isinstance(imports, str):
            imports = [imports]

        for imp in imports:
            assert isinstance(imp, str)
            if not imp.strip():
                continue
            transform.add_import(Import(imp))
        middle = str(transform.output())

    return reformat_import_statements(before + bmarker + middle + emarker +
                                      after)
Esempio n. 20
0
def test_scan_for_import_issues_setcomp_unused_1():
    code = dedent("""
        import x1, x2
        {x2 for x1 in []}
    """)
    missing, unused = scan_for_import_issues(code)
    assert missing == []
    assert unused == [(2, Import('import x1'))]
Esempio n. 21
0
def test_scan_for_import_issues_brace_identifiers_bad_1():
    code = dedent("""
        import x1, x2, x3
        def f():
            '''{x1} {x3} {if}'''
    """)
    missing, unused = scan_for_import_issues(code, parse_docstrings=True)
    assert missing == []
    assert unused == [(2, Import('import x2'))]
Esempio n. 22
0
def test_ImportDB_pyflyby_dotdotdot_1():
    with EnvVarCtx(PYFLYBY_PATH=".../f1198375"):
        d = mkdtemp("_pyflyby")
        os.mkdir("%s/d1" % d)
        os.mkdir("%s/d1/d2" % d)
        os.mkdir("%s/d1/d2/d3" % d)
        with open("%s/f1198375" % d, 'w') as f:
            f.write("from m97722423 import f49463937, f3532073\n")
        with open("%s/d1/d2/f1198375" % d, 'w') as f:
            f.write("from m90927291 import f6273971, f49463937\n")
        db = ImportDB.get_default("%s/d1/d2/d3/f" % d)
        result = db.by_fullname_or_import_as["f49463937"]
        expected = (
            Import("from m90927291 import f49463937"),
            Import("from m97722423 import f49463937"),
        )
        assert result == expected
        rmtree(d)
Esempio n. 23
0
def test_scan_for_import_issues_star_import_1():
    code = dedent("""
        import x1, y1
        x1, x2
        from m2 import *
        x3
    """)
    missing, unused = scan_for_import_issues(code, parse_docstrings=True)
    assert missing == [(3, DottedIdentifier('x2'))]
    assert unused == [(2, Import('import y1'))]
Esempio n. 24
0
def test_ImportDB_pyflyby_path_filename_1():
    # Check that PYFLYBY_PATH set to a filename works.
    with NamedTemporaryFile() as f:
        f.write("from m4065635 import f78841936, f44111337, f73485346\n")
        f.flush()
        with EnvVarCtx(PYFLYBY_PATH=f.name):
            db = ImportDB.get_default('/bin')
        assert isinstance(db, ImportDB)
        result = db.by_fullname_or_import_as["f44111337"]
        expected = (Import('from m4065635 import f44111337'), )
        assert result == expected
Esempio n. 25
0
def test_scan_for_import_issues_star_import_deferred_1():
    code = dedent("""
        import x1, y1
        def f1():
            x1, x2
        from m2 import *
        def f1():
            x3
    """)
    missing, unused = scan_for_import_issues(code, parse_docstrings=True)
    assert missing == []
    assert unused == [(2, Import('import y1'))]
Esempio n. 26
0
def test_ImportDB_pyflyby_path_no_default_1():
    # Check that defaults can be turned off from PYFLYBY_PATH.
    with NamedTemporaryFile() as f:
        f.write("from m27056973 import f8855924\n")
        f.flush()
        with EnvVarCtx(PYFLYBY_PATH=f.name):
            db = ImportDB.get_default('/bin')
        assert isinstance(db, ImportDB)
        result = db.by_fullname_or_import_as["f8855924"]
        expected = (Import('from m27056973 import f8855924'), )
        assert result == expected
        assert "defaultdict" not in db.by_fullname_or_import_as
        expected_bfoia = {
            "f8855924": (Import("from m27056973 import f8855924"), ),
            "m27056973": (Import("import m27056973"), ),
        }
        assert db.by_fullname_or_import_as == expected_bfoia
    # For the default PYFLYBY_PATH (configured in conftest.py), we should have
    # defaultdict.
    db2 = ImportDB.get_default('/bin')
    result = db2.by_fullname_or_import_as["defaultdict"]
    expected = (Import('from collections import defaultdict'), )
    assert result == expected
Esempio n. 27
0
def test_ImportDB_pyflyby_path_change_1():
    # Check that memoization takes into account changes in
    # os.environ["PYFLYBY_PATH"].
    with NamedTemporaryFile() as f:
        f.write("from m60309242 import f5781152\n")
        f.flush()
        with EnvVarCtx(PYFLYBY_PATH=f.name):
            db = ImportDB.get_default('/bin')
        result = db.by_fullname_or_import_as["f5781152"]
        expected = (Import('from m60309242 import f5781152'), )
        assert result == expected
        db2 = ImportDB.get_default('/bin')
        assert db2 is not db
        assert "f5781152" not in db2.by_fullname_or_import_as
Esempio n. 28
0
def test_scan_for_import_issues_star_import_local_1():
    code = dedent("""
        import x1, y1
        def f1():
            from m2 import *
            x2
        def f2():
            x3
        x1, x4
    """)
    missing, unused = scan_for_import_issues(code, parse_docstrings=True)
    assert missing == [(7, DottedIdentifier('x3')),
                       (8, DottedIdentifier('x4'))]
    assert unused == [(2, Import('import y1'))]
Esempio n. 29
0
 def _from_args(cls, args, ignore_nonimports=False, ignore_shadowed=False):
     """
     :type args:
       ``tuple`` or ``list`` of `ImportStatement` s, `PythonStatement` s,
       `PythonBlock` s, `FileText`, and/or `Filename` s
     :param ignore_nonimports:
       If ``False``, complain about non-imports.  If ``True``, ignore
       non-imports.
     :param ignore_shadowed:
       See `ImportSet.__new__`.
     :rtype:
       `ImportSet`
     """
     if not isinstance(args, (tuple, list)):
         args = [args]
     # Filter empty arguments to allow the subsequent optimizations to work
     # more often.
     args = [a for a in args if a]
     if not args:
         return cls._EMPTY
     # If we only got one ``ImportSet``, just return it.
     if len(args) == 1 and type(args[0]) is cls and not ignore_shadowed:
         return args[0]
     # Collect all `Import` s from arguments.
     imports = []
     for arg in args:
         if isinstance(arg, Import):
             imports.append(arg)
         elif isinstance(arg, ImportSet):
             imports.extend(arg.imports)
         elif isinstance(arg, ImportStatement):
             imports.extend(arg.imports)
         elif isinstance(arg, str) and is_identifier(arg, dotted=True):
             imports.append(Import(arg))
         else: # PythonBlock, PythonStatement, Filename, FileText, str
             block = PythonBlock(arg)
             for statement in block.statements:
                 # Ignore comments/blanks.
                 if statement.is_comment_or_blank:
                     pass
                 elif statement.is_import:
                     imports.extend(ImportStatement(statement).imports)
                 elif ignore_nonimports:
                     pass
                 else:
                     raise NonImportStatementError(
                         "Got non-import statement %r" % (statement,))
     return cls._from_imports(imports, ignore_shadowed=ignore_shadowed)
Esempio n. 30
0
 def _from_args(cls, args, ignore_nonimports=False, ignore_shadowed=False):
     """
     @type args:
       C{tuple} or C{list} of L{ImportStatement}s, L{PythonStatement}s,
       L{PythonBlock}s, L{FileText}, and/or L{Filename}s
     @param ignore_nonimports:
       If C{False}, complain about non-imports.  If C{True}, ignore
       non-imports.
     @param ignore_shadowed:
       See L{ImportSet.__new__}.
     @rtype:
       L{ImportSet}
     """
     if not isinstance(args, (tuple, list)):
         args = [args]
     # Filter empty arguments to allow the subsequent optimizations to work
     # more often.
     args = [a for a in args if a]
     if not args:
         return cls._EMPTY
     # If we only got one C{ImportSet}, just return it.
     if len(args) == 1 and type(args[0]) is cls and not ignore_shadowed:
         return args[0]
     # Collect all L{Import}s from arguments.
     imports = []
     for arg in args:
         if isinstance(arg, Import):
             imports.append(arg)
         elif isinstance(arg, ImportSet):
             imports.extend(arg.imports)
         elif isinstance(arg, ImportStatement):
             imports.extend(arg.imports)
         elif isinstance(arg, str) and is_identifier(arg, dotted=True):
             imports.append(Import(arg))
         else:  # PythonBlock, PythonStatement, Filename, FileText, str
             block = PythonBlock(arg)
             for statement in block.statements:
                 # Ignore comments/blanks.
                 if statement.is_comment_or_blank:
                     pass
                 elif statement.is_import:
                     imports.extend(ImportStatement(statement).imports)
                 elif ignore_nonimports:
                     pass
                 else:
                     raise NonImportStatementError(
                         "Got non-import statement %r" % (statement, ))
     return cls._from_imports(imports, ignore_shadowed=ignore_shadowed)