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
OpenDAS
vllm_cscc
Commits
2807271c
Unverified
Commit
2807271c
authored
May 24, 2025
by
Aaron Pham
Committed by
GitHub
May 24, 2025
Browse files
[CI] enforce import regex instead of re (#18665)
Signed-off-by:
Aaron Pham
<
contact@aarnphm.xyz
>
parent
b9018a3f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
90 additions
and
0 deletions
+90
-0
.pre-commit-config.yaml
.pre-commit-config.yaml
+7
-0
tools/enforce_regex_import.py
tools/enforce_regex_import.py
+83
-0
No files found.
.pre-commit-config.yaml
View file @
2807271c
...
@@ -128,6 +128,13 @@ repos:
...
@@ -128,6 +128,13 @@ repos:
name
:
Update Dockerfile dependency graph
name
:
Update Dockerfile dependency graph
entry
:
tools/update-dockerfile-graph.sh
entry
:
tools/update-dockerfile-graph.sh
language
:
script
language
:
script
-
id
:
enforce-import-regex-instead-of-re
name
:
Enforce import regex as re
entry
:
python tools/enforce_regex_import.py
language
:
python
types
:
[
python
]
pass_filenames
:
false
additional_dependencies
:
[
regex
]
# forbid directly import triton
# forbid directly import triton
-
id
:
forbid-direct-triton-import
-
id
:
forbid-direct-triton-import
name
:
"
Forbid
direct
'import
triton'"
name
:
"
Forbid
direct
'import
triton'"
...
...
tools/enforce_regex_import.py
0 → 100644
View file @
2807271c
# SPDX-License-Identifier: Apache-2.0
from
__future__
import
annotations
import
subprocess
from
pathlib
import
Path
import
regex
as
re
FORBIDDEN_PATTERNS
=
re
.
compile
(
r
'^\s*(?:import\s+re(?:$|\s|,)|from\s+re\s+import)'
)
ALLOWED_PATTERNS
=
[
re
.
compile
(
r
'^\s*import\s+regex\s+as\s+re\s*$'
),
re
.
compile
(
r
'^\s*import\s+regex\s*$'
),
]
def
get_staged_python_files
()
->
list
[
str
]:
try
:
result
=
subprocess
.
run
(
[
'git'
,
'diff'
,
'--cached'
,
'--name-only'
,
'--diff-filter=AM'
],
capture_output
=
True
,
text
=
True
,
check
=
True
)
files
=
result
.
stdout
.
strip
().
split
(
'
\n
'
)
if
result
.
stdout
.
strip
()
else
[]
return
[
f
for
f
in
files
if
f
.
endswith
(
'.py'
)]
except
subprocess
.
CalledProcessError
:
return
[]
def
is_forbidden_import
(
line
:
str
)
->
bool
:
line
=
line
.
strip
()
return
bool
(
FORBIDDEN_PATTERNS
.
match
(
line
)
and
not
any
(
pattern
.
match
(
line
)
for
pattern
in
ALLOWED_PATTERNS
))
def
check_file
(
filepath
:
str
)
->
list
[
tuple
[
int
,
str
]]:
violations
=
[]
try
:
with
open
(
filepath
,
encoding
=
'utf-8'
)
as
f
:
for
line_num
,
line
in
enumerate
(
f
,
1
):
if
is_forbidden_import
(
line
):
violations
.
append
((
line_num
,
line
.
strip
()))
except
(
OSError
,
UnicodeDecodeError
):
pass
return
violations
def
main
()
->
int
:
files
=
get_staged_python_files
()
if
not
files
:
return
0
total_violations
=
0
for
filepath
in
files
:
if
not
Path
(
filepath
).
exists
():
continue
violations
=
check_file
(
filepath
)
if
violations
:
print
(
f
"
\n
❌
{
filepath
}
:"
)
for
line_num
,
line
in
violations
:
print
(
f
" Line
{
line_num
}
:
{
line
}
"
)
total_violations
+=
1
if
total_violations
>
0
:
print
(
f
"
\n
💡 Found
{
total_violations
}
violation(s)."
)
print
(
"❌ Please replace 'import re' with 'import regex as re'"
)
print
(
" Also replace 'from re import ...' with 'from regex import ...'"
)
# noqa: E501
print
(
"✅ Allowed imports:"
)
print
(
" - import regex as re"
)
print
(
" - import regex"
)
# noqa: E501
return
1
return
0
if
__name__
==
"__main__"
:
raise
SystemExit
(
main
())
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