Skip to content

Commit 54618df

Browse files
authored
Merge pull request #262 from betaboon/refactor-absolute-imports-and-remove-compat
Refactor absolute imports and remove compat
2 parents 3bf9686 + 1cf09ec commit 54618df

File tree

8 files changed

+37
-39
lines changed

8 files changed

+37
-39
lines changed

mocket/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from .async_mocket import async_mocketize
2-
from .mocket import FakeSSLContext, Mocket, MocketEntry, Mocketizer, mocketize
1+
from mocket.async_mocket import async_mocketize
2+
from mocket.mocket import FakeSSLContext, Mocket, MocketEntry, Mocketizer, mocketize
33

44
__all__ = (
55
"async_mocketize",

mocket/async_mocket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from .mocket import Mocketizer
2-
from .utils import get_mocketize
1+
from mocket.mocket import Mocketizer
2+
from mocket.utils import get_mocketize
33

44

55
async def wrapper(

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: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
urllib3_wrap_socket = None
2424

2525

26-
from .compat import basestring, byte_type, decode_from_bytes, encode_to_bytes, text_type
27-
from .utils import (
26+
from mocket.compat import decode_from_bytes, encode_to_bytes
27+
from mocket.utils import (
2828
MocketMode,
2929
MocketSocketCore,
3030
get_mocketize,
@@ -317,7 +317,7 @@ def true_sendall(self, data, *args, **kwargs):
317317
# make request unique again
318318
req_signature = _hash_request(hasher, req)
319319
# port should be always a string
320-
port = text_type(self._port)
320+
port = str(self._port)
321321

322322
# prepare responses dictionary
323323
responses = {}
@@ -427,7 +427,7 @@ class Mocket:
427427
_address = (None, None)
428428
_entries = collections.defaultdict(list)
429429
_requests = []
430-
_namespace = text_type(id(_entries))
430+
_namespace = str(id(_entries))
431431
_truesocket_recording_dir = None
432432

433433
@classmethod
@@ -518,7 +518,7 @@ def enable(namespace=None, truesocket_recording_dir=None):
518518
socket.socketpair = socket.__dict__["socketpair"] = socketpair
519519
ssl.wrap_socket = ssl.__dict__["wrap_socket"] = FakeSSLContext.wrap_socket
520520
ssl.SSLContext = ssl.__dict__["SSLContext"] = FakeSSLContext
521-
socket.inet_pton = socket.__dict__["inet_pton"] = lambda family, ip: byte_type(
521+
socket.inet_pton = socket.__dict__["inet_pton"] = lambda family, ip: bytes(
522522
"\x7f\x00\x00\x01", "utf-8"
523523
)
524524
urllib3.util.ssl_.wrap_socket = urllib3.util.ssl_.__dict__["wrap_socket"] = (
@@ -592,13 +592,13 @@ def assert_fail_if_entries_not_served(cls):
592592

593593

594594
class MocketEntry:
595-
class Response(byte_type):
595+
class Response(bytes):
596596
@property
597597
def data(self):
598598
return self
599599

600600
response_index = 0
601-
request_cls = byte_type
601+
request_cls = bytes
602602
response_cls = Response
603603
responses = None
604604
_served = None
@@ -607,9 +607,7 @@ def __init__(self, location, responses):
607607
self._served = False
608608
self.location = location
609609

610-
if not isinstance(responses, collections_abc.Iterable) or isinstance(
611-
responses, basestring
612-
):
610+
if not isinstance(responses, collections_abc.Iterable):
613611
responses = [responses]
614612

615613
if not responses:
@@ -618,7 +616,7 @@ def __init__(self, location, responses):
618616
self.responses = []
619617
for r in responses:
620618
if not isinstance(r, BaseException) and not getattr(r, "data", False):
621-
if isinstance(r, text_type):
619+
if isinstance(r, str):
622620
r = encode_to_bytes(r)
623621
r = self.response_cls(r)
624622
self.responses.append(r)
@@ -658,7 +656,7 @@ def __init__(
658656
):
659657
self.instance = instance
660658
self.truesocket_recording_dir = truesocket_recording_dir
661-
self.namespace = namespace or text_type(id(self))
659+
self.namespace = namespace or str(id(self))
662660
MocketMode().STRICT = strict_mode
663661
if strict_mode:
664662
MocketMode().STRICT_ALLOWED = strict_mode_allowed or []

mocket/mockhttp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from h11 import SERVER, Connection, Data
88
from h11 import Request as H11Request
99

10-
from .compat import ENCODING, decode_from_bytes, do_the_magic, encode_to_bytes
11-
from .mocket import Mocket, MocketEntry
10+
from mocket.compat import ENCODING, decode_from_bytes, do_the_magic, encode_to_bytes
11+
from mocket.mocket import Mocket, MocketEntry
1212

1313
STATUS = {k: v[0] for k, v in BaseHTTPRequestHandler.responses.items()}
1414
CRLF = "\r\n"

mocket/mockredis.py

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

3-
from .compat import byte_type, decode_from_bytes, encode_to_bytes, shsplit, text_type
4-
from .mocket import Mocket, MocketEntry
3+
from mocket.compat import (
4+
decode_from_bytes,
5+
encode_to_bytes,
6+
shsplit,
7+
)
8+
from mocket.mocket import Mocket, MocketEntry
59

610

711
class Request:
@@ -14,7 +18,7 @@ def __init__(self, data=None):
1418
self.data = Redisizer.redisize(data or OK)
1519

1620

17-
class Redisizer(byte_type):
21+
class Redisizer(bytes):
1822
@staticmethod
1923
def tokens(iterable):
2024
iterable = [encode_to_bytes(x) for x in iterable]
@@ -30,15 +34,15 @@ def get_conversion(t):
3034
Redisizer.tokens(list(chain(*tuple(x.items()))))
3135
),
3236
int: lambda x: f":{x}".encode(),
33-
text_type: lambda x: "${}\r\n{}".format(
34-
len(x.encode("utf-8")), x
35-
).encode("utf-8"),
37+
str: lambda x: "${}\r\n{}".format(len(x.encode("utf-8")), x).encode(
38+
"utf-8"
39+
),
3640
list: lambda x: b"\r\n".join(Redisizer.tokens(x)),
3741
}[t]
3842

3943
if isinstance(data, Redisizer):
4044
return data
41-
if isinstance(data, byte_type):
45+
if isinstance(data, bytes):
4246
data = decode_from_bytes(data)
4347
return Redisizer(get_conversion(data.__class__)(data) + b"\r\n")
4448

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
)

mocket/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import ssl
77
from typing import TYPE_CHECKING, Any, Callable, ClassVar
88

9-
from .compat import decode_from_bytes, encode_to_bytes
10-
from .exceptions import StrictMocketException
9+
from mocket.compat import decode_from_bytes, encode_to_bytes
10+
from mocket.exceptions import StrictMocketException
1111

1212
if TYPE_CHECKING: # pragma: no cover
1313
from typing import NoReturn
@@ -83,7 +83,7 @@ def is_allowed(self, location: str | tuple[str, int]) -> bool:
8383

8484
@staticmethod
8585
def raise_not_allowed() -> NoReturn:
86-
from .mocket import Mocket
86+
from mocket.mocket import Mocket
8787

8888
current_entries = [
8989
(location, "\n ".join(map(str, entries)))

0 commit comments

Comments
 (0)