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

optional import of subpackages #4361

Closed
svenkreiss opened this issue Apr 15, 2021 · 6 comments
Closed

optional import of subpackages #4361

svenkreiss opened this issue Apr 15, 2021 · 6 comments
Labels
Duplicate 🐫 Duplicate of an already existing issue Enhancement ✨ Improvement to a component False Positive 🦟 A message is emitted but nothing is wrong with the code

Comments

@svenkreiss
Copy link

svenkreiss commented Apr 15, 2021

Steps to reproduce

Pylint is happy but scipy.ndimage is not imported:

try:
    import scipy
except ImportError:
    scipy = None

Need the import below to make scipy.ndimage available, but pylint complains:

try:
    import scipy.ndimage  # <--- need to import ndimage like this otherwise not imported
except ImportError:
    scipy = None

Pylint: C0103: Constant name "scipy" doesn't conform to UPPER_CASE naming style (invalid-name)

Expected behavior (updated)

When I run pylint, I want the output to be:

Context

I am making scipy an optional dependency in OpenPifPaf: openpifpaf/openpifpaf#390

@dbaty
Copy link
Contributor

dbaty commented Apr 19, 2021

In the second form, you define two different symbols: scipy.ndimage if scipy is installed; scipy otherwise. How do you cope with this discrepancy in the rest of the module?

I would suggest doing something like this:

try:
    import scipy.ndimage as scipy_ndimage
except ImportError:
    scipy_ndimage = None

def foo():
    if scipy_ndimage:
         return scipy_ndimage.frobulate()
    return 2  # good approximation of the frobulation algorithm without scipy

Alternatively:

try:
    import scipy
    import scipy.ndimage
except ImportError:
    scipy = None

def foo():
    if scipy:
         return scipy.ndimage.frobulate()
    return 2  # good approximation of the frobulation algorithm without scipy

(It's untested and I hope it's not too far from something that would work!)

@hippo91
Copy link
Contributor

hippo91 commented May 13, 2021

@svenkreiss thanks for you report.
@dbaty thanks for your research and explanation.
@svenkreiss are you ok with @dbaty's advice?
If so i will close the issue.

@svenkreiss
Copy link
Author

@dbaty I didn't follow your two different symbols argument. When I import scipy.ndimage then that is not just one thing. After the import, I do have access to scipy as well:
image

I'd argue that my original code is valid and should not fail pylint.

@Pierre-Sassoulas
Copy link
Member

I don't understand this issue, what would you want pylint to do exactly ? Not raise a C0103: Constant name "scipy" doesn't conform to UPPER_CASE naming style on scipy = None ? There is an issue template for a reason.

@svenkreiss
Copy link
Author

Yes, I don't want pylint to raise this C0103.

I have updated the report with the relevant part from the issue template (apologies @Pierre-Sassoulas ).

@Pierre-Sassoulas Pierre-Sassoulas added Enhancement ✨ Improvement to a component False Positive 🦟 A message is emitted but nothing is wrong with the code labels May 17, 2021
@Pierre-Sassoulas
Copy link
Member

Thank you for the clarification @svenkreiss, I'm going to close as duplicate of #3585 .

@Pierre-Sassoulas Pierre-Sassoulas added the Duplicate 🐫 Duplicate of an already existing issue label Jul 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate 🐫 Duplicate of an already existing issue Enhancement ✨ Improvement to a component False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

No branches or pull requests

4 participants