Ejemplo n.º 1
0
def test_import():
    # Test module names without periods.
    res = parse_code_element('from   a    import  b')
    assert res == CodeElementImport(path=ExprIdentifier(name='a'),
                                    orig_identifier=ExprIdentifier(name='b'),
                                    local_name=None)
    assert res.format(allowed_line_length=100) == 'from a import b'

    # Test module names without periods, with aliasing.
    res = parse_code_element('from   a    import  b   as   c')
    assert res == CodeElementImport(path=ExprIdentifier(name='a'),
                                    orig_identifier=ExprIdentifier(name='b'),
                                    local_name=ExprIdentifier(name='c'))
    assert res.format(allowed_line_length=100) == 'from a import b as c'

    # Test module names with periods.
    res = parse_code_element('from   a.b12.c4    import  lib345')
    assert res == CodeElementImport(
        path=ExprIdentifier(name='a.b12.c4'),
        orig_identifier=ExprIdentifier(name='lib345'))
    assert res.format(allowed_line_length=100) == 'from a.b12.c4 import lib345'

    # Test module with bad identifier (with periods).
    with pytest.raises(ParserError):
        parse_expr('from a.b import c.d')

    # Test module with bad local name (with periods).
    with pytest.raises(ParserError):
        parse_expr('from a.b import c as d.d')
Ejemplo n.º 2
0
def test_import():
    # Test module names without periods.
    res = parse_code_element('from   a    import  b')
    assert res == CodeElementImport(
        path=ExprIdentifier(name='a'),
        import_items=[
            AliasedIdentifier(orig_identifier=ExprIdentifier(name='b'),
                              local_name=None)
        ])
    assert res.format(allowed_line_length=100) == 'from a import b'

    # Test module names without periods, with aliasing.
    res = parse_code_element('from   a    import  b   as   c')
    assert res == CodeElementImport(
        path=ExprIdentifier(name='a'),
        import_items=[
            AliasedIdentifier(orig_identifier=ExprIdentifier(name='b'),
                              local_name=ExprIdentifier(name='c'))
        ])
    assert res.format(allowed_line_length=100) == 'from a import b as c'

    # Test module names with periods.
    res = parse_code_element('from   a.b12.c4    import  lib345')
    assert res == CodeElementImport(
        path=ExprIdentifier(name='a.b12.c4'),
        import_items=[
            AliasedIdentifier(orig_identifier=ExprIdentifier(name='lib345'),
                              local_name=None)
        ])
    assert res.format(allowed_line_length=100) == 'from a.b12.c4 import lib345'

    # Test multiple imports.
    res = parse_code_element('from  lib    import  a,b as    b2,   c')

    assert res == CodeElementImport(
        path=ExprIdentifier(name='lib'),
        import_items=[
            AliasedIdentifier(orig_identifier=ExprIdentifier(name='a'),
                              local_name=None),
            AliasedIdentifier(orig_identifier=ExprIdentifier(name='b'),
                              local_name=ExprIdentifier(name='b2')),
            AliasedIdentifier(orig_identifier=ExprIdentifier(name='c'),
                              local_name=None),
        ])
    assert res.format(
        allowed_line_length=100) == 'from lib import a, b as b2, c'
    assert res.format(
        allowed_line_length=20) == 'from lib import (\n    a, b as b2, c)'

    assert res == parse_code_element('from lib import (\n    a, b as b2, c)')

    # Test module with bad identifier (with periods).
    with pytest.raises(ParserError):
        parse_expr('from a.b import c.d')

    # Test module with bad local name (with periods).
    with pytest.raises(ParserError):
        parse_expr('from a.b import c as d.d')
Ejemplo n.º 3
0
    def code_element_import(self, value, meta):
        if len(value) == 2:
            # Statement of the form:
            # from <path> import <identifier>.
            path, identifier = value
            local_name = None
        elif len(value) == 3:
            # Statement of the form:
            # from <path> import <identifier> as <local_name>.
            path, identifier, local_name = value
        else:
            raise NotImplementedError(f'Unexpected argument: value={value}')

        return CodeElementImport(path=path,
                                 orig_identifier=identifier,
                                 local_name=local_name,
                                 location=self.meta2loc(meta))
Ejemplo n.º 4
0
    def code_element_import(self, value, meta):
        path = value[0]
        if isinstance(value[1], ImportItem):
            # Single line.
            import_items = value[1:]
            notes = []
        else:
            # Multiline.
            assert len(value) % 3 == 2, f'Unexpected value {value}.'
            import_items = value[2::3]
            # Join the notes before and after the comma.
            notes = [value[1]] + [
                value[i] + value[i + 1] for i in range(3,
                                                       len(value) - 1, 3)
            ]

        return CodeElementImport(
            path=path,
            import_items=import_items,
            notes=notes,
            location=self.meta2loc(meta),
        )