"...git@developer.sourcefind.cn:chenpangpang/transformers.git" did not exist on "749f94e4607b5b59aef7c42a64ee0f959297f50f"
Unverified Commit 1144d336 authored by Sylvain Gugger's avatar Sylvain Gugger Committed by GitHub
Browse files

Copies and docstring styling (#15202)

* Style docstrings when making/checking copies

* Polish
parent 531336bb
......@@ -19,6 +19,7 @@ import os
import re
import black
from style_doc import style_docstrings_in_code
# All paths are set with the intent you should run this script from the root of the repo with the command
......@@ -130,6 +131,7 @@ def blackify(code):
if has_indent:
code = f"class Bla:\n{code}"
result = black.format_str(code, mode=black.FileMode([black.TargetVersion.PY35], line_length=119))
result, _ = style_docstrings_in_code(result)
return result[len("class Bla:\n") :] if has_indent else result
......
......@@ -367,21 +367,17 @@ def style_docstring(docstring, max_len):
return "\n".join(new_lines), "\n\n".join(black_errors)
def style_file_docstrings(code_file, max_len=119, check_only=False):
def style_docstrings_in_code(code, max_len=119):
"""
Style all docstrings in a given file.
Style all docstrings in some code.
Args:
code_file (`str` or `os.PathLike`): The file in which we want to style the docstring.
code (`str`): The code in which we want to style the docstrings.
max_len (`int`): The maximum number of characters per line.
check_only (`bool`, *optional*, defaults to `False`):
Whether to restyle file or just check if they should be restyled.
Returns:
`bool`: Whether or not the file was or should be restyled.
`Tuple[str, str]`: A tuple with the clean code and the black errors (if any)
"""
with open(code_file, "r", encoding="utf-8", newline="\n") as f:
code = f.read()
# fmt: off
splits = code.split('\"\"\"')
splits = [
......@@ -393,6 +389,27 @@ def style_file_docstrings(code_file, max_len=119, check_only=False):
clean_code = '\"\"\"'.join(splits)
# fmt: on
return clean_code, black_errors
def style_file_docstrings(code_file, max_len=119, check_only=False):
"""
Style all docstrings in a given file.
Args:
code_file (`str` or `os.PathLike`): The file in which we want to style the docstring.
max_len (`int`): The maximum number of characters per line.
check_only (`bool`, *optional*, defaults to `False`):
Whether to restyle file or just check if they should be restyled.
Returns:
`bool`: Whether or not the file was or should be restyled.
"""
with open(code_file, "r", encoding="utf-8", newline="\n") as f:
code = f.read()
clean_code, black_errors = style_docstrings_in_code(code, max_len=max_len)
diff = clean_code != code
if not check_only and diff:
print(f"Overwriting content of {code_file}.")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment