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

Support alias import #448

Open
hunghoang-saritasa opened this issue May 26, 2023 · 8 comments
Open

Support alias import #448

hunghoang-saritasa opened this issue May 26, 2023 · 8 comments

Comments

@hunghoang-saritasa
Copy link

hunghoang-saritasa commented May 26, 2023

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.

@adamchainz
Copy link
Owner

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?

@hunghoang-saritasa
Copy link
Author

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.

@hunghoang-saritasa
Copy link
Author

To simplify things, shouldn't we allow something like this ?

idiomatic_imports = 
     from django.db import models, utils

@adamchainz
Copy link
Owner

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.

@hunghoang-saritasa
Copy link
Author

hunghoang-saritasa commented May 31, 2023

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.

@adamchainz
Copy link
Owner

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.

@hunghoang-saritasa
Copy link
Author

hunghoang-saritasa commented Jun 5, 2023

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.

?

@adamchainz
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants