flake8-tidy-imports
flake8-tidy-imports copied to clipboard
Support alias import
Description
Hi Adam,
Can we support alias import something like this ?
# Don't
import datetime
# Do
import datetime as dt
In my projects, some of my modules have name conflict with 3rd-party package so I want to leverage alias imports to avoid name confusion.
Thanks.
I have thought of this too. I would like to have a setting to list "idiomatic imports", like:
idiomatic_imports =
import datetime as dt
import pandas as pd
from django.db import models
Then we could check that all imports of the matching modules use the "idiomatic" format, to maintain consistency and prevent conflicts like you say.
What do you think?
Totally agree that we should have this settings.
idiomatic_imports
also sounds good.
Do you have plan to work on it in near future ? If not, I may make contribution.
To simplify things, shouldn't we allow something like this ?
idiomatic_imports =
from django.db import models, utils
This is just an idea I’ve been thinking of, no intention to work on it immediately. If you’d like to start on a PR that would be great.
I agree with your proposal to support comma-separated imports. The idea of supporting all import syntax es here would allow us to parse them with the AST module, and we could support comments too.
I'm working on it. It's almost done. Writing docs is in progress then I think import_idioms
term sounds better and less verbose. What do you think ?
Btw, could you elaborate on this
we could support comments too.
I may work on it if have time.
I'm working on it. It's almost done. Writing docs is in progress then I think
import_idioms
term sounds better and less verbose. What do you think ?
Sounds good.
Btw, could you elaborate on this
Use ast.parse
to parse the configuration value after dedenting. It will parse the import statements and skip comments:
In [4]: idioms = """
...: # data science
...: import numpy as np
...: import pandas as pd
...: """
In [5]: import ast, textwrap
In [6]: ast.parse(textwrap.dedent(idioms))
Out[6]: <ast.Module at 0x110ccd660>
In [9]: print(ast.dump(Out[6], indent=2))
Module(
body=[
Import(
names=[
alias(name='numpy', asname='np')]),
Import(
names=[
alias(name='pandas', asname='pd')])],
type_ignores=[])
We'd want to check that all the contents of module.body
are ast.Import
or ast.ImportFrom
objects to prevent nonsense values.
Hi Adam,
sorry but I still didn't get your point
we could support comments too.
as far as I perceive, ast
module skip comment lines by default.
So why do we need to
prevent nonsense values.
?
as far as I perceive,
ast
module skip comment lines by default.
Yes exactly - I mean we would support them by using ast
, rather than writing our own custom parser.
So why do we need to
prevent nonsense values.
?
We should check all the parsed ast nodes are as expected, and not other ast nodes, to catch misconfiguration. For example if a user wrote:
import_idioms = "pandas as pd"
That would be parsed as an ast string constant node, so we'd want to raise an error for that.