1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-09 09:13:23 +00:00

Add isort to code lint

This commit is contained in:
Jair Henrique
2022-10-09 11:23:20 -03:00
parent eb59d871b4
commit 0eece7f96e
50 changed files with 147 additions and 111 deletions

View File

@@ -1,2 +1,9 @@
[tool.black] [tool.black]
line-length=110 line-length=110
[tool.isort]
line_length = 110
known_first_party = "vcrpy"
multi_line_output = 3
use_parentheses = true
include_trailing_comma = true

View File

@@ -5,7 +5,7 @@ import os
import re import re
import sys import sys
from setuptools import setup, find_packages from setuptools import find_packages, setup
from setuptools.command.test import test as TestCommand from setuptools.command.test import test as TestCommand
long_description = open("README.rst", "r").read() long_description = open("README.rst", "r").read()

View File

@@ -8,6 +8,7 @@ asyncio = pytest.importorskip("asyncio")
aiohttp = pytest.importorskip("aiohttp") aiohttp = pytest.importorskip("aiohttp")
import vcr # noqa: E402 import vcr # noqa: E402
from .aiohttp_utils import aiohttp_app, aiohttp_request # noqa: E402 from .aiohttp_utils import aiohttp_app, aiohttp_request # noqa: E402

View File

@@ -2,11 +2,13 @@ import pytest
boto = pytest.importorskip("boto") boto = pytest.importorskip("boto")
from configparser import DuplicateSectionError # NOQA
import boto # NOQA import boto # NOQA
import boto.iam # NOQA import boto.iam # NOQA
from boto.s3.connection import S3Connection # NOQA from boto.s3.connection import S3Connection # NOQA
from boto.s3.key import Key # NOQA from boto.s3.key import Key # NOQA
from configparser import DuplicateSectionError # NOQA
import vcr # NOQA import vcr # NOQA
@@ -15,6 +17,7 @@ def test_boto_stubs(tmpdir):
# Perform the imports within the patched context so that # Perform the imports within the patched context so that
# CertValidatingHTTPSConnection refers to the patched version. # CertValidatingHTTPSConnection refers to the patched version.
from boto.https_connection import CertValidatingHTTPSConnection from boto.https_connection import CertValidatingHTTPSConnection
from vcr.stubs.boto_stubs import VCRCertValidatingHTTPSConnection from vcr.stubs.boto_stubs import VCRCertValidatingHTTPSConnection
# Prove that the class was patched by the stub and that we can instantiate it. # Prove that the class was patched by the stub and that we can instantiate it.

View File

@@ -1,10 +1,12 @@
import pytest
import os import os
import pytest
boto3 = pytest.importorskip("boto3") boto3 = pytest.importorskip("boto3")
import boto3 # NOQA import boto3 # NOQA
import botocore # NOQA import botocore # NOQA
import vcr # NOQA import vcr # NOQA
try: try:
@@ -64,6 +66,7 @@ def test_boto_vendored_stubs(tmpdir):
HTTPConnection, HTTPConnection,
VerifiedHTTPSConnection, VerifiedHTTPSConnection,
) )
from vcr.stubs.boto3_stubs import VCRRequestsHTTPConnection, VCRRequestsHTTPSConnection from vcr.stubs.boto3_stubs import VCRRequestsHTTPConnection, VCRRequestsHTTPSConnection
# Prove that the class was patched by the stub and that we can instantiate it. # Prove that the class was patched by the stub and that we can instantiate it.

View File

@@ -1,9 +1,11 @@
import os
import json import json
import pytest import os
import vcr
from urllib.request import urlopen from urllib.request import urlopen
import pytest
import vcr
def test_set_serializer_default_config(tmpdir, httpbin): def test_set_serializer_default_config(tmpdir, httpbin):
my_vcr = vcr.VCR(serializer="json") my_vcr = vcr.VCR(serializer="json")

View File

@@ -1,12 +1,14 @@
import base64 import base64
import pytest
from urllib.request import urlopen, Request
from urllib.parse import urlencode
from urllib.error import HTTPError
import vcr
import json import json
from urllib.error import HTTPError
from urllib.parse import urlencode
from urllib.request import Request, urlopen
import pytest
from assertions import assert_cassette_has_one_response, assert_is_json from assertions import assert_cassette_has_one_response, assert_is_json
import vcr
def _request_with_auth(url, username, password): def _request_with_auth(url, username, password):
request = Request(url) request = Request(url)

View File

@@ -2,15 +2,14 @@
"""Integration tests with httplib2""" """Integration tests with httplib2"""
import sys import sys
from urllib.parse import urlencode from urllib.parse import urlencode
import pytest import pytest
import pytest_httpbin.certs import pytest_httpbin.certs
from assertions import assert_cassette_has_one_response
import vcr import vcr
from assertions import assert_cassette_has_one_response
httplib2 = pytest.importorskip("httplib2") httplib2 = pytest.importorskip("httplib2")

View File

@@ -1,6 +1,7 @@
import pytest
import os import os
import pytest
asyncio = pytest.importorskip("asyncio") asyncio = pytest.importorskip("asyncio")
httpx = pytest.importorskip("httpx") httpx = pytest.importorskip("httpx")

View File

@@ -1,6 +1,7 @@
from urllib.request import urlopen
import socket import socket
from contextlib import contextmanager from contextlib import contextmanager
from urllib.request import urlopen
import vcr import vcr

View File

@@ -1,7 +1,8 @@
import vcr
import pytest
from urllib.request import urlopen from urllib.request import urlopen
import pytest
import vcr
DEFAULT_URI = "http://httpbin.org/get?p1=q1&p2=q2" # base uri for testing DEFAULT_URI = "http://httpbin.org/get?p1=q1&p2=q2" # base uri for testing

View File

@@ -1,7 +1,9 @@
import pytest
import vcr
from urllib.request import urlopen from urllib.request import urlopen
import pytest
import vcr
def test_making_extra_request_raises_exception(tmpdir, httpbin): def test_making_extra_request_raises_exception(tmpdir, httpbin):
# make two requests in the first request that are considered # make two requests in the first request that are considered

View File

@@ -1,15 +1,13 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""Test using a proxy.""" """Test using a proxy."""
# External imports
import multiprocessing
import pytest
import http.server import http.server
import multiprocessing
import socketserver import socketserver
from urllib.request import urlopen from urllib.request import urlopen
# Internal imports import pytest
import vcr import vcr
# Conditional imports # Conditional imports

View File

@@ -1,7 +1,9 @@
import pytest
import vcr
from urllib.request import urlopen from urllib.request import urlopen
import pytest
import vcr
def test_once_record_mode(tmpdir, httpbin): def test_once_record_mode(tmpdir, httpbin):
testfile = str(tmpdir.join("recordmode.yml")) testfile = str(tmpdir.join("recordmode.yml"))

View File

@@ -1,6 +1,7 @@
import vcr
from urllib.request import urlopen from urllib.request import urlopen
import vcr
def true_matcher(r1, r2): def true_matcher(r1, r2):
return True return True

View File

@@ -1,6 +1,7 @@
import vcr
from urllib.request import urlopen from urllib.request import urlopen
import vcr
def test_recorded_request_uri_with_redirected_request(tmpdir, httpbin): def test_recorded_request_uri_with_redirected_request(tmpdir, httpbin):
with vcr.use_cassette(str(tmpdir.join("test.yml"))) as cass: with vcr.use_cassette(str(tmpdir.join("test.yml"))) as cass:

View File

@@ -1,8 +1,9 @@
"""Test requests' interaction with vcr""" """Test requests' interaction with vcr"""
import pytest import pytest
import vcr
from assertions import assert_cassette_empty, assert_is_json from assertions import assert_cassette_empty, assert_is_json
import vcr
requests = pytest.importorskip("requests") requests = pytest.importorskip("requests")
from requests.exceptions import ConnectionError # noqa E402 from requests.exceptions import ConnectionError # noqa E402

View File

@@ -1,10 +1,11 @@
import vcr
import zlib
import json
import http.client as httplib import http.client as httplib
import json
import zlib
from assertions import assert_is_json from assertions import assert_is_json
import vcr
def _headers_are_case_insensitive(host, port): def _headers_are_case_insensitive(host, port):
conn = httplib.HTTPConnection(host, port) conn = httplib.HTTPConnection(host, port)

View File

@@ -4,11 +4,11 @@
import json import json
import pytest import pytest
from assertions import assert_cassette_empty, assert_is_json
import vcr import vcr
from vcr.errors import CannotOverwriteExistingCassetteException from vcr.errors import CannotOverwriteExistingCassetteException
from assertions import assert_cassette_empty, assert_is_json
tornado = pytest.importorskip("tornado") tornado = pytest.importorskip("tornado")
http = pytest.importorskip("tornado.httpclient") http = pytest.importorskip("tornado.httpclient")

View File

@@ -2,15 +2,15 @@
"""Integration tests with urllib2""" """Integration tests with urllib2"""
import ssl import ssl
from urllib.request import urlopen
from urllib.parse import urlencode from urllib.parse import urlencode
from urllib.request import urlopen
import pytest_httpbin.certs import pytest_httpbin.certs
from assertions import assert_cassette_has_one_response
# Internal imports # Internal imports
import vcr import vcr
from assertions import assert_cassette_has_one_response
def urlopen_with_cafile(*args, **kwargs): def urlopen_with_cafile(*args, **kwargs):
context = ssl.create_default_context(cafile=pytest_httpbin.certs.where()) context = ssl.create_default_context(cafile=pytest_httpbin.certs.where())

View File

@@ -4,9 +4,10 @@
import pytest import pytest
import pytest_httpbin import pytest_httpbin
from assertions import assert_cassette_empty, assert_is_json
import vcr import vcr
from vcr.patch import force_reset from vcr.patch import force_reset
from assertions import assert_cassette_empty, assert_is_json
urllib3 = pytest.importorskip("urllib3") urllib3 = pytest.importorskip("urllib3")

View File

@@ -1,9 +1,10 @@
import http.client as httplib import http.client as httplib
import multiprocessing import multiprocessing
import pytest
from xmlrpc.client import ServerProxy from xmlrpc.client import ServerProxy
from xmlrpc.server import SimpleXMLRPCServer from xmlrpc.server import SimpleXMLRPCServer
import pytest
requests = pytest.importorskip("requests") requests = pytest.importorskip("requests")
import vcr # NOQA import vcr # NOQA

View File

@@ -7,6 +7,7 @@ from unittest import mock
import pytest import pytest
import yaml import yaml
from vcr.cassette import Cassette from vcr.cassette import Cassette
from vcr.errors import UnhandledHTTPRequestError from vcr.errors import UnhandledHTTPRequestError
from vcr.patch import force_reset from vcr.patch import force_reset

View File

@@ -1,18 +1,19 @@
from io import BytesIO
from vcr.filters import (
remove_headers,
replace_headers,
remove_query_parameters,
replace_query_parameters,
remove_post_data_parameters,
replace_post_data_parameters,
decode_response,
)
from vcr.request import Request
import gzip import gzip
import json import json
from unittest import mock
import zlib import zlib
from io import BytesIO
from unittest import mock
from vcr.filters import (
decode_response,
remove_headers,
remove_post_data_parameters,
remove_query_parameters,
replace_headers,
replace_post_data_parameters,
replace_query_parameters,
)
from vcr.request import Request
def test_replace_headers(): def test_replace_headers():

View File

@@ -1,6 +1,7 @@
import pytest import pytest
from vcr.serializers.jsonserializer import serialize
from vcr.request import Request from vcr.request import Request
from vcr.serializers.jsonserializer import serialize
def test_serialize_binary(): def test_serialize_binary():

View File

@@ -3,8 +3,7 @@ from unittest import mock
import pytest import pytest
from vcr import matchers from vcr import matchers, request
from vcr import request
# the dict contains requests with corresponding to its key difference # the dict contains requests with corresponding to its key difference
# with 'base' request. # with 'base' request.

View File

@@ -1,6 +1,7 @@
import filecmp import filecmp
import json import json
import shutil import shutil
import yaml import yaml
import vcr.migration import vcr.migration

View File

@@ -1,6 +1,6 @@
import pytest import pytest
from vcr.request import Request, HeadersDict from vcr.request import HeadersDict, Request
@pytest.mark.parametrize( @pytest.mark.parametrize(

View File

@@ -5,7 +5,7 @@ import pytest
from vcr.request import Request from vcr.request import Request
from vcr.serialize import deserialize, serialize from vcr.serialize import deserialize, serialize
from vcr.serializers import yamlserializer, jsonserializer, compat from vcr.serializers import compat, jsonserializer, yamlserializer
def test_deserialize_old_yaml_cassette(): def test_deserialize_old_yaml_cassette():

View File

@@ -1,8 +1,8 @@
from unittest import mock from unittest import mock
from vcr import mode from vcr import mode
from vcr.stubs import VCRHTTPSConnection
from vcr.cassette import Cassette from vcr.cassette import Cassette
from vcr.stubs import VCRHTTPSConnection
class TestVCRConnection: class TestVCRConnection:

View File

@@ -1,13 +1,13 @@
from unittest import mock import http.client as httplib
import os import os
from unittest import mock
import pytest import pytest
import http.client as httplib
from vcr import VCR, mode, use_cassette from vcr import VCR, mode, use_cassette
from vcr.patch import _HTTPConnection, force_reset
from vcr.request import Request from vcr.request import Request
from vcr.stubs import VCRHTTPSConnection from vcr.stubs import VCRHTTPSConnection
from vcr.patch import _HTTPConnection, force_reset
def test_vcr_use_cassette(): def test_vcr_use_cassette():

View File

@@ -36,13 +36,16 @@ skipsdist = True
commands = commands =
black --version black --version
black --check --diff . black --check --diff .
isort --version
isort . --check --diff
flake8 --version flake8 --version
flake8 --exclude=./docs/conf.py,./.tox/ flake8 --exclude=./docs/conf.py,./.tox/
pyflakes ./docs/conf.py pyflakes ./docs/conf.py
deps = deps =
flake8 flake8
black black
basepython = python3.7 isort
basepython = python3.10
[testenv:docs] [testenv:docs]
# Running sphinx from inside the "docs" directory # Running sphinx from inside the "docs" directory

View File

@@ -1,6 +1,7 @@
import logging import logging
from .config import VCR
from logging import NullHandler from logging import NullHandler
from .config import VCR
from .record_mode import RecordMode as mode # noqa import is not used in this file from .record_mode import RecordMode as mode # noqa import is not used in this file
__version__ = "4.2.1" __version__ = "4.2.1"

View File

@@ -1,20 +1,20 @@
import collections import collections
import contextlib import contextlib
import copy import copy
import sys
import inspect import inspect
import logging import logging
import sys
import wrapt import wrapt
from .errors import UnhandledHTTPRequestError
from .matchers import requests_match, uri, method, get_matchers_results
from .patch import CassettePatcherBuilder
from .serializers import yamlserializer
from .persisters.filesystem import FilesystemPersister
from .util import partition_dict
from ._handle_coroutine import handle_coroutine from ._handle_coroutine import handle_coroutine
from .errors import UnhandledHTTPRequestError
from .matchers import get_matchers_results, method, requests_match, uri
from .patch import CassettePatcherBuilder
from .persisters.filesystem import FilesystemPersister
from .record_mode import RecordMode from .record_mode import RecordMode
from .serializers import yamlserializer
from .util import partition_dict
try: try:
from asyncio import iscoroutinefunction from asyncio import iscoroutinefunction

View File

@@ -1,20 +1,18 @@
import copy import copy
from collections import abc as collections_abc
import functools import functools
import inspect import inspect
import os import os
import types import types
from collections import abc as collections_abc
import six import six
from . import filters, matchers
from .cassette import Cassette from .cassette import Cassette
from .serializers import yamlserializer, jsonserializer
from .persisters.filesystem import FilesystemPersister from .persisters.filesystem import FilesystemPersister
from .util import compose, auto_decorate
from .record_mode import RecordMode from .record_mode import RecordMode
from . import matchers from .serializers import jsonserializer, yamlserializer
from . import filters from .util import auto_decorate, compose
class VCR: class VCR:

View File

@@ -1,8 +1,8 @@
from io import BytesIO
from urllib.parse import urlparse, urlencode, urlunparse
import copy import copy
import json import json
import zlib import zlib
from io import BytesIO
from urllib.parse import urlencode, urlparse, urlunparse
from .util import CaseInsensitiveDict from .util import CaseInsensitiveDict

View File

@@ -1,9 +1,9 @@
import json import json
import logging
import urllib import urllib
import xmlrpc.client import xmlrpc.client
from .util import read_body
import logging
from .util import read_body
log = logging.getLogger(__name__) log = logging.getLogger(__name__)

View File

@@ -17,11 +17,12 @@ import os
import shutil import shutil
import sys import sys
import tempfile import tempfile
import yaml import yaml
from .serializers import yamlserializer, jsonserializer
from .serialize import serialize
from . import request from . import request
from .serialize import serialize
from .serializers import jsonserializer, yamlserializer
from .stubs.compat import get_httpmessage from .stubs.compat import get_httpmessage
# Use the libYAML versions if possible # Use the libYAML versions if possible

View File

@@ -1,13 +1,12 @@
"""Utilities for patching in cassettes""" """Utilities for patching in cassettes"""
import contextlib import contextlib
import functools import functools
import http.client as httplib
import itertools import itertools
import logging
from unittest import mock from unittest import mock
from .stubs import VCRHTTPConnection, VCRHTTPSConnection from .stubs import VCRHTTPConnection, VCRHTTPSConnection
import http.client as httplib
import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
# Save some of the original types for the purposes of unpatching # Save some of the original types for the purposes of unpatching
@@ -16,7 +15,7 @@ _HTTPSConnection = httplib.HTTPSConnection
# Try to save the original types for boto3 # Try to save the original types for boto3
try: try:
from botocore.awsrequest import AWSHTTPSConnection, AWSHTTPConnection from botocore.awsrequest import AWSHTTPConnection, AWSHTTPSConnection
except ImportError: except ImportError:
try: try:
import botocore.vendored.requests.packages.urllib3.connectionpool as cpool import botocore.vendored.requests.packages.urllib3.connectionpool as cpool
@@ -269,8 +268,7 @@ class CassettePatcherBuilder:
except ImportError: # pragma: no cover except ImportError: # pragma: no cover
pass pass
else: else:
from .stubs.httplib2_stubs import VCRHTTPConnectionWithTimeout from .stubs.httplib2_stubs import VCRHTTPConnectionWithTimeout, VCRHTTPSConnectionWithTimeout
from .stubs.httplib2_stubs import VCRHTTPSConnectionWithTimeout
yield cpool, "HTTPConnectionWithTimeout", VCRHTTPConnectionWithTimeout yield cpool, "HTTPConnectionWithTimeout", VCRHTTPConnectionWithTimeout
yield cpool, "HTTPSConnectionWithTimeout", VCRHTTPSConnectionWithTimeout yield cpool, "HTTPSConnectionWithTimeout", VCRHTTPSConnectionWithTimeout

View File

@@ -1,7 +1,8 @@
# .. _persister_example: # .. _persister_example:
import os import os
from ..serialize import serialize, deserialize
from ..serialize import deserialize, serialize
class FilesystemPersister: class FilesystemPersister:

View File

@@ -1,8 +1,9 @@
import logging
import warnings import warnings
from io import BytesIO from io import BytesIO
from urllib.parse import urlparse, parse_qsl from urllib.parse import parse_qsl, urlparse
from .util import CaseInsensitiveDict from .util import CaseInsensitiveDict
import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)

View File

@@ -1,7 +1,8 @@
from vcr.serializers import compat
from vcr.request import Request
import yaml import yaml
from vcr.request import Request
from vcr.serializers import compat
# version 1 cassettes started with VCR 1.0.x. # version 1 cassettes started with VCR 1.0.x.
# Before 1.0.x, there was no versioning. # Before 1.0.x, there was no versioning.
CASSETTE_FORMAT_VERSION = 1 CASSETTE_FORMAT_VERSION = 1

View File

@@ -2,9 +2,10 @@ import yaml
# Use the libYAML versions if possible # Use the libYAML versions if possible
try: try:
from yaml import CLoader as Loader, CDumper as Dumper from yaml import CDumper as Dumper
from yaml import CLoader as Loader
except ImportError: except ImportError:
from yaml import Loader, Dumper from yaml import Dumper, Loader
def deserialize(cassette_string): def deserialize(cassette_string):

View File

@@ -1,14 +1,12 @@
"""Stubs for patching HTTP and HTTPS requests""" """Stubs for patching HTTP and HTTPS requests"""
import logging import logging
from http.client import ( from http.client import HTTPConnection, HTTPResponse, HTTPSConnection
HTTPConnection,
HTTPSConnection,
HTTPResponse,
)
from io import BytesIO from io import BytesIO
from vcr.request import Request
from vcr.errors import CannotOverwriteExistingCassetteException from vcr.errors import CannotOverwriteExistingCassetteException
from vcr.request import Request
from . import compat from . import compat
log = logging.getLogger(__name__) log = logging.getLogger(__name__)

View File

@@ -1,15 +1,14 @@
"""Stubs for aiohttp HTTP clients""" """Stubs for aiohttp HTTP clients"""
import asyncio import asyncio
import functools import functools
import logging
import json import json
import logging
from aiohttp import ClientConnectionError, ClientResponse, RequestInfo, streams
from aiohttp import hdrs, CookieJar
from http.cookies import CookieError, Morsel, SimpleCookie from http.cookies import CookieError, Morsel, SimpleCookie
from typing import Mapping, Union
from aiohttp import ClientConnectionError, ClientResponse, CookieJar, RequestInfo, hdrs, streams
from aiohttp.helpers import strip_auth_from_url from aiohttp.helpers import strip_auth_from_url
from multidict import CIMultiDict, CIMultiDictProxy, MultiDict from multidict import CIMultiDict, CIMultiDictProxy, MultiDict
from typing import Union, Mapping
from yarl import URL from yarl import URL
from vcr.errors import CannotOverwriteExistingCassetteException from vcr.errors import CannotOverwriteExistingCassetteException

View File

@@ -1,6 +1,7 @@
"""Stubs for boto""" """Stubs for boto"""
from boto.https_connection import CertValidatingHTTPSConnection from boto.https_connection import CertValidatingHTTPSConnection
from ..stubs import VCRHTTPSConnection from ..stubs import VCRHTTPSConnection

View File

@@ -1,6 +1,5 @@
from io import BytesIO
import http.client import http.client
from io import BytesIO
""" """
The python3 http.client api moved some stuff around, so this is an abstraction The python3 http.client api moved some stuff around, so this is an abstraction

View File

@@ -1,6 +1,7 @@
"""Stubs for httplib2""" """Stubs for httplib2"""
from httplib2 import HTTPConnectionWithTimeout, HTTPSConnectionWithTimeout from httplib2 import HTTPConnectionWithTimeout, HTTPSConnectionWithTimeout
from ..stubs import VCRHTTPConnection, VCRHTTPSConnection from ..stubs import VCRHTTPConnection, VCRHTTPSConnection

View File

@@ -1,11 +1,12 @@
import functools import functools
import inspect
import logging import logging
from unittest.mock import patch, MagicMock from unittest.mock import MagicMock, patch
import httpx import httpx
from vcr.request import Request as VcrRequest
from vcr.errors import CannotOverwriteExistingCassetteException from vcr.errors import CannotOverwriteExistingCassetteException
import inspect from vcr.request import Request as VcrRequest
_httpx_signature = inspect.signature(httpx.Client.request) _httpx_signature = inspect.signature(httpx.Client.request)

View File

@@ -1,6 +1,7 @@
"""Stubs for urllib3""" """Stubs for urllib3"""
from urllib3.connectionpool import HTTPConnection, VerifiedHTTPSConnection from urllib3.connectionpool import HTTPConnection, VerifiedHTTPSConnection
from ..stubs import VCRHTTPConnection, VCRHTTPSConnection from ..stubs import VCRHTTPConnection, VCRHTTPSConnection
# urllib3 defines its own HTTPConnection classes. It includes some polyfills # urllib3 defines its own HTTPConnection classes. It includes some polyfills