Skip to content

Commit c47a4e7

Browse files
authored
feat: remove compat module (#303)
1 parent 861de1e commit c47a4e7

17 files changed

Lines changed: 75 additions & 126 deletions

.github/workflows/tests.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ jobs:
2525
os: "windows-latest"
2626

2727
steps:
28-
- uses: "actions/checkout@v2"
29-
- uses: "actions/setup-python@v2"
28+
- uses: "actions/checkout@v3"
29+
- uses: "actions/setup-python@v4"
3030
with:
3131
python-version: "${{ matrix.python-version }}"
3232
- name: "Install dependencies"
@@ -38,6 +38,3 @@ jobs:
3838
3939
- name: "Run tox targets for ${{ matrix.python-version }}"
4040
run: "python -m tox"
41-
42-
- name: "Run mypy for ${{ matrix.python-version }}"
43-
run: "python -m tox -e mypy"

cachecontrol/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
__email__ = "eric@ionrock.org"
1111
__version__ = "0.12.11"
1212

13-
from .adapter import CacheControlAdapter
14-
from .controller import CacheController
15-
from .wrapper import CacheControl
13+
from cachecontrol.adapter import CacheControlAdapter
14+
from cachecontrol.controller import CacheController
15+
from cachecontrol.wrapper import CacheControl
1616

1717
__all__ = [
1818
"__author__",

cachecontrol/_cmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
if TYPE_CHECKING:
1616
from argparse import Namespace
1717

18-
from .controller import CacheController
18+
from cachecontrol.controller import CacheController
1919

2020

2121
def setup_logging() -> None:

cachecontrol/adapter.py

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

1010
from requests.adapters import HTTPAdapter
1111

12-
from .cache import DictCache
13-
from .controller import PERMANENT_REDIRECT_STATUSES, CacheController
14-
from .filewrapper import CallbackFileWrapper
12+
from cachecontrol.cache import DictCache
13+
from cachecontrol.controller import PERMANENT_REDIRECT_STATUSES, CacheController
14+
from cachecontrol.filewrapper import CallbackFileWrapper
1515

1616
if TYPE_CHECKING:
1717
from requests import PreparedRequest, Response
18+
from urllib3 import HTTPResponse
1819

19-
from .cache import BaseCache
20-
from .compat import HTTPResponse
21-
from .heuristics import BaseHeuristic
22-
from .serialize import Serializer
20+
from cachecontrol.cache import BaseCache
21+
from cachecontrol.heuristics import BaseHeuristic
22+
from cachecontrol.serialize import Serializer
2323

2424

2525
class CacheControlAdapter(HTTPAdapter):
@@ -128,21 +128,21 @@ def build_response(
128128
else:
129129
# Wrap the response file with a wrapper that will cache the
130130
# response when the stream has been consumed.
131-
response._fp = CallbackFileWrapper(
132-
response._fp,
131+
response._fp = CallbackFileWrapper( # type: ignore[attr-defined]
132+
response._fp, # type: ignore[attr-defined]
133133
functools.partial(
134134
self.controller.cache_response, request, response
135135
),
136136
)
137137
if response.chunked:
138-
super_update_chunk_length = response._update_chunk_length
138+
super_update_chunk_length = response._update_chunk_length # type: ignore[attr-defined]
139139

140140
def _update_chunk_length(self: "HTTPResponse") -> None:
141141
super_update_chunk_length()
142142
if self.chunk_left == 0:
143-
self._fp._close()
143+
self._fp._close() # type: ignore[attr-defined]
144144

145-
response._update_chunk_length = types.MethodType(
145+
response._update_chunk_length = types.MethodType( # type: ignore[attr-defined]
146146
_update_chunk_length, response
147147
)
148148

cachecontrol/caches/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
from .file_cache import FileCache, SeparateBodyFileCache
6-
from .redis_cache import RedisCache
7-
5+
from cachecontrol.caches.file_cache import FileCache, SeparateBodyFileCache
6+
from cachecontrol.caches.redis_cache import RedisCache
87

98
__all__ = ["FileCache", "SeparateBodyFileCache", "RedisCache"]

cachecontrol/caches/file_cache.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,15 @@
77
from textwrap import dedent
88
from typing import IO, TYPE_CHECKING, Optional, Type, Union
99

10-
from ..cache import BaseCache, SeparateBodyBaseCache
11-
from ..controller import CacheController
10+
from cachecontrol.cache import BaseCache, SeparateBodyBaseCache
11+
from cachecontrol.controller import CacheController
1212

1313
if TYPE_CHECKING:
1414
from datetime import datetime
1515

1616
from filelock import BaseFileLock
1717

1818

19-
try:
20-
FileNotFoundError
21-
except NameError:
22-
# py2.X
23-
FileNotFoundError = (IOError, OSError)
24-
25-
2619
def _secure_open_write(filename: str, fmode: int) -> "IO[bytes]":
2720
# We only want to write to this file, so open it in write only mode
2821
flags = os.O_WRONLY

cachecontrol/compat.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

cachecontrol/controller.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414

1515
from requests.structures import CaseInsensitiveDict
1616

17-
from .cache import DictCache, SeparateBodyBaseCache
18-
from .serialize import Serializer
17+
from cachecontrol.cache import DictCache, SeparateBodyBaseCache
18+
from cachecontrol.serialize import Serializer
1919

2020
if TYPE_CHECKING:
21+
from typing import Literal
22+
2123
from requests import PreparedRequest
24+
from urllib3 import HTTPResponse
2225

23-
from .cache import BaseCache
24-
from .compat import HTTPResponse
26+
from cachecontrol.cache import BaseCache
2527

2628
logger = logging.getLogger(__name__)
2729

@@ -157,7 +159,9 @@ def _load_from_cache(self, request: "PreparedRequest") -> Optional["HTTPResponse
157159
logger.warning("Cache entry deserialization failed, entry ignored")
158160
return result
159161

160-
def cached_request(self, request: "PreparedRequest") -> Union["HTTPResponse", bool]:
162+
def cached_request(
163+
self, request: "PreparedRequest"
164+
) -> Union["HTTPResponse", "Literal[False]"]:
161165
"""
162166
Return a cached response if it exists in the cache, otherwise
163167
return False.
@@ -478,7 +482,7 @@ def update_cached_response(
478482
cached_response.headers.update(
479483
dict(
480484
(k, v)
481-
for k, v in response.headers.items()
485+
for k, v in response.headers.items() # type: ignore[no-untyped-call]
482486
if k.lower() not in excluded_headers
483487
)
484488
)

cachecontrol/heuristics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional
1010

1111
if TYPE_CHECKING:
12-
from .compat import HTTPResponse
12+
from urllib3 import HTTPResponse
1313

1414
TIME_FMT = "%a, %d %b %Y %H:%M:%S GMT"
1515

cachecontrol/serialize.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import base64
66
import io
77
import json
8+
import pickle
89
import zlib
910
from typing import IO, TYPE_CHECKING, Any, Mapping, Optional
1011

1112
import msgpack
1213
from requests.structures import CaseInsensitiveDict
13-
14-
from .compat import HTTPResponse, pickle, text_type
14+
from urllib3 import HTTPResponse
1515

1616
if TYPE_CHECKING:
1717
from requests import PreparedRequest, Request
@@ -44,18 +44,16 @@ def dumps(
4444
# also update the response with a new file handler to be
4545
# sure it acts as though it was never read.
4646
body = response.read(decode_content=False)
47-
response._fp = io.BytesIO(body)
47+
response._fp = io.BytesIO(body) # type: ignore[attr-defined]
4848
response.length_remaining = len(body)
4949

5050
data = {
5151
"response": {
5252
"body": body, # Empty bytestring if body is stored separately
53-
"headers": dict(
54-
(text_type(k), text_type(v)) for k, v in response.headers.items()
55-
),
53+
"headers": dict((str(k), str(v)) for k, v in response.headers.items()), # type: ignore[no-untyped-call]
5654
"status": response.status,
5755
"version": response.version,
58-
"reason": text_type(response.reason),
56+
"reason": str(response.reason),
5957
"decode_content": response.decode_content,
6058
}
6159
}
@@ -65,10 +63,10 @@ def dumps(
6563
if "vary" in response_headers:
6664
varied_headers = response_headers["vary"].split(",")
6765
for header in varied_headers:
68-
header = text_type(header).strip()
66+
header = str(header).strip()
6967
header_value = request.headers.get(header, None)
7068
if header_value is not None:
71-
header_value = text_type(header_value)
69+
header_value = str(header_value)
7270
data["vary"][header] = header_value
7371

7472
return b",".join([b"cc=4", msgpack.dumps(data, use_bin_type=True)])
@@ -78,10 +76,10 @@ def loads(
7876
request: "PreparedRequest",
7977
data: bytes,
8078
body_file: Optional["IO[bytes]"] = None,
81-
) -> HTTPResponse:
79+
) -> Optional[HTTPResponse]:
8280
# Short circuit if we've been given an empty set of data
8381
if not data:
84-
return
82+
return None
8583

8684
# Determine what version of the serializer the data was serialized
8785
# with
@@ -101,12 +99,12 @@ def loads(
10199

102100
# Dispatch to the actual load method for the given version
103101
try:
104-
return getattr(self, "_loads_v{}".format(verstr))(request, data, body_file)
102+
return getattr(self, "_loads_v{}".format(verstr))(request, data, body_file) # type: ignore[no-any-return]
105103

106104
except AttributeError:
107105
# This is a version we don't have a loads function for, so we'll
108106
# just treat it as a miss and return None
109-
return
107+
return None
110108

111109
def prepare_response(
112110
self,

0 commit comments

Comments
 (0)