Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
56b35ce3
Unverified
Commit
56b35ce3
authored
May 25, 2022
by
Sylvain Gugger
Committed by
GitHub
May 25, 2022
Browse files
Make check_init script more robust and clean inits (#17408)
parent
bd908e9b
Changes
61
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
1 deletion
+11
-1
utils/check_inits.py
utils/check_inits.py
+11
-1
No files found.
utils/check_inits.py
View file @
56b35ce3
...
...
@@ -25,6 +25,8 @@ PATH_TO_TRANSFORMERS = "src/transformers"
# Matches is_xxx_available()
_re_backend
=
re
.
compile
(
r
"is\_([a-z_]*)_available()"
)
# Catches a one-line _import_struct = {xxx}
_re_one_line_import_struct
=
re
.
compile
(
r
"^_import_structure\s+=\s+\{([^\}]+)\}"
)
# Catches a line with a key-values pattern: "bla": ["foo", "bar"]
_re_import_struct_key_value
=
re
.
compile
(
r
'\s+"\S*":\s+\[([^\]]*)\]'
)
# Catches a line if not is_foo_available
...
...
@@ -74,6 +76,14 @@ def parse_init(init_file):
objects
=
[]
while
not
lines
[
line_index
].
startswith
(
"if TYPE_CHECKING"
)
and
find_backend
(
lines
[
line_index
])
is
None
:
line
=
lines
[
line_index
]
# If we have everything on a single line, let's deal with it.
if
_re_one_line_import_struct
.
search
(
line
):
content
=
_re_one_line_import_struct
.
search
(
line
).
groups
()[
0
]
imports
=
re
.
findall
(
"\[([^\]]+)\]"
,
content
)
for
imp
in
imports
:
objects
.
extend
([
obj
[
1
:
-
1
]
for
obj
in
imp
.
split
(
", "
)])
line_index
+=
1
continue
single_line_import_search
=
_re_import_struct_key_value
.
search
(
line
)
if
single_line_import_search
is
not
None
:
imports
=
[
obj
[
1
:
-
1
]
for
obj
in
single_line_import_search
.
groups
()[
0
].
split
(
", "
)
if
len
(
obj
)
>
0
]
...
...
@@ -144,7 +154,7 @@ def parse_init(init_file):
type_hint_objects
=
{
"none"
:
objects
}
# Let's continue with backend-specific objects
while
line_index
<
len
(
lines
):
# If the line is an if is_backe
m
d_available, we grab all objects associated.
# If the line is an if is_backe
n
d_available, we grab all objects associated.
backend
=
find_backend
(
lines
[
line_index
])
# Check if the backend declaration is inside a try block:
if
_re_try
.
search
(
lines
[
line_index
-
1
])
is
None
:
...
...
Prev
1
2
3
4
Next
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