Skip to content

[flake8-import-conventions] Add missing conventions from upstream (ICN001, ICN002)#21373

Merged
ntBre merged 17 commits intoastral-sh:mainfrom
danparizher:fix-21300
Feb 24, 2026
Merged

[flake8-import-conventions] Add missing conventions from upstream (ICN001, ICN002)#21373
ntBre merged 17 commits intoastral-sh:mainfrom
danparizher:fix-21300

Conversation

@danparizher
Copy link
Contributor

Summary

Adds missing import conventions from upstream flake8-import-conventions plugin. Specifically:

  • ICN001: Enforces plotly.graph_objectsgo and statsmodels.apism
  • ICN002: Bans geopandasgpd alias

Fixes #21300

Problem Analysis

Ruff's ICN001 (unconventional-import-alias) and ICN002 (banned-import-alias) rules were missing some conventions enforced by the upstream flake8-import-conventions plugin:

  1. ICN001 missing conventions:

    • plotly.graph_objects should be imported as go (IC008 in upstream)
    • statsmodels.api should be imported as sm (IC010 in upstream)
  2. ICN002 missing convention:

    • geopandas should not be imported as gpd (IC002 in upstream)

The root cause was that these conventions were not included in Ruff's default configuration. The CONVENTIONAL_ALIASES constant was missing the two ICN001 entries, and there was no default banned aliases configuration for ICN002.

Approach

  1. Added missing ICN001 conventions:

    • Added ("plotly.graph_objects", "go") and ("statsmodels.api", "sm") to CONVENTIONAL_ALIASES in settings.rs
    • Updated the default option string in options.rs to include these new aliases
  2. Added default banned aliases for ICN002:

    • Created default_banned_aliases() function returning geopandas["gpd"]
    • Updated Settings::default() to use default_banned_aliases()
    • Updated try_into_settings() in options.rs to use default_banned_aliases() when banned_aliases is None
    • Updated the default option string for banned_aliases in options.rs
  3. Added comprehensive tests:

    • Created missing_conventions.py test fixture with cases for all three new conventions
    • Added missing_conventions test in mod.rs to verify the new conventions work correctly

Test Plan

Added new snapshot test missing_conventions that verifies:

  • ICN001 correctly flags plotly.graph_objects without alias and requires go
  • ICN001 correctly flags statsmodels.api without alias and requires sm
  • ICN002 correctly flags geopandas as gpd as banned
  • All existing tests continue to pass (10/10 tests passing)

The fix has been manually verified to match the behavior of upstream flake8-import-conventions.

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 11, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+3 -0 violations, +0 -0 fixes in 1 projects; 55 projects unchanged)

ibis-project/ibis (+3 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview

+ ibis/backends/bigquery/converter.py:9:9: ICN002 `geopandas` should not be imported as `gpd`
+ ibis/backends/postgres/converter.py:11:9: ICN002 `geopandas` should not be imported as `gpd`
+ ibis/formats/pandas.py:184:9: ICN002 `geopandas` should not be imported as `gpd`

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
ICN002 3 3 0 0 0

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks! This looks good, just one suggestion about where to put the new tests

@ntBre ntBre added the rule Implementing or modifying a lint rule label Nov 11, 2025
@danparizher danparizher requested a review from ntBre November 11, 2025 22:20
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

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.

@danparizher danparizher requested a review from ntBre November 14, 2025 02:19
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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

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

@danparizher danparizher requested a review from ntBre November 19, 2025 23:31
@ntBre ntBre added the preview Related to preview mode features label Dec 3, 2025
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

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.

Comment on lines +1676 to +1680
// Merge preview aliases if preview mode is enabled
if preview.is_enabled() {
aliases.extend(flake8_import_conventions::settings::preview_aliases());
}

Copy link
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines +1695 to +1697
if preview.is_enabled() {
banned_aliases.extend(flake8_import_conventions::settings::preview_banned_aliases());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above, we should only do this in the or_default case.

@danparizher danparizher requested a review from ntBre January 26, 2026 05:26
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thank you!

@ntBre
Copy link
Contributor

ntBre commented Feb 24, 2026

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.

@ntBre ntBre merged commit 46f71fb into astral-sh:main Feb 24, 2026
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ICN rules miss some conventions from flake8-import-conventions

2 participants