Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use six instead of twisted.python.compat #290

Merged
merged 12 commits into from
Sep 9, 2020
8 changes: 5 additions & 3 deletions src/treq/client.py
Expand Up @@ -5,10 +5,12 @@

from io import BytesIO

from six import PY3

from twisted.internet.interfaces import IProtocol
from twisted.internet.defer import Deferred
from twisted.python.components import proxyForInterface
from twisted.python.compat import _PY3, unicode
from twisted.python.compat import unicode
altendky marked this conversation as resolved.
Show resolved Hide resolved
from twisted.python.filepath import FilePath
from hyperlink import DecodedURL, EncodedURL

Expand All @@ -33,7 +35,7 @@
from treq.response import _Response
from requests.cookies import cookiejar_from_dict, merge_cookies

if _PY3:
if PY3:
from urllib.parse import urlencode as _urlencode
altendky marked this conversation as resolved.
Show resolved Hide resolved

def urlencode(query, doseq):
altendky marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -377,7 +379,7 @@ def _guess_content_type(filename):
registerAdapter(_from_bytes, bytes, IBodyProducer)
registerAdapter(_from_file, BytesIO, IBodyProducer)

if not _PY3:
if not PY3:
altendky marked this conversation as resolved.
Show resolved Hide resolved
from StringIO import StringIO
altendky marked this conversation as resolved.
Show resolved Hide resolved
registerAdapter(_from_file, StringIO, IBodyProducer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

io.StringIO is the wrong StringIO

Suggested change
registerAdapter(_from_file, StringIO, IBodyProducer)
registerAdapter(_from_file, six.StringIO, IBodyProducer)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's best to

import io
import six

and then use io.StringIO or six.StringIO so it's obvious

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is coming from six. (aren't these from imports great?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah right, that's super confusing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@altendky the first hit for six uses an idiomatic import:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend following modernize here and using idiomatic-style imports for both io and six https://modernize.readthedocs.io/en/latest/fixers.html#unicode_type

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Over following the existing local practice? (and that import is off in some other file that i didn't get to touching)

# Suppress lint failure on Python 3.
altendky marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
6 changes: 4 additions & 2 deletions src/treq/multipart.py
Expand Up @@ -7,13 +7,15 @@
from io import BytesIO
from contextlib import closing

from six import PY3

from twisted.internet import defer, task
from twisted.python.compat import unicode, _PY3
from twisted.python.compat import unicode
from twisted.web.iweb import UNKNOWN_LENGTH, IBodyProducer

from zope.interface import implementer

if _PY3:
if PY3:
long = int
altendky marked this conversation as resolved.
Show resolved Hide resolved

CRLF = b"\r\n"
Expand Down
4 changes: 3 additions & 1 deletion src/treq/test/test_multipart.py
Expand Up @@ -10,14 +10,16 @@
from twisted.trial import unittest
from zope.interface.verify import verifyObject

from six import PY3

from twisted.python import compat
from twisted.internet import task
from twisted.web.client import FileBodyProducer
from twisted.web.iweb import UNKNOWN_LENGTH, IBodyProducer

from treq.multipart import MultiPartProducer, _LengthConsumer

if compat._PY3:
if PY3:
long = int
unicode = compat.unicode

Expand Down
5 changes: 2 additions & 3 deletions src/treq/test/test_testing.py
Expand Up @@ -6,14 +6,13 @@

from mock import ANY

from six import text_type, binary_type
from six import text_type, binary_type, PY3

from twisted.trial.unittest import TestCase
from twisted.web.client import ResponseFailed
from twisted.web.error import SchemeNotSupported
from twisted.web.resource import Resource
from twisted.web.server import NOT_DONE_YET
from twisted.python.compat import _PY3

import treq

Expand Down Expand Up @@ -307,7 +306,7 @@ def test_repr(self):
"""
:obj:`HasHeaders` returns a nice string repr.
"""
if _PY3:
if PY3:
reprOutput = "HasHeaders({b'a': [b'b']})"
else:
reprOutput = "HasHeaders({'a': ['b']})"
Expand Down