[flake8-import-conventions] Add missing conventions from upstream (ICN001, ICN002)#21373
[flake8-import-conventions] Add missing conventions from upstream (ICN001, ICN002)#21373ntBre merged 17 commits intoastral-sh:mainfrom
flake8-import-conventions] Add missing conventions from upstream (ICN001, ICN002)#21373Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| ICN002 | 3 | 3 | 0 | 0 | 0 |
ntBre
left a comment
There was a problem hiding this comment.
Thanks! This looks good, just one suggestion about where to put the new tests
crates/ruff_linter/resources/test/fixtures/flake8_import_conventions/missing_conventions.py
Outdated
Show resolved
Hide resolved
ntBre
left a comment
There was a problem hiding this comment.
On second thought, could we make this a preview change? Initially I assumed this was an unintentional omission from the original PR adding the rules (#1098), but I see now that these were changed upstream after our rule was added (joaopalmeiro/flake8-import-conventions#3, joaopalmeiro/flake8-import-conventions#4). These issues were opened upstream before our rule but closed after. And there are a few hits in the ecosystem report for geopandas too.
crates/ruff_linter/src/rules/flake8_import_conventions/rules/banned_import_alias.rs
Outdated
Show resolved
Hide resolved
| impl Settings { | ||
| /// Merge preview aliases and banned aliases into the settings if preview mode is enabled. | ||
| #[must_use] | ||
| pub fn with_preview(mut self, preview_enabled: bool) -> Self { |
There was a problem hiding this comment.
I think the way we usually handle this is by defining a method that takes a preview argument instead of impl Default (which can't take arguments). This is kind of the opposite since it was a stabilization PR, but you can see an example here: https://github.com/astral-sh/ruff/pull/12838/files#diff-0e4daa1a461406d9fd7af87b85ff4ecc89bd61f748d2b7cb24b88b25c7d5ba59
Possibly a better example: #15951
ntBre
left a comment
There was a problem hiding this comment.
Thanks, this is looking good. I fixed a few nits, reverting some unrelated changes and tweaking the test setup, but I also wanted to get your thoughts on the try_into_settings changes. See my inline comments for that.
crates/ruff_workspace/src/options.rs
Outdated
| // Merge preview aliases if preview mode is enabled | ||
| if preview.is_enabled() { | ||
| aliases.extend(flake8_import_conventions::settings::preview_aliases()); | ||
| } | ||
|
|
There was a problem hiding this comment.
I don't think we should set this here. If I understand correctly, this will extend the list of aliases when preview is enabled even if the user provided a set of aliases. We only want to extend the default set in the case where self.aliases is None above. I think it would make sense for the default_aliases helper function to take the preview argument and apply it there.
crates/ruff_workspace/src/options.rs
Outdated
| if preview.is_enabled() { | ||
| banned_aliases.extend(flake8_import_conventions::settings::preview_banned_aliases()); | ||
| } |
There was a problem hiding this comment.
Same as above, we should only do this in the or_default case.
|
Not sure why the ecosystem check dropped a diagnostic after the last push, I'm still seeing 4 locally. I think I'll go ahead and land this anyway once CI finishes again. |
Summary
Adds missing import conventions from upstream flake8-import-conventions plugin. Specifically:
ICN001: Enforcesplotly.graph_objects→goandstatsmodels.api→smICN002: Bansgeopandas→gpdaliasFixes #21300
Problem Analysis
Ruff's
ICN001(unconventional-import-alias) andICN002(banned-import-alias) rules were missing some conventions enforced by the upstream flake8-import-conventions plugin:ICN001 missing conventions:
plotly.graph_objectsshould be imported asgo(IC008 in upstream)statsmodels.apishould be imported assm(IC010 in upstream)ICN002 missing convention:
geopandasshould not be imported asgpd(IC002 in upstream)The root cause was that these conventions were not included in Ruff's default configuration. The
CONVENTIONAL_ALIASESconstant was missing the two ICN001 entries, and there was no default banned aliases configuration for ICN002.Approach
Added missing ICN001 conventions:
("plotly.graph_objects", "go")and("statsmodels.api", "sm")toCONVENTIONAL_ALIASESinsettings.rsoptions.rsto include these new aliasesAdded default banned aliases for ICN002:
default_banned_aliases()function returninggeopandas→["gpd"]Settings::default()to usedefault_banned_aliases()try_into_settings()inoptions.rsto usedefault_banned_aliases()whenbanned_aliasesis Nonebanned_aliasesinoptions.rsAdded comprehensive tests:
missing_conventions.pytest fixture with cases for all three new conventionsmissing_conventionstest inmod.rsto verify the new conventions work correctlyTest Plan
Added new snapshot test
missing_conventionsthat verifies:plotly.graph_objectswithout alias and requiresgostatsmodels.apiwithout alias and requiressmgeopandas as gpdas bannedThe fix has been manually verified to match the behavior of upstream flake8-import-conventions.