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

Custom auto-imports #11141

Open
MartinBernstorff opened this issue Apr 25, 2024 · 2 comments
Open

Custom auto-imports #11141

MartinBernstorff opened this issue Apr 25, 2024 · 2 comments
Labels
rule Implementing or modifying a lint rule

Comments

@MartinBernstorff
Copy link

I have a handful of imports that are always under the same alias, e.g.:

pn -> import plotnine as pn
pl -> import polars as pl
pd -> import pandas as pd
Iter -> from iterpy import Iter

I would love if Ruff could detect a missing import and, if it matches any of a set of patterns specified in settings, auto-add the corresponding import.

Is this possible with the current feature set? Or could it be added? Love to hear what you think!

@bersbersbers
Copy link
Contributor

ruff --show-settings shows me

linter.flake8_import_conventions.aliases = {
        altair = alt,
        holoviews = hv,
        matplotlib = mpl,
        matplotlib.pyplot = plt,
        networkx = nx,
        numpy = np,
        pandas = pd,
        panel = pn,
        plotly.express = px,
        polars = pl,
        pyarrow = pa,
        seaborn = sns,
        tensorflow = tf,
        tkinter = tk,
}

Does that help?

@MartinBernstorff
Copy link
Author

This is pretty nice, thanks! It supports part of, but not all of the workflow. What I would love to be able to do is:

  1. Write some code, e.g. pl.DataFrame({"col1": [1,2,3})
  2. Ruff detects a missing import for pl.
  3. Because pl is in the list of "known imports", it is automatically added to the top of the file as import polars as pl

VSCode has some of this functionality, but AFAICT, you cannot add your own rules: https://dev.to/krisplatis/auto-add-missing-imports-on-file-save-in-vs-code-1b89

AFAICT, flake8_import_conventions does not automatically add the import.

@AlexWaygood AlexWaygood added the rule Implementing or modifying a lint rule label Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

3 participants