From 8a5050ea41dffe7ca1b1fc95122504eb89d0471d Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 11 Sep 2020 14:37:51 +0100 Subject: [PATCH] Refactor to use isinstance(..., typing.Iterator) (#1282) --- httpx/_content_streams.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/httpx/_content_streams.py b/httpx/_content_streams.py index 3cd2196a..e51646b2 100644 --- a/httpx/_content_streams.py +++ b/httpx/_content_streams.py @@ -380,11 +380,9 @@ def encode( return URLEncodedStream(data=data) elif isinstance(data, (str, bytes)): return ByteStream(body=data) - elif hasattr(data, "__aiter__"): - data = typing.cast(typing.AsyncIterator[bytes], data) + elif isinstance(data, typing.AsyncIterator): return AsyncIteratorStream(aiterator=data) - elif hasattr(data, "__iter__"): - data = typing.cast(typing.Iterator[bytes], data) + elif isinstance(data, typing.Iterator): return IteratorStream(iterator=data) raise TypeError(f"Unexpected type for 'data', {type(data)!r}") @@ -395,11 +393,9 @@ def encode_response(content: ResponseContent = None) -> ContentStream: return ByteStream(b"") elif isinstance(content, bytes): return ByteStream(body=content) - elif hasattr(content, "__aiter__"): - content = typing.cast(typing.AsyncIterator[bytes], content) + elif isinstance(content, typing.AsyncIterator): return AsyncIteratorStream(aiterator=content) - elif hasattr(content, "__iter__"): - content = typing.cast(typing.Iterator[bytes], content) + elif isinstance(content, typing.Iterator): return IteratorStream(iterator=content) raise TypeError(f"Unexpected type for 'content', {type(content)!r}") -- 2.47.3