Skip to content

Commit 1cf09ec

Browse files
committed
refactor: remove old compat text_type, byte_type, basestring
1 parent 7224add commit 1cf09ec

File tree

4 files changed

+21
-35
lines changed

4 files changed

+21
-35
lines changed

mocket/compat.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,17 @@
99

1010
ENCODING: Final[str] = os.getenv("MOCKET_ENCODING", "utf-8")
1111

12-
text_type = str
13-
byte_type = bytes
14-
basestring = (str,)
15-
1612

1713
def encode_to_bytes(s: str | bytes, encoding: str = ENCODING) -> bytes:
18-
if isinstance(s, text_type):
14+
if isinstance(s, str):
1915
s = s.encode(encoding)
20-
return byte_type(s)
16+
return bytes(s)
2117

2218

2319
def decode_from_bytes(s: str | bytes, encoding: str = ENCODING) -> str:
24-
if isinstance(s, byte_type):
20+
if isinstance(s, bytes):
2521
s = codecs.decode(s, encoding, "ignore")
26-
return text_type(s)
22+
return str(s)
2723

2824

2925
def shsplit(s: str | bytes) -> list[str]:

mocket/mocket.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,7 @@
2323
urllib3_wrap_socket = None
2424

2525

26-
from mocket.compat import (
27-
basestring,
28-
byte_type,
29-
decode_from_bytes,
30-
encode_to_bytes,
31-
text_type,
32-
)
26+
from mocket.compat import decode_from_bytes, encode_to_bytes
3327
from mocket.utils import (
3428
MocketMode,
3529
MocketSocketCore,
@@ -323,7 +317,7 @@ def true_sendall(self, data, *args, **kwargs):
323317
# make request unique again
324318
req_signature = _hash_request(hasher, req)
325319
# port should be always a string
326-
port = text_type(self._port)
320+
port = str(self._port)
327321

328322
# prepare responses dictionary
329323
responses = {}
@@ -433,7 +427,7 @@ class Mocket:
433427
_address = (None, None)
434428
_entries = collections.defaultdict(list)
435429
_requests = []
436-
_namespace = text_type(id(_entries))
430+
_namespace = str(id(_entries))
437431
_truesocket_recording_dir = None
438432

439433
@classmethod
@@ -524,7 +518,7 @@ def enable(namespace=None, truesocket_recording_dir=None):
524518
socket.socketpair = socket.__dict__["socketpair"] = socketpair
525519
ssl.wrap_socket = ssl.__dict__["wrap_socket"] = FakeSSLContext.wrap_socket
526520
ssl.SSLContext = ssl.__dict__["SSLContext"] = FakeSSLContext
527-
socket.inet_pton = socket.__dict__["inet_pton"] = lambda family, ip: byte_type(
521+
socket.inet_pton = socket.__dict__["inet_pton"] = lambda family, ip: bytes(
528522
"\x7f\x00\x00\x01", "utf-8"
529523
)
530524
urllib3.util.ssl_.wrap_socket = urllib3.util.ssl_.__dict__["wrap_socket"] = (
@@ -598,13 +592,13 @@ def assert_fail_if_entries_not_served(cls):
598592

599593

600594
class MocketEntry:
601-
class Response(byte_type):
595+
class Response(bytes):
602596
@property
603597
def data(self):
604598
return self
605599

606600
response_index = 0
607-
request_cls = byte_type
601+
request_cls = bytes
608602
response_cls = Response
609603
responses = None
610604
_served = None
@@ -613,9 +607,7 @@ def __init__(self, location, responses):
613607
self._served = False
614608
self.location = location
615609

616-
if not isinstance(responses, collections_abc.Iterable) or isinstance(
617-
responses, basestring
618-
):
610+
if not isinstance(responses, collections_abc.Iterable):
619611
responses = [responses]
620612

621613
if not responses:
@@ -624,7 +616,7 @@ def __init__(self, location, responses):
624616
self.responses = []
625617
for r in responses:
626618
if not isinstance(r, BaseException) and not getattr(r, "data", False):
627-
if isinstance(r, text_type):
619+
if isinstance(r, str):
628620
r = encode_to_bytes(r)
629621
r = self.response_cls(r)
630622
self.responses.append(r)
@@ -664,7 +656,7 @@ def __init__(
664656
):
665657
self.instance = instance
666658
self.truesocket_recording_dir = truesocket_recording_dir
667-
self.namespace = namespace or text_type(id(self))
659+
self.namespace = namespace or str(id(self))
668660
MocketMode().STRICT = strict_mode
669661
if strict_mode:
670662
MocketMode().STRICT_ALLOWED = strict_mode_allowed or []

mocket/mockredis.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from itertools import chain
22

33
from mocket.compat import (
4-
byte_type,
54
decode_from_bytes,
65
encode_to_bytes,
76
shsplit,
8-
text_type,
97
)
108
from mocket.mocket import Mocket, MocketEntry
119

@@ -20,7 +18,7 @@ def __init__(self, data=None):
2018
self.data = Redisizer.redisize(data or OK)
2119

2220

23-
class Redisizer(byte_type):
21+
class Redisizer(bytes):
2422
@staticmethod
2523
def tokens(iterable):
2624
iterable = [encode_to_bytes(x) for x in iterable]
@@ -36,15 +34,15 @@ def get_conversion(t):
3634
Redisizer.tokens(list(chain(*tuple(x.items()))))
3735
),
3836
int: lambda x: f":{x}".encode(),
39-
text_type: lambda x: "${}\r\n{}".format(
40-
len(x.encode("utf-8")), x
41-
).encode("utf-8"),
37+
str: lambda x: "${}\r\n{}".format(len(x.encode("utf-8")), x).encode(
38+
"utf-8"
39+
),
4240
list: lambda x: b"\r\n".join(Redisizer.tokens(x)),
4341
}[t]
4442

4543
if isinstance(data, Redisizer):
4644
return data
47-
if isinstance(data, byte_type):
45+
if isinstance(data, bytes):
4846
data = decode_from_bytes(data)
4947
return Redisizer(get_conversion(data.__class__)(data) + b"\r\n")
5048

mocket/plugins/httpretty/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from mocket import Mocket, mocketize
22
from mocket.async_mocket import async_mocketize
3-
from mocket.compat import ENCODING, byte_type, text_type
3+
from mocket.compat import ENCODING
44
from mocket.mockhttp import Entry as MocketHttpEntry
55
from mocket.mockhttp import Request as MocketHttpRequest
66
from mocket.mockhttp import Response as MocketHttpResponse
@@ -129,6 +129,6 @@ def __getattr__(self, name):
129129
"HEAD",
130130
"PATCH",
131131
"register_uri",
132-
"text_type",
133-
"byte_type",
132+
"str",
133+
"bytes",
134134
)

0 commit comments

Comments
 (0)