AttributeError: 'NoneType' object has no attribute 'read'
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
requests-mock |
Invalid
|
Undecided
|
Jamie Lennox |
Bug Description
Originally filed here: https:/
I'm using a test suite which uses `requests_mock`, and it appears that something is not handled properly for the new 2.12.1 version of `requests`.
Build: https:/
Relevant part:
```
=======
_________________ InvalidLinkBear
self = <tests.
def test_redirect_
https:/
https:/
http://
> settings=
tests\general\
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
tests\general\
out = list(uut.
bears\general\
file, timeout, link_ignore_regex):
bears\general\
code = InvalidLinkBear
bears\general\
timeout=
C:\Python34\
return request('head', url, **kwargs)
C:\Python34\
return session.
C:\Python34\
resp = self.send(prep, **send_kwargs)
C:\Python34\
return self._real_
C:\Python34\
r.content
C:\Python34\
self._content = bytes()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
def generate():
# Special case for urllib3.
if hasattr(self.raw, 'stream'):
try:
for chunk in self.raw.
except ProtocolError as e:
except DecodeError as e:
except ReadTimeoutError as e:
else:
# Standard file-like object.
while True:
> chunk = self.raw.
E AttributeError: 'NoneType' object has no attribute 'read'
C:\Python34\
```
happens on Windows and Linux.
Closing as not a bug in requests-mock, but in the way coala is providing its responses.
Duplicating my response from https:/ /github. com/kennethreit z/requests/ issues/ 3698
From a requests-mock perspective bug #1642697 was filed that was hit because of the exact same removal of AttributeError handling in https:/ /github. com/kennethreit z/requests/ commit/ 327512f5ef9eea7 9a0cb324ba47e7e abf6967b3a . This only affects the requests-mock tests and not the mocking itself but is likely the same result.
From a requests-mock usage perspective (which should probably go elsewhere, but i'm not sure where) there's a few things you can fix in your tests to not have this problem.
1). It's unlikely you really want a custom matcher, A matcher is the code that checks if you want to return a response. When you do a requests_mock.get() or .register_uri() it creates a matcher for you and you can regexp or a whole variety of things there. You can then attach a function that fills in a response for you, and requests_mock will handle making it a response that requests understands [1].
It's fine/expected to make register each url you are using with it's own response rather than try to cram everything into the same custom matcher.
2). requests_ mock.create_ response( ) is a function that is used to create a HTTPResponse that requests understands and it's public for just this reason. If you use this instead of constructing your own requests.Response it will patch all this stuff for you.
3). update to requests_mock 1.x. There's no API break, it had just been stable long enough to not be considered a 0.x release any more. There's also probably not anything you really need from the 1.x branch but it will be less likely to break with new requests releases.
[1] http:// requests- mock.readthedoc s.io/en/ latest/ response. html#dynamic- response