def test_format_code_skip_nested(self): code = """\ def foo(): '''Hello foo. \"\"\"abc\"\"\" ''' """ self.assertEqual(code, docformatter.format_code(code))
def run(self): if self.lines is None: with open(self.filename) as f: self.lines = f.readlines() if self.lines and PYTHON_SHEBANG not in self.lines[0]: yield ( 1, 0, 'PAI100 Missing python3 shebang. (`#!/usr/bin/env python3`)', '', ) # check copyrighting next source = "".join(self.lines) formatted_source = docformatter.format_code( source, pre_summary_newline=True, description_wrap_length=88, summary_wrap_length=88, make_summary_multi_line=True, force_wrap=False, ) if source != formatted_source: diff = difflib.unified_diff( source.split('\n'), # have to strip newlines formatted_source.split('\n'), f'before/{self.filename}', f'after/{self.filename}', n=0, lineterm='', ) for line in diff: if line.startswith('@@'): fields = line.split() # find out the beginning line of the docstring reformat. Example: # --- /path/to/original timestamp # +++ /path/to/new timestamp # @@ -1,3 +1,9 @@ # that -1 says the first line changed, and 3 lines were removed # with a new offset belonging at the first line, and 9 # inserted lines. line_no, _ = fields[1].split(',') line_no = -int(line_no) yield ( line_no, 1, f'PAI101 autoformat.sh would reformat the docstring', '', ) # the rest is checking copyright, but there are some exceptions if any(wl in source for wl in WHITELIST_PHRASES): return for i, msg in enumerate(COPYRIGHT, 1): if any(wl in self.filename for wl in WHITELIST_FNS) and i < 3: continue if msg not in source: yield (i, 0, f'PAI20{i} Missing copyright `{msg}`', '')
def fix_line_range(source_code, start, end, options): """Apply autopep8 (and docformatter) between the lines start and end of source.""" # TODO confirm behaviour outside range (indexing starts at 1) start = max(start, 1) options.line_range = [start, end] from autopep8 import fix_code fixed = fix_code(source_code, options) try: if options.docformatter: from docformatter import format_code fixed = format_code( fixed, summary_wrap_length=options.max_line_length - 1, description_wrap_length=(options.max_line_length - 2 * options.indent_size), pre_summary_newline=options.pre_summary_newline, post_description_blank=options.post_description_blank, force_wrap=options.force_wrap, line_range=[start, end]) except AttributeError: # e.g. using autopep8.parse_args, pragma: no cover pass return fixed
def test_format_code_with_module_docstring(self): self.assertEqual( '''\ #!/usr/env/bin python """This is a module docstring. 1. One 2. Two """ """But this is not.""" ''', docformatter.format_code('''\ #!/usr/env/bin python """This is a module docstring. 1. One 2. Two """ """But this is not.""" '''))
def format_docstring(self, doc_string: str, level: int) -> str: """Format doc strings.""" content, params = doc_string.rsplit('"""', 1) params = params.strip() if content.strip() == '"""' and not params: return "" content += ' """' if content.endswith('"') else '"""' max_length = self.max_line_length - level * 4 content = format_code( content, summary_wrap_length=max_length, description_wrap_length=max_length - 7, make_summary_multi_line=True, ) if params: content = content.rstrip('"""').strip() new_lines = "\n" if content.endswith('"""') else "\n\n" content += f'{new_lines}{params}\n"""' return content
def test_format_code_docstring_length(self): self.assertEqual( '''\ def f(x): """This is a docstring. That should be on less lines """ pass def g(x): """Badly indented docstring.""" pass''', docformatter.format_code('''\ def f(x): """This is a docstring. That should be on less lines """ pass def g(x): """ Badly indented docstring""" pass''', length_range=[1, 1]))
def test_format_code_with_regular_strings_too(self): self.assertEqual( '''\ def foo(): """Hello foo and this is a docstring. More stuff. """ x = """My non-docstring This should not touched.""" """More stuff that should not be touched\t""" ''', docformatter.format_code('''\ def foo(): """ Hello foo and this is a docstring. More stuff. """ x = """My non-docstring This should not touched.""" """More stuff that should not be touched\t""" '''))
def test_format_code_with_regular_strings_too(self): self.assertEqual( '''\ def foo(): """Hello foo and this is a docstring. More stuff. """ x = """My non-docstring This should not touched.""" """More stuff that should not be touched\t""" ''', docformatter.format_code( '''\ def foo(): """ Hello foo and this is a docstring. More stuff. """ x = """My non-docstring This should not touched.""" """More stuff that should not be touched\t""" '''))
def test_format_code_with_module_docstring(self): self.assertEqual( '''\ #!/usr/env/bin python """This is a module docstring. 1. One 2. Two """ """But this is not.""" ''', docformatter.format_code( '''\ #!/usr/env/bin python """This is a module docstring. 1. One 2. Two """ """But this is not.""" '''))
def test_format_code_should_ignore_non_docstring(self): source = '''\ x = """This is not.""" ''' self.assertEqual(source, docformatter.format_code(source))
def test_format_code_should_ignore_non_docstring(self): source = '''\ x = """This is not.""" ''' self.assertEqual( source, docformatter.format_code(source))
def test_format_code_with_single_quote(self): self.assertEqual( '''\ def foo(): """Just a regular string.""" ''', docformatter.format_code('''\ def foo(): 'Just a regular string' '''))
def test_format_code_with_assignment_on_first_line(self): self.assertEqual( '''\ def foo(): x = """Just a regular string. Alpha.""" ''', docformatter.format_code('''\ def foo(): x = """Just a regular string. Alpha.""" '''))
def PEP8(): class Options(object): aggressive = vim.vars.get('pep8_aggressive', 1) diff = False experimental = True ignore = vim.vars.get('pymode_lint_ignore', ()) in_place = False indent_size = autopep8.DEFAULT_INDENT_SIZE line_range = None max_line_length = int(vim.vars.get('pep8_max_line_length', 78)) pep8_passes = 100 recursive = False select = vim.vars.get('pymode_lint_select', ()) verbose = 0 start = vim.vvars['lnum'] - 1 end = vim.vvars['lnum'] + vim.vvars['count'] - 1 first_non_blank = int(vim.eval('nextnonblank(v:lnum)')) - 1 last_non_blank = int(vim.eval('prevnonblank(v:lnum + v:count - 1)')) - 1 doc_string = False if (first_non_blank >= 0 and doc_start.match(vim.current.buffer[first_non_blank]) and doc_end.match(vim.current.buffer[last_non_blank])): doc_string = True else: # Don't remove trailing blank lines except at end of file while (end < len(vim.current.buffer) and re.match('^\s*$', vim.current.buffer[end - 1])): end += 1 lines = vim.current.buffer[start:end] if not isinstance(lines[0], unicode): lines = [unicode(line, vim.eval('&encoding') or 'utf-8') for line in lines] if doc_string: new_lines = docformatter.format_code( u'\n'.join(lines), force_wrap=bool(vim.vars.get('pep8_force_wrap', 0)), post_description_blank=False, pre_summary_newline=True) else: options = Options() if vim.vars['pep8_indent_only']: options.select = ['E1', 'W1'] new_lines = autopep8.fix_lines(lines, options).lstrip() new_lines = new_lines.split('\n')[: None if doc_string else -1] if new_lines != lines: vim.current.buffer[start:end] = new_lines
def docstring(obj: Class, enum: bool = False) -> str: lines = [] if obj.help: lines.append(obj.help) var_type = "cvar" if enum else "ivar" for attr in obj.attrs: description = attr.help.strip() if attr.help else "" lines.append(f":{var_type} {attr.name}: {description}".strip()) return format_code('"""\n{}\n"""'.format("\n".join(lines))) if lines else ""
def test_format_code_with_assignment_on_first_line(self): self.assertEqual( '''\ def foo(): x = """Just a regular string. Alpha.""" ''', docformatter.format_code( '''\ def foo(): x = """Just a regular string. Alpha.""" '''))
def test_format_code_with_single_quote(self): self.assertEqual( '''\ def foo(): """Just a regular string.""" ''', docformatter.format_code( '''\ def foo(): 'Just a regular string' '''))
def test_format_code(self): self.assertEqual( '''\ def foo(): """Hello foo.""" ''', docformatter.format_code('''\ def foo(): """ Hello foo. """ '''))
def test_format_code(self): self.assertEqual( '''\ def foo(): """Hello foo.""" ''', docformatter.format_code( '''\ def foo(): """ Hello foo. """ '''))
def test_format_code_with_escaped_newline_in_inline_comment(self): self.assertEqual( r''' def foo(): """Hello foo.""" def test_method_no_chr_92(): the501(92) # \ '''.lstrip(), docformatter.format_code(r''' def foo(): """ Hello foo. """ def test_method_no_chr_92(): the501(92) # \ '''.lstrip()))
def test_format_code_with_escaped_newlines(self): self.assertEqual( r'''def foo(): """Hello foo.""" x = \ 1 ''', docformatter.format_code(r'''def foo(): """ Hello foo. """ x = \ 1 '''))
def test_format_code_range_miss(self): self.assertEqual('''\ def f(x): """ This is a docstring. That should be on more lines""" pass def g(x): """ Badly indented docstring""" pass''', docformatter.format_code('''\ def f(x): """ This is a docstring. That should be on more lines""" pass def g(x): """ Badly indented docstring""" pass''', line_range=[1, 1]))
def test_format_code_with_escaped_newline_in_inline_comment(self): self.assertEqual( r''' def foo(): """Hello foo.""" def test_method_no_chr_92(): the501(92) # \ '''.lstrip(), docformatter.format_code( r''' def foo(): """ Hello foo. """ def test_method_no_chr_92(): the501(92) # \ '''.lstrip()))
def test_format_code_with_escaped_newlines(self): self.assertEqual( r'''def foo(): """Hello foo.""" x = \ 1 ''', docformatter.format_code( r'''def foo(): """ Hello foo. """ x = \ 1 '''))
def create_file(fname, group, objs): source_files = [] for obj in objs: class_source = generate_class(obj) if tidy: class_source = autopep8.fix_code( class_source, options=autopep8.parse_args(["--aggressive", "--aggressive", "--aggressive", ""]) ) class_source = format_code(class_source) source_files.append(class_source) source = generate_group(group, source_files) with open("../pyidf/{}.py".format(fname), "w") as f: f.write(source)
def test_format_code_with_multiple_sentences_same_line(self): self.assertEqual( '''\ def foo(): """Hello foo. This is a docstring. """ ''', docformatter.format_code('''\ def foo(): """ Hello foo. This is a docstring. """ '''))
def test_format_code_skip_complex(self): """We do not handle r/u/b prefixed strings.""" self.assertEqual( '''\ def foo(): r""" Hello foo. """ ''', docformatter.format_code('''\ def foo(): r""" Hello foo. """ '''))
def test_format_code_skip_complex_single(self): """We do not handle r/u/b prefixed strings.""" self.assertEqual( """\ def foo(): r''' Hello foo. ''' """, docformatter.format_code("""\ def foo(): r''' Hello foo. ''' """))
def test_format_code_with_mixed_tabs(self): self.assertEqual( '''\ def foo(): \t"""Hello foo.""" \tif True: \t x = 1 ''', docformatter.format_code('''\ def foo(): \t""" \tHello foo. \t""" \tif True: \t x = 1 '''))
def class_docstring(obj: Class, enum: bool = False) -> str: """Generate docstring for the given class and the constructor arguments.""" lines = [] if obj.help: lines.append(obj.help) var_type = "cvar" if enum else "ivar" name_func = constant_name if enum else attribute_name for attr in obj.attrs: description = attr.help.strip() if attr.help else "" lines.append( f":{var_type} {name_func(attr.name)}: {description}".strip()) return format_code('"""\n{}\n"""'.format( "\n".join(lines))) if lines else ""
def test_format_code_skip_complex(self): """We do not handle r/u/b prefixed strings.""" self.assertEqual( '''\ def foo(): r""" Hello foo. """ ''', docformatter.format_code( '''\ def foo(): r""" Hello foo. """ '''))
def test_format_code_skip_complex_single(self): """We do not handle r/u/b prefixed strings.""" self.assertEqual( """\ def foo(): r''' Hello foo. ''' """, docformatter.format_code( """\ def foo(): r''' Hello foo. ''' """))
def test_format_code_with_multiple_sentences_same_line(self): self.assertEqual( '''\ def foo(): """Hello foo. This is a docstring. """ ''', docformatter.format_code( '''\ def foo(): """ Hello foo. This is a docstring. """ '''))
def test_format_code_with_parameters_list(self): self.assertEqual( '''\ def foo(): """Test. one - first two - second """ ''', docformatter.format_code(('''\ def foo(): """Test one - first two - second """ ''')))
def autopep8_line_range(self, f, start, end): """Apply autopep8 between start and end of file f xcasxz.""" self.options.line_range = [start, end] fixed = autopep8.fix_code(f, self.options) if self.options.docformatter: fixed = docformatter.format_code( fixed, summary_wrap_length=self.options.max_line_length - 1, description_wrap_length=(self.options.max_line_length - 2 * self.options.indent_size), pre_summary_newline=self.options.pre_summary_newline, post_description_blank=self.options.post_description_blank, force_wrap=self.options.force_wrap, line_range=[start, end]) return fixed
def test_format_code_with_mixed_tabs(self): self.assertEqual( '''\ def foo(): \t"""Hello foo.""" \tif True: \t x = 1 ''', docformatter.format_code( '''\ def foo(): \t""" \tHello foo. \t""" \tif True: \t x = 1 '''))
def test_format_code_with_comments(self): self.assertEqual( r''' def foo(): """Hello foo.""" # My comment # My comment with escape \ 123 '''.lstrip(), docformatter.format_code(r''' def foo(): """ Hello foo. """ # My comment # My comment with escape \ 123 '''.lstrip()))
def test_format_code_with_parameters_list(self): self.assertEqual( '''\ def foo(): """Test. one - first two - second """ ''', docformatter.format_code( ('''\ def foo(): """Test one - first two - second """ ''')))
def test_format_code_with_trailing_whitespace(self): self.assertEqual( '''\ def foo(): """Hello foo and this is a docstring. More stuff. """ ''', docformatter.format_code(('''\ def foo(): """ Hello foo and this is a docstring.\t More stuff.\t """ ''')))
def create_file(fname, group, objs): source_files = [] for obj in objs: class_source = generate_class(obj) if tidy: class_source = autopep8.fix_code(class_source, options=autopep8.parse_args([ '--aggressive', '--aggressive', '--aggressive', '' ])) class_source = format_code(class_source) source_files.append(class_source) source = generate_group(group, source_files) with open("../pyidf/{}.py".format(fname), 'w') as f: f.write(source)
def test_format_code_with_empty_lines(self): self.assertEqual( '''\ def foo(): """Hello foo and this is a docstring. More stuff. """ ''', docformatter.format_code('''\ def foo(): """ Hello foo and this is a docstring. More stuff. """ '''))
def test_format_code_dominant_line_ending_style_preserved(self): input = '''\ def foo():\r """\r Hello\r foo. This is a docstring.\r """\r ''' self.assertEqual(docformatter.CRLF, docformatter.find_newline(input.splitlines(True))) self.assertEqual( '''\ def foo():\r """Hello foo.\r \r This is a docstring.\r """\r ''', docformatter.format_code(input))
def test_format_code_with_comments(self): self.assertEqual( r''' def foo(): """Hello foo.""" # My comment # My comment with escape \ 123 '''.lstrip(), docformatter.format_code( r''' def foo(): """ Hello foo. """ # My comment # My comment with escape \ 123 '''.lstrip()))
def test_format_code_with_empty_lines(self): self.assertEqual( '''\ def foo(): """Hello foo and this is a docstring. More stuff. """ ''', docformatter.format_code( '''\ def foo(): """ Hello foo and this is a docstring. More stuff. """ '''))
def test_format_code_with_trailing_whitespace(self): self.assertEqual( '''\ def foo(): """Hello foo and this is a docstring. More stuff. """ ''', docformatter.format_code( ('''\ def foo(): """ Hello foo and this is a docstring.\t More stuff.\t """ ''')))
def test_format_code_with_syntax_error_case_slash_r_slash_n(self): self.assertEqual('"""\r\n', docformatter.format_code('"""\r\n'))
q = multiprocessing.Queue() for fname in files: q.put((fname, files[fname], groups[fname])) ps = [multiprocessing.Process(target=worker, args=(q, i)) for i in range(num_worker_threads)] for p in ps: p.start() for p in ps: p.join() source_file = generate_idf(objs) if tidy: source_file = autopep8.fix_code( source_file, options=autopep8.parse_args(["--aggressive", "--aggressive", "--aggressive", ""]) ) source_file = format_code(source_file) with open("../pyidf/idf.py", "w") as f: f.write(source_file) helper_source = generate_helper(objs) if tidy: helper_source = autopep8.fix_code( helper_source, options=autopep8.parse_args(["--aggressive", "--aggressive", "--aggressive", ""]) ) helper_source = format_code(helper_source) with open("../pyidf/helper.py", "w") as f: f.write(helper_source) init_source = generate_init(version) if tidy:
def test_format_code_with_syntax_error(self): self.assertEqual('"""\n', docformatter.format_code('"""\n'))
def test_format_code_with_empty_string(self): self.assertEqual( '', docformatter.format_code(''))
from generator import generate_epw, generate_testclass from iddparser import IDDParser if __name__ == '__main__': parser = IDDParser() objs = parser.parse("epw.idd") source_file = generate_epw(objs) source_file = autopep8.fix_code( source_file, options=autopep8.parse_args(['--aggressive', '--aggressive', '--aggressive', ''])) source_file = format_code(source_file) with open("../pyepw/epw.py", 'w') as f: f.write(source_file) for obj in objs[:-1]: if not obj.is_list_object: source_file = generate_testclass(obj, objs) source_file = autopep8.fix_code( source_file, options=autopep8.parse_args(['--aggressive', '--aggressive', '--aggressive', ''])) source_file = format_code(source_file) with open("../tests/test_{}.py".format(obj.var_name), 'w') as f: f.write(source_file)
def test_format_code_with_should_skip_nested_triple_quotes(self): line = '''\ def foo(): 'Just a """foo""" string' ''' self.assertEqual(line, docformatter.format_code(line))