Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Do not emit import-error with Py2 / Py3 import blocks #769

Closed
PCManticore opened this issue Jan 7, 2016 · 6 comments
Closed

Do not emit import-error with Py2 / Py3 import blocks #769

PCManticore opened this issue Jan 7, 2016 · 6 comments
Labels
Milestone

Comments

@PCManticore
Copy link
Contributor

Given the following code, pylint emits an import-error when this gets analyzed with Python 3 for the ImportError block. We should be able to detect this and not warn about those imports.

try:
    from urllib import request as urllib_request
    from urllib import error as urllib_error
    from urllib.parse import urlparse
except ImportError:
    # python2
    import urllib2 as urllib_request
    import urllib2 as urllib_error
    from urlparse import urlparse
@smoser
Copy link

smoser commented Jan 7, 2016

just for rthe record, it complains in both python2 and python3 paths:

$ python3 -m pylint --errors-only m.py
No config file found, using default configuration
************* Module m
E:  9, 4: Unable to import 'urlparse' (import-error)

$ python2 -m pylint --errors-only m.py
No config file found, using default configuration
************* Module m
E:  2, 4: No name 'request' in module 'urllib' (no-name-in-module)
E:  3, 4: No name 'error' in module 'urllib' (no-name-in-module)
E:  4, 4: No name 'parse' in module 'urllib' (no-name-in-module)

@NeilGirdhar
Copy link

The import block is a bad way to do this: use six.moves.urllib instead.

@PCManticore
Copy link
Contributor Author

In some cases, having an extra dependency isn't ideal, which is the exact case here. Using six.moves.urllib doesn't solve the fact that we're wrongly emitting an error in this case.

@NeilGirdhar
Copy link

six is not really much of a "dependency". It is the most or second-most downloaded Python package. I think the better error to emit is something like "not using six when you should be" :)

@NeilGirdhar
Copy link

(you're probably right though.)

@PCManticore PCManticore added this to the 1.6.0 milestone May 9, 2016
@PCManticore
Copy link
Contributor Author

6 months later, but should be fixed now.

PCManticore added a commit that referenced this issue Jun 30, 2016
…cks by default.

Until now, we warned with these errors when a fallback import block (a TryExcept block
that contained imports for Python 2 and 3) was found, but this gets cumbersome when
trying to write compatible code. As such, we don't check these blocks by default,
but the analysis can be enforced by using the new ``--analyse-fallback-block`` flag.

Close #769.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants