Skip to content

feat: remove compat module #303

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

Merged
merged 2 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ jobs:
os: "windows-latest"

steps:
- uses: "actions/checkout@v2"
- uses: "actions/setup-python@v2"
- uses: "actions/checkout@v3"
- uses: "actions/setup-python@v4"
with:
python-version: "${{ matrix.python-version }}"
- name: "Install dependencies"
Expand All @@ -38,6 +38,3 @@ jobs:

- name: "Run tox targets for ${{ matrix.python-version }}"
run: "python -m tox"

- name: "Run mypy for ${{ matrix.python-version }}"
run: "python -m tox -e mypy"
6 changes: 3 additions & 3 deletions cachecontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
__email__ = "eric@ionrock.org"
__version__ = "0.12.11"

from .adapter import CacheControlAdapter
from .controller import CacheController
from .wrapper import CacheControl
from cachecontrol.adapter import CacheControlAdapter
from cachecontrol.controller import CacheController
from cachecontrol.wrapper import CacheControl

__all__ = [
"__author__",
Expand Down
2 changes: 1 addition & 1 deletion cachecontrol/_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
if TYPE_CHECKING:
from argparse import Namespace

from .controller import CacheController
from cachecontrol.controller import CacheController


def setup_logging() -> None:
Expand Down
24 changes: 12 additions & 12 deletions cachecontrol/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

from requests.adapters import HTTPAdapter

from .cache import DictCache
from .controller import PERMANENT_REDIRECT_STATUSES, CacheController
from .filewrapper import CallbackFileWrapper
from cachecontrol.cache import DictCache
from cachecontrol.controller import PERMANENT_REDIRECT_STATUSES, CacheController
from cachecontrol.filewrapper import CallbackFileWrapper

if TYPE_CHECKING:
from requests import PreparedRequest, Response
from urllib3 import HTTPResponse

from .cache import BaseCache
from .compat import HTTPResponse
from .heuristics import BaseHeuristic
from .serialize import Serializer
from cachecontrol.cache import BaseCache
from cachecontrol.heuristics import BaseHeuristic
from cachecontrol.serialize import Serializer


class CacheControlAdapter(HTTPAdapter):
Expand Down Expand Up @@ -128,21 +128,21 @@ def build_response(
else:
# Wrap the response file with a wrapper that will cache the
# response when the stream has been consumed.
response._fp = CallbackFileWrapper(
response._fp,
response._fp = CallbackFileWrapper( # type: ignore[attr-defined]
response._fp, # type: ignore[attr-defined]
functools.partial(
self.controller.cache_response, request, response
),
)
if response.chunked:
super_update_chunk_length = response._update_chunk_length
super_update_chunk_length = response._update_chunk_length # type: ignore[attr-defined]

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

response._update_chunk_length = types.MethodType(
response._update_chunk_length = types.MethodType( # type: ignore[attr-defined]
_update_chunk_length, response
)

Expand Down
5 changes: 2 additions & 3 deletions cachecontrol/caches/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#
# SPDX-License-Identifier: Apache-2.0

from .file_cache import FileCache, SeparateBodyFileCache
from .redis_cache import RedisCache

from cachecontrol.caches.file_cache import FileCache, SeparateBodyFileCache
from cachecontrol.caches.redis_cache import RedisCache

__all__ = ["FileCache", "SeparateBodyFileCache", "RedisCache"]
11 changes: 2 additions & 9 deletions cachecontrol/caches/file_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,15 @@
from textwrap import dedent
from typing import IO, TYPE_CHECKING, Optional, Type, Union

from ..cache import BaseCache, SeparateBodyBaseCache
from ..controller import CacheController
from cachecontrol.cache import BaseCache, SeparateBodyBaseCache
from cachecontrol.controller import CacheController

if TYPE_CHECKING:
from datetime import datetime

from filelock import BaseFileLock


try:
FileNotFoundError
except NameError:
# py2.X
FileNotFoundError = (IOError, OSError)


def _secure_open_write(filename: str, fmode: int) -> "IO[bytes]":
# We only want to write to this file, so open it in write only mode
flags = os.O_WRONLY
Expand Down
34 changes: 0 additions & 34 deletions cachecontrol/compat.py

This file was deleted.

16 changes: 10 additions & 6 deletions cachecontrol/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@

from requests.structures import CaseInsensitiveDict

from .cache import DictCache, SeparateBodyBaseCache
from .serialize import Serializer
from cachecontrol.cache import DictCache, SeparateBodyBaseCache
from cachecontrol.serialize import Serializer

if TYPE_CHECKING:
from typing import Literal

from requests import PreparedRequest
from urllib3 import HTTPResponse

from .cache import BaseCache
from .compat import HTTPResponse
from cachecontrol.cache import BaseCache

logger = logging.getLogger(__name__)

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

def cached_request(self, request: "PreparedRequest") -> Union["HTTPResponse", bool]:
def cached_request(
self, request: "PreparedRequest"
) -> Union["HTTPResponse", "Literal[False]"]:
"""
Return a cached response if it exists in the cache, otherwise
return False.
Expand Down Expand Up @@ -478,7 +482,7 @@ def update_cached_response(
cached_response.headers.update(
dict(
(k, v)
for k, v in response.headers.items()
for k, v in response.headers.items() # type: ignore[no-untyped-call]
if k.lower() not in excluded_headers
)
)
Expand Down
2 changes: 1 addition & 1 deletion cachecontrol/heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional

if TYPE_CHECKING:
from .compat import HTTPResponse
from urllib3 import HTTPResponse

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

Expand Down
24 changes: 11 additions & 13 deletions cachecontrol/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import base64
import io
import json
import pickle
import zlib
from typing import IO, TYPE_CHECKING, Any, Mapping, Optional

import msgpack
from requests.structures import CaseInsensitiveDict

from .compat import HTTPResponse, pickle, text_type
from urllib3 import HTTPResponse

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

data = {
"response": {
"body": body, # Empty bytestring if body is stored separately
"headers": dict(
(text_type(k), text_type(v)) for k, v in response.headers.items()
),
"headers": dict((str(k), str(v)) for k, v in response.headers.items()), # type: ignore[no-untyped-call]
"status": response.status,
"version": response.version,
"reason": text_type(response.reason),
"reason": str(response.reason),
"decode_content": response.decode_content,
}
}
Expand All @@ -65,10 +63,10 @@ def dumps(
if "vary" in response_headers:
varied_headers = response_headers["vary"].split(",")
for header in varied_headers:
header = text_type(header).strip()
header = str(header).strip()
header_value = request.headers.get(header, None)
if header_value is not None:
header_value = text_type(header_value)
header_value = str(header_value)
data["vary"][header] = header_value

return b",".join([b"cc=4", msgpack.dumps(data, use_bin_type=True)])
Expand All @@ -78,10 +76,10 @@ def loads(
request: "PreparedRequest",
data: bytes,
body_file: Optional["IO[bytes]"] = None,
) -> HTTPResponse:
) -> Optional[HTTPResponse]:
# Short circuit if we've been given an empty set of data
if not data:
return
return None

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

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

except AttributeError:
# This is a version we don't have a loads function for, so we'll
# just treat it as a miss and return None
return
return None

def prepare_response(
self,
Expand Down
13 changes: 6 additions & 7 deletions cachecontrol/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

from typing import TYPE_CHECKING, Collection, Optional, Type

from .adapter import CacheControlAdapter
from .cache import DictCache
from cachecontrol.adapter import CacheControlAdapter
from cachecontrol.cache import DictCache

if TYPE_CHECKING:
import requests

from .cache import BaseCache
from .controller import CacheController
from .heuristics import BaseHeuristic
from .serialize import Serializer
from cachecontrol.cache import BaseCache
from cachecontrol.controller import CacheController
from cachecontrol.heuristics import BaseHeuristic
from cachecontrol.serialize import Serializer


def CacheControl(
Expand All @@ -26,7 +26,6 @@ def CacheControl(
adapter_class: Optional[Type[CacheControlAdapter]] = None,
cacheable_methods: Optional[Collection[str]] = None,
) -> "requests.Session":

cache = DictCache() if cache is None else cache
adapter_class = adapter_class or CacheControlAdapter
adapter = adapter_class(
Expand Down
3 changes: 0 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,5 @@ show_error_codes = true
strict = true
enable_error_code = ignore-without-code,redundant-expr,truthy-bool

[mypy-cachecontrol.compat]
ignore_errors = true

[mypy-msgpack]
ignore_missing_imports = true
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
include_package_data=True,
description="httplib2 caching for requests",
long_description=long_description,
install_requires=["requests", "msgpack>=0.5.2"],
install_requires=["requests>=2.16.0", "msgpack>=0.5.2"],
extras_require={"filecache": ["filelock>=3.8.0"], "redis": ["redis>=2.10.5"]},
entry_points={"console_scripts": ["doesitcache = cachecontrol._cmd:main"]},
python_requires=">=3.6",
Expand Down
11 changes: 4 additions & 7 deletions tests/test_etag.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
# SPDX-FileCopyrightText: 2015 Eric Larson
#
# SPDX-License-Identifier: Apache-2.0
from contextlib import ExitStack
from contextlib import suppress

import pytest

from contextlib import ExitStack, suppress
from unittest.mock import Mock, patch
from urllib.parse import urljoin

import pytest
import requests

from cachecontrol import CacheControl
from cachecontrol.cache import DictCache
from cachecontrol.compat import urljoin
from .utils import NullSerializer
from tests.utils import NullSerializer


class TestETag(object):
Expand Down
Loading