Skip to content

Commit

Permalink
Updating in response to review
Browse files Browse the repository at this point in the history
  • Loading branch information
sg3-141-592 committed Jun 7, 2023
1 parent 7d3969e commit 5aa78bb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/urllib3/request.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import absolute_import

import sys
import warnings

from .filepost import encode_multipart_formdata
from .packages import six
Expand Down Expand Up @@ -176,16 +175,18 @@ def request_encode_body(

if not six.PY2:

class RequestModule(object):
class RequestModule(sys.modules[__name__].__class__):
def __call__(self, *args, **kwargs):
"""
If user tries to call this module directly urllib3 v2.x style raise an error to the user
suggesting they may need urllib3 v2
"""
warnings.warn(
"urllib3.requests() method is not supported in this release\n"
raise TypeError(
"TypeError: 'module' object is not callable\n"
"urllib3.requests() method is not supported in this release, "
"upgrade to urllib3 v2 to use it"
)

RequestMethods = RequestMethods

sys.modules[__name__] = RequestModule()
sys.modules[__name__].__class__ = RequestModule
9 changes: 7 additions & 2 deletions test/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
reason="This behaviour isn't added when running urllib3 in Python 2",
)
class TestRequestImport(object):
def test_request_import_warning(self):
def test_request_import_error(self):
"""Ensure an appropriate error is raised to the user
if they try and run urllib3.request()"""
with pytest.warns(UserWarning):
with pytest.raises(TypeError) as exc_info:
urllib3.request(1, a=2)
assert "urllib3 v2" in exc_info.value.args[0]

def test_request_method_import(self):
"""Ensure that * method imports are not broken by this change"""
from urllib3.request import urlencode # noqa: F401

0 comments on commit 5aa78bb

Please sign in to comment.