Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dynamic builtins list based on Python version #13172

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@
from some import other as int
from some import input, exec
from directory import new as dir

# See: https://github.com/astral-sh/ruff/issues/13037
import sys

if sys.version_info < (3, 11):
from exceptiongroup import BaseExceptionGroup, ExceptionGroup
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use ruff_python_semantic::{
ModuleKind, ModuleSource, NodeId, ScopeId, ScopeKind, SemanticModel, SemanticModelFlags,
StarImport, SubmoduleImport,
};
use ruff_python_stdlib::builtins::{IPYTHON_BUILTINS, MAGIC_GLOBALS, PYTHON_BUILTINS};
use ruff_python_stdlib::builtins::{python_builtins, IPYTHON_BUILTINS, MAGIC_GLOBALS};
use ruff_python_trivia::CommentRanges;
use ruff_source_file::{Locator, OneIndexed, SourceRow};
use ruff_text_size::{Ranged, TextRange, TextSize};
Expand Down Expand Up @@ -1912,7 +1912,7 @@ impl<'a> Checker<'a> {
}

fn bind_builtins(&mut self) {
for builtin in PYTHON_BUILTINS
for builtin in python_builtins(self.settings.target_version.minor())
.iter()
.chain(MAGIC_GLOBALS.iter())
.chain(
Expand Down
8 changes: 6 additions & 2 deletions crates/ruff_linter/src/rules/flake8_builtins/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use crate::settings::types::PythonVersion;
use ruff_python_ast::PySourceType;
use ruff_python_stdlib::builtins::{is_ipython_builtin, is_python_builtin};

pub(super) fn shadows_builtin(
name: &str,
ignorelist: &[String],
source_type: PySourceType,
ignorelist: &[String],
python_version: PythonVersion,
) -> bool {
if is_python_builtin(name) || source_type.is_ipynb() && is_ipython_builtin(name) {
if is_python_builtin(name, python_version.minor())
|| source_type.is_ipynb() && is_ipython_builtin(name)
Comment on lines +11 to +12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the use case (when checking a Jupyter notebook) for distinguishing between Python builtins and IPython builtins? Since we're refactoring the is_python_builtin function anyway, should we just make it a function that also accepts the PySourceType as well as the Python minor version? (And get rid of the is_ipython_builtin function?)

It seems less error-prone: currently you have to remember to call both functions if you want to know if a symbol is a builtin symbol in a notebook file

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try this out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think the motivation here is that the builtins check is in uv_python_stdlib which doesn't depend on PySourceType.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. You could probably just do it with a boolean include_ipython_builtins parameter... but also this doesn't need to be done in this PR!

{
ignorelist.iter().all(|ignore| ignore != name)
} else {
false
Expand Down
15 changes: 15 additions & 0 deletions crates/ruff_linter/src/rules/flake8_builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod tests {

use crate::assert_messages;
use crate::registry::Rule;
use crate::settings::types::PythonVersion;
use crate::settings::LinterSettings;
use crate::test::test_path;

Expand Down Expand Up @@ -112,4 +113,18 @@ mod tests {
assert_messages!(snapshot, diagnostics);
Ok(())
}

#[test_case(Rule::BuiltinImportShadowing, Path::new("A004.py"))]
fn rules_py312(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}_py38", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path(
Path::new("flake8_builtins").join(path).as_path(),
&LinterSettings {
target_version: PythonVersion::Py38,
..LinterSettings::for_rule(rule_code)
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ impl Violation for BuiltinArgumentShadowing {
pub(crate) fn builtin_argument_shadowing(checker: &mut Checker, parameter: &Parameter) {
if shadows_builtin(
parameter.name.as_str(),
&checker.settings.flake8_builtins.builtins_ignorelist,
checker.source_type,
&checker.settings.flake8_builtins.builtins_ignorelist,
checker.settings.target_version,
) {
// Ignore `@override` and `@overload` decorated functions.
if checker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ pub(crate) fn builtin_attribute_shadowing(

if shadows_builtin(
name,
&checker.settings.flake8_builtins.builtins_ignorelist,
checker.source_type,
&checker.settings.flake8_builtins.builtins_ignorelist,
checker.settings.target_version,
) {
// Ignore explicit overrides.
if class_def.decorator_list.iter().any(|decorator| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ pub(crate) fn builtin_import_shadowing(checker: &mut Checker, alias: &Alias) {
let name = alias.asname.as_ref().unwrap_or(&alias.name);
if shadows_builtin(
name.as_str(),
&checker.settings.flake8_builtins.builtins_ignorelist,
checker.source_type,
&checker.settings.flake8_builtins.builtins_ignorelist,
checker.settings.target_version,
) {
checker.diagnostics.push(Diagnostic::new(
BuiltinImportShadowing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ pub(crate) fn builtin_lambda_argument_shadowing(checker: &mut Checker, lambda: &
let name = &param.parameter.name;
if shadows_builtin(
name.as_ref(),
&checker.settings.flake8_builtins.builtins_ignorelist,
checker.source_type,
&checker.settings.flake8_builtins.builtins_ignorelist,
checker.settings.target_version,
) {
checker.diagnostics.push(Diagnostic::new(
BuiltinLambdaArgumentShadowing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ impl Violation for BuiltinVariableShadowing {
pub(crate) fn builtin_variable_shadowing(checker: &mut Checker, name: &str, range: TextRange) {
if shadows_builtin(
name,
&checker.settings.flake8_builtins.builtins_ignorelist,
checker.source_type,
&checker.settings.flake8_builtins.builtins_ignorelist,
checker.settings.target_version,
) {
checker.diagnostics.push(Diagnostic::new(
BuiltinVariableShadowing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,20 @@ A004.py:5:30: A004 Import `dir` is shadowing a Python builtin
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
|
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,17 @@ A004.py:4:25: A004 Import `exec` is shadowing a Python builtin
| ^^^^ A004
5 | from directory import new as dir
|

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
|
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
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
|
Loading
Loading