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

Allow for varying import styles #92

Open
jshwi opened this issue Nov 29, 2021 · 7 comments
Open

Allow for varying import styles #92

jshwi opened this issue Nov 29, 2021 · 7 comments

Comments

@jshwi
Copy link
Contributor

jshwi commented Nov 29, 2021

Works

from setuptools import setup


setup(
    ...
)

Does not work

import setuptools


setuptools.setup(
    ...
)

Results in:

No setup() call found in setup.py
can not perform sync
@jshwi
Copy link
Contributor Author

jshwi commented Nov 30, 2021

@bryant-finney I might look at the options for this if you think it's worth perusing.

@bryant-finney
Copy link
Collaborator

@jshwi Ah interesting. Do you know if adding an import guard breaks it?

if __name__ == "__main__":
    setup (
        ...
    )

I'm also assuming being funky with the import would break it?

from setuptools import setup as silly
...
silly(
    ...
)

@jshwi
Copy link
Contributor Author

jshwi commented Dec 1, 2021

@bryant-finney For the first one, I haven't checked. Can you fill me on a bit on how that may help? Curious to know. The second one I find interesting, hadn't considered that yet. I'll look into it.

@nelsyeung
Copy link

@bryant-finney The first one works. The second one fails.

@bryant-finney
Copy link
Collaborator

bryant-finney commented Jun 6, 2022

@nelsyeung and @jshwi

I can see that since the ast module is used, the import guard is correctly handled, which is great.

I'm wondering if the ast module provides a way to test equality between nodes.

Or in other words, if one were using a shell and inspecting the setup.py script:

>>> import setup      # an example; we can't actually do this

>>> import setuptools 

>>> def get_setup_func() -> Callable:
...    for obj in setup.__dict__.values():
...        if obj is setuptools.setup:
...            return obj
...    raise ValueError(f"could not find {setuptools.setup} function")

>>> the_func = get_setup_func()

>>> ...     # process the function call

I feel like the ast module might provide some way or another to implement the get_setup_func() above in order to handle cases where from setuptools import setup as something_else is used. But that's just a hunch.

@jshwi
Copy link
Contributor Author

jshwi commented Jun 17, 2022

@bryant-finney I think you're right about the ast module, I haven't used it a great deal, this PR was a a bit of a WIP. I'm confident there's a cleaner way of doing this. I should be able to find the time to look into this.

@jshwi
Copy link
Contributor Author

jshwi commented Jun 29, 2022

The answer to the second problem might be astroid, as per the pylint README

Pylint can infer actual values from your code using its internal code representation (astroid). If your code is import logging as argparse, Pylint will know that argparse.error(...) is in fact a logging call and not an argparse call.

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

3 participants