Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
1144d336
Unverified
Commit
1144d336
authored
Jan 18, 2022
by
Sylvain Gugger
Committed by
GitHub
Jan 18, 2022
Browse files
Copies and docstring styling (#15202)
* Style docstrings when making/checking copies * Polish
parent
531336bb
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
8 deletions
+27
-8
utils/check_copies.py
utils/check_copies.py
+2
-0
utils/style_doc.py
utils/style_doc.py
+25
-8
No files found.
utils/check_copies.py
View file @
1144d336
...
...
@@ -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
...
...
utils/style_doc.py
View file @
1144d336
...
...
@@ -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_fil
e
,
max_len
=
119
,
check_only
=
False
):
def
style_docstrings
_in_code
(
cod
e
,
max_len
=
119
):
"""
Style all docstrings in
a given fil
e.
Style all docstrings in
some cod
e.
Args:
code
_file
(`str`
or `os.PathLike`
): The
fil
e in which we want to style the docstring.
code (`str`): The
cod
e in which we want to style the docstring
s
.
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
}
."
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment