-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use dynamic builtins list based on Python version
- Loading branch information
1 parent
34b4732
commit 8db8fb2
Showing
17 changed files
with
565 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import subprocess | ||
import json | ||
|
||
def enumerate_builtins_by_version(python_version): | ||
cmd = [ | ||
"uv", "run", "--python", f"{python_version}", "--no-project", "python", | ||
"-c", | ||
""" | ||
import sys | ||
import builtins | ||
import json | ||
python_version = f"{sys.version_info.major}.{sys.version_info.minor}" | ||
builtin_list = sorted(dir(builtins)) | ||
print(json.dumps(builtin_list)) | ||
""" | ||
] | ||
result = subprocess.run(cmd, check=True, capture_output=True, text=True) | ||
return json.loads(result.stdout) | ||
|
||
if __name__ == "__main__": | ||
python_versions = ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"] | ||
all_results = {} | ||
for version in python_versions: | ||
print(f"\nRunning for Python {version}") | ||
result = enumerate_builtins_by_version(version) | ||
all_results[version] = result | ||
|
||
print("\nRust code for listing builtins:") | ||
print("match (major, minor) {") | ||
for version, builtins in all_results.items(): | ||
major, minor = version.split('.') | ||
print(f" ({major}, {minor}) => &[") | ||
for builtin in builtins: | ||
print(f' "{builtin}",') | ||
print(" ],") | ||
print(" _ => &[],") | ||
print("}") | ||
|
||
print("\nRust code for checking if a string is a builtin:") | ||
print("matches!(minor, {") | ||
|
||
# Collect all unique builtins across versions | ||
all_builtins = set() | ||
for builtins in all_results.values(): | ||
all_builtins.update(builtins) | ||
|
||
print("pub fn is_known_standard_library(minor_version: u8, module: &str) -> bool {") | ||
print(" matches!(") | ||
print(" (minor_version, module),") | ||
print(" (") | ||
|
||
# Print the builtins that are common to all versions | ||
common_builtins = set.intersection(*map(set, all_results.values())) | ||
for builtin in sorted(common_builtins): | ||
print(f' (_, "{builtin}"),') | ||
|
||
# Print version-specific builtins | ||
for version, builtins in all_results.items(): | ||
_, minor = version.split('.') | ||
version_specific = set(builtins) - common_builtins | ||
if version_specific: | ||
for builtin in sorted(version_specific): | ||
print(f' ({minor}, "{builtin}"),') | ||
|
||
print(" )") | ||
print(" )") | ||
print("}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...e8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A004_A004.py_py312.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_builtins/mod.rs | ||
--- | ||
A004.py:1:16: A004 Import `sum` is shadowing a Python builtin | ||
| | ||
1 | import some as sum | ||
| ^^^ A004 | ||
2 | import float | ||
3 | from some import other as int | ||
| | ||
|
||
A004.py:2:8: A004 Import `float` is shadowing a Python builtin | ||
| | ||
1 | import some as sum | ||
2 | import float | ||
| ^^^^^ A004 | ||
3 | from some import other as int | ||
4 | from some import input, exec | ||
| | ||
|
||
A004.py:3:27: A004 Import `int` is shadowing a Python builtin | ||
| | ||
1 | import some as sum | ||
2 | import float | ||
3 | from some import other as int | ||
| ^^^ A004 | ||
4 | from some import input, exec | ||
5 | from directory import new as dir | ||
| | ||
|
||
A004.py:4:18: A004 Import `input` is shadowing a Python builtin | ||
| | ||
2 | import float | ||
3 | from some import other as int | ||
4 | from some import input, exec | ||
| ^^^^^ A004 | ||
5 | from directory import new as dir | ||
| | ||
|
||
A004.py:4:25: A004 Import `exec` is shadowing a Python builtin | ||
| | ||
2 | import float | ||
3 | from some import other as int | ||
4 | from some import input, exec | ||
| ^^^^ A004 | ||
5 | from directory import new as dir | ||
| | ||
|
||
A004.py:5:30: A004 Import `dir` is shadowing a Python builtin | ||
| | ||
3 | from some import other as int | ||
4 | from some import input, exec | ||
5 | from directory import new as dir | ||
| ^^^ A004 | ||
6 | | ||
7 | # See: https://github.com/astral-sh/ruff/issues/13037 | ||
| | ||
|
||
A004.py:11:32: A004 Import `BaseExceptionGroup` is shadowing a Python builtin | ||
| | ||
10 | if sys.version_info < (3, 11): | ||
11 | from exceptiongroup import BaseExceptionGroup, ExceptionGroup | ||
| ^^^^^^^^^^^^^^^^^^ A004 | ||
| | ||
|
||
A004.py:11:52: A004 Import `ExceptionGroup` is shadowing a Python builtin | ||
| | ||
10 | if sys.version_info < (3, 11): | ||
11 | from exceptiongroup import BaseExceptionGroup, ExceptionGroup | ||
| ^^^^^^^^^^^^^^ A004 | ||
| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.