Skip to content

Add compat entry for urllib3 import due to new requests release#5185

Closed
audiolion wants to merge 1 commit intoencode:masterfrom
audiolion:requests-updated-compat
Closed

Add compat entry for urllib3 import due to new requests release#5185
audiolion wants to merge 1 commit intoencode:masterfrom
audiolion:requests-updated-compat

Conversation

@audiolion
Copy link
Copy Markdown

Description

Fixes #5184 . The new version of requests breaks APITestCase's client library because it removed its vendored packages.

@audiolion
Copy link
Copy Markdown
Author

The change was reverted in 2.16.1

@audiolion audiolion closed this May 27, 2017
@michaelhelmick
Copy link
Copy Markdown

In 2.18.4, I'm getting this error again with the simple example of

from django.urls import reverse
from rest_framework.test import APITestCase


class AccountTests(APITestCase):
    def test_create_account(self):
        """Ensure we can create a new account object."""
        url = reverse('accounts-list')
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, 200)
Traceback (most recent call last):
  File "./manage.py", line 13, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/mikehelmick/.virtualenvs/myproject/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/mikehelmick/.virtualenvs/myproject/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/mikehelmick/.virtualenvs/myproject/lib/python2.7/site-packages/django/core/management/commands/test.py", line 29, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/Users/mikehelmick/.virtualenvs/myproject/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/mikehelmick/.virtualenvs/myproject/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Users/mikehelmick/.virtualenvs/myproject/lib/python2.7/site-packages/django/core/management/commands/test.py", line 62, in handle
    failures = test_runner.run_tests(test_labels)
  File "/Users/mikehelmick/.virtualenvs/myproject/lib/python2.7/site-packages/django/test/runner.py", line 600, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
  File "/Users/mikehelmick/.virtualenvs/myproject/lib/python2.7/site-packages/django/test/runner.py", line 484, in build_suite
    tests = self.test_loader.loadTestsFromName(label)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 91, in loadTestsFromName
    module = __import__('.'.join(parts_copy))
  File "/Users/mikehelmick/Code/myproject/myproject/account/tests/test_viewsets.py", line 3, in <module>
    from rest_framework.test import APITestCase
  File "/Users/mikehelmick/.virtualenvs/myproject/lib/python2.7/site-packages/rest_framework/test.py", line 30, in <module>
    class HeaderDict(requests.packages.urllib3._collections.HTTPHeaderDict):
AttributeError: 'module' object has no attribute 'urllib3'

@xordoquy
Copy link
Copy Markdown
Contributor

xordoquy commented Nov 28, 2017

@michaelhelmick we don't support 2.18.4 any longer.

Edit: coming to think 2.18.4 was referring to request version and not DRF one right ?

@michaelhelmick
Copy link
Copy Markdown

Correct :D haha, in the email (before the edit) I assumed you were talking DRF.

I'm referring to requests==2.18.4 which is the latest version as of this writing

@carltongibson
Copy link
Copy Markdown
Collaborator

@michaelhelmick So, does this patch fix it for you? Can we just re-open or do we need a fresh PR? (Fancy providing that if so... 🙂)

@audiolion
Copy link
Copy Markdown
Author

if requests broke it again this should still work as a fix

@michaelhelmick
Copy link
Copy Markdown

michaelhelmick commented Nov 28, 2017

This patch does work.

EDIT: Although, the lines have changed slightly; but it works. :)

@audiolion audiolion reopened this Nov 28, 2017
@rpkilby
Copy link
Copy Markdown
Contributor

rpkilby commented Nov 29, 2017

As far as I can tell, this should still work.

https://github.com/requests/requests/blob/v2.18.4/requests/packages.py

@michaelhelmick
Copy link
Copy Markdown

michaelhelmick commented Nov 29, 2017 via email

@rpkilby
Copy link
Copy Markdown
Contributor

rpkilby commented Nov 29, 2017

@michaelhelmick. Have you tried recreating the virtual environment? I'm wondering if something else is causing this. The following worked with requests v2.18.4:

import requests
requests.packages.urllib3

It's also worth noting that the test suite covers the relevant paths. You can see here that the latest requests version is installed in the builds, and here that the RequestsClient is covered.

@carltongibson
Copy link
Copy Markdown
Collaborator

So I've got the same result as @rpkilby here:

>>> import requests
>>> requests.__version__
'2.18.4'
>>> requests.packages.urllib3
<module 'urllib3' from '/Users/carlton/ve/rest_framework/lib/python3.5/site-packages/urllib3/__init__.py'>

I'm going to close this. There must be something else going on. Happy to re-open, or address a separate issue, if we can pin that down.

@audiolion
Copy link
Copy Markdown
Author

maybe his urllib3 isn't installed? requests imports it basically and puts it under that namespace, but if it can't find it then we get the module not found error?

@michaelhelmick
Copy link
Copy Markdown

Yeah, I reinstalled reqs (in a clean virtualenv) and this works. Sorry about the false alarm!

@audiolion audiolion deleted the requests-updated-compat branch November 29, 2017 19:31
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

Successfully merging this pull request may close these issues.

5 participants