Skip to content

Presence of otherwise absence modules breaks conditional imports #158

Closed

Description

@jaraco

Consider this conditional import from CherryPy (cherrypy._cpcompat):

try:
    # Python 3
    from urllib.parse import unquote as parse_unquote

    def unquote_qs(atom, encoding, errors='strict'):
        return parse_unquote(
            atom.replace('+', ' '),
            encoding=encoding,
            errors=errors)
except ImportError:
    # Python 2
    from urllib import unquote as parse_unquote

    def unquote_qs(atom, encoding, errors='strict'):
        return parse_unquote(atom.replace('+', ' ')).decode(encoding, errors)

As CherryPy was a pioneer in supporting Python 3 on a non-trivial library (and the first web framework), it has had this compatibility library since before six and future were mature.

However, the presence of future breaks this library. If an application pulls in future on Python 2, the CherryPy detection for urllib.parse will erroneously select the Python 3 invocation, but on Python 2, even with future installed, the unquote function does not accept the encoding parameter and will fail with a TypeError when invoked. This behavior has recently worked its way into one of our applications and is causing the test suite to fail. If it works into production, it could cause our apps to fail.

I see several possible ways to address the issue:

  1. python-future accepts this as a bug (exposing a Python 3 function without the full Python 3 interface) and corrects it, such that even if CherryPy selects that implementation, it will work on Python 2 as on Python 3.
  2. We scour the dependencies and exclude any that are pulling python-future into the application. This technique would pin our application to old code, prevent forward compatibility, and exclude the possibility of using python-future.
  3. Users submit bug reports to each and every library that uses conditional imports for feature detection, including CherryPy, patch those libraries, re-release them, and update the applications that depend on them. This approach would be tedious and drawn out and would prevent use of older versions of these libraries.

There are probably others as well.

I'd like to get a read from the python-future maintainers. Is this a known issue with the technique (I didn't see any bugs reporting something similar)? What do you recommend be done?

Activity

  1. jaraco commented on Jul 23, 2015

    @jaraco
    Author

    After tracking the library that was triggering this behavior, I found it was invoking this:

    from future import standard_library
    standard_library.install_aliases()
    

    I suspect that call is implicated, and while it's an optional invocation, it came as the result of running futurize on the codebase, so would be likely to be called by other libraries employing the use of future.

  2. edschofield commented on Jul 24, 2015

    @edschofield
    Contributor

    Jason, thanks very much for highlighting this. I'm going to choose (1): to accept this as a bug. I'm working on a fix now.

  3. edschofield commented on Jul 25, 2015

    @edschofield
    Contributor

    I've released a new version (v0.15.0) that should resolve this bug. Please let me know if it works for you!

  4. jaraco commented on Jul 25, 2015

    @jaraco
    Author

    What service! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions