コード例 #1
0
def test_pyfill_5():
    result = pyfill('print ', ["foo.bar", "baz", "quux", "quuuuux"],
                    FormatParams(max_line_length=14, hanging_indent='auto'))
    expected = dedent("""
        print (
            foo.bar,
            baz, quux,
            quuuuux)
    """).lstrip()
    assert result == expected
コード例 #2
0
def test_pyfill_hanging_indent_always_1():
    prefix = 'from   foo                      import '
    #          <---------------39 chars-------------->
    tokens = ['x23456789a123456789b123456789c123456789','z1','z2']
    params = FormatParams(max_line_length=79, hanging_indent='always')
    result = pyfill(prefix, tokens, params)
    expected = dedent("""
        from   foo                      import (
            x23456789a123456789b123456789c123456789, z1, z2)
    """).lstrip()
    assert result == expected
コード例 #3
0
    def pretty_print(self,
                     params=FormatParams(),
                     import_column=None,
                     from_spaces=1):
        """
        Pretty-print into a single string.

        :type params:
          `FormatParams`
        :param modulename_ljust:
          Number of characters to left-justify the 'from' name.
        :rtype:
          ``str``
        """
        s0 = ''
        s = ''
        assert from_spaces >= 1
        if self.fromname is not None:
            s += "from%s%s " % (' ' * from_spaces, self.fromname)
            if import_column is not None:
                if len(s) > import_column:
                    # The caller wants the 'import' statement lined up left of
                    # where the current end of the line is.  So wrap it
                    # specially like this::
                    #     from foo     import ...
                    #     from foo.bar.baz \
                    #                  import ...
                    s0 = s + '\\\n'
                    s = ' ' * import_column
                else:
                    s = s.ljust(import_column)
        s += "import "
        tokens = []
        for importname, asname in self.aliases:
            if asname is not None:
                t = "%s as %s" % (importname, asname)
            else:
                t = "%s" % (importname, )
            tokens.append(t)
        res = s0 + pyfill(s, tokens, params=params)
        if params.use_black:
            import black
            mode = black.FileMode()
            return black.format_str(res, mode=mode)
        return res
コード例 #4
0
 def __str__(self):
     return self.pretty_print(FormatParams(max_line_length=Inf)).rstrip()
コード例 #5
0
 def pretty_print(self, params=FormatParams()):
     return ImportStatement([self]).pretty_print(params)