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

[flake8-import-conventions] Syntax check aliases supplied in configuration for unconventional-import-alias (ICN001) #14477

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dylwil3
Copy link
Collaborator

@dylwil3 dylwil3 commented Nov 20, 2024

This PR introduces a validation step to the deserialization of the configuration for unconventional-import-alias (ICN001). We verify that the import module and alias supplied have valid syntax in order to avoid a panic if these aliases are added in during a fix.

Closes #14439

@dylwil3
Copy link
Collaborator Author

dylwil3 commented Nov 20, 2024

Two notes:

  • Sorry for bloating the workspace crate with serde and especially with serde_json (since I only use the latter for a test)... let me know if you'd like me to work around that
  • I don't add a deserialization check for "banned aliases" or "banned from" since a fix would never add those to the source code

Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@MichaReiser MichaReiser added the configuration Related to settings and configuration label Nov 20, 2024
@@ -42,6 +43,7 @@ serde = { workspace = true }
shellexpand = { workspace = true }
strum = { workspace = true }
toml = { workspace = true }
serde_json.workspace = true
Copy link
Member

Choose a reason for hiding this comment

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

This should be a dev-dependency if it is only used in tests

@MichaReiser MichaReiser added bug Something isn't working and removed configuration Related to settings and configuration labels Nov 20, 2024
Comment on lines +3443 to +3449
#[test]
fn flake8_import_conventions_validate_aliases() {
let json_options = r#"{"aliases": {"a.b":"a.b"}}"#;
let result: Result<Flake8ImportConventionsOptions, _> = serde_json::from_str(json_options);
assert!(result.unwrap_err().to_string().contains("Module must be valid identifier separated by single periods and alias must be valid identifier"));
}

Copy link
Member

Choose a reason for hiding this comment

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

I'm leaning towards making this a ruff cli integration test. It then also demonstrates how the error message is displayed to the users.

See

fn top_level_options() -> Result<()> {

Comment on lines +1387 to +1394
if module.is_empty()
|| module.split('.').any(|part| !is_identifier(part))
|| !is_identifier(alias)
{
return Err(de::Error::custom(
"Module must be valid identifier separated by single periods and alias must be valid identifier",
));
}
Copy link
Member

Choose a reason for hiding this comment

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

I suggest that we use two dedicated errors: One for alias and one for module.

@AlexWaygood
Copy link
Member

Let's try to get this into ruff 0.8 if we can — while it is a bugfix, it might still be breaking for some users who are currently "using the rule incorrectly"

@AlexWaygood AlexWaygood added this to the v0.8 milestone Nov 20, 2024
@AlexWaygood AlexWaygood changed the base branch from main to ruff-0.8 November 20, 2024 08:49
@AlexWaygood AlexWaygood changed the base branch from ruff-0.8 to main November 20, 2024 08:49
@AlexWaygood
Copy link
Member

AlexWaygood commented Nov 20, 2024

Sorry — I optimistically tried to change the target branch, but it messed up the diff, so I swiftly reverted that back 😅

It's probably fine if this goes straight into main, though, as long as we merge it before the 0.8 release goes out. I highly doubt we'll be cutting another patch release before 0.8, since everything is roughly on schedule for the minor release

@AlexWaygood AlexWaygood added the breaking Breaking API change label Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Breaking API change bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Possible error when trying to fix with ICN001
3 participants