Skip to content

Commit

Permalink
Support Python 3.6+
Browse files Browse the repository at this point in the history
  • Loading branch information
pquentin committed Oct 30, 2023
1 parent e13c862 commit 796b45f
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ steps:
matrix:
setup:
python:
- "2.7"
- "3.6"
- "3.7"
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "3.12"
connection:
- "urllib3"
- "requests"
Expand Down
24 changes: 8 additions & 16 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,18 @@ sphinx
jinja2
python-dateutil

# Testing the 'search_mvt' API response
mapbox-vector-tile; python_version!="3.4.*"
# For mapbox-vector-tile, package broke Python 2 support without an annotation.
# See: protocolbuffers/protobuf#8984
protobuf<3.18; python_version!="3.4.*"
mapbox-vector-tile

# No wheels for Python 3.10 yet!
numpy; python_version<"3.10"
pandas; python_version<"3.10"
numpy
pandas

# PyYAML 5.3 dropped support for Python 3.4 while
# not amending that requirement to the package. :(
pyyaml>=5.4; python_version>="3.6"
pyyaml<5.3; python_version<"3.6"
pyyaml>=5.4

isort
black; python_version>="3.6"
black
twine

# Requirements for testing [async] extra
aiohttp; python_version>="3.6"
pytest-asyncio; python_version>="3.6"
unasync; python_version>="3.6"
aiohttp
pytest-asyncio
unasync
14 changes: 0 additions & 14 deletions elasticsearch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@
]

try:
# Asyncio only supported on Python 3.6+
if sys.version_info < (3, 6):
raise ImportError

from ._async.client import AsyncElasticsearch
from ._async.http_aiohttp import AIOHttpConnection, AsyncConnection
from ._async.transport import AsyncTransport
Expand All @@ -104,13 +100,3 @@
]
except (ImportError, SyntaxError):
pass

# Python earlier than 3.6 is deprecated and will be removed in 8.0.0
if sys.version_info < (3, 6):
warnings.warn(
"Support for Python 3.5 and earlier is deprecated and will be removed "
"in v8.0.0 (current instance is Python %d.%d) See https://github.com/elastic"
"/elasticsearch-py/issues/1696 for details." % sys.version_info[:2],
category=DeprecationWarning,
stacklevel=2,
)
58 changes: 14 additions & 44 deletions elasticsearch/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,28 @@

import sys

PY2 = sys.version_info[0] == 2
string_types = str, bytes
from urllib.parse import quote, quote_plus, unquote, urlencode, urlparse

if PY2:
string_types = (basestring,) # noqa: F821
from itertools import imap as map
from urllib import quote, quote_plus, unquote, urlencode
map = map
from queue import Queue

from Queue import Queue
from urlparse import urlparse

def to_str(x, encoding="ascii"):
if not isinstance(x, str):
return x.encode(encoding)
return x
def to_str(x, encoding="ascii"):
if not isinstance(x, str):
return x.decode(encoding)
return x

to_bytes = to_str

else:
string_types = str, bytes
from urllib.parse import quote, quote_plus, unquote, urlencode, urlparse
def to_bytes(x, encoding="ascii"):
if not isinstance(x, bytes):
return x.encode(encoding)
return x

map = map
from queue import Queue

def to_str(x, encoding="ascii"):
if not isinstance(x, str):
return x.decode(encoding)
return x
from collections.abc import Mapping

def to_bytes(x, encoding="ascii"):
if not isinstance(x, bytes):
return x.encode(encoding)
return x


try:
from collections.abc import Mapping
except ImportError:
from collections import Mapping


try:
reraise_exceptions = (RecursionError,)
except NameError:
reraise_exceptions = ()

try:
import asyncio

reraise_exceptions += (asyncio.CancelledError,)
except (ImportError, AttributeError):
pass
reraise_exceptions = (RecursionError, asyncio.CancelledError)

try:
from threading import Lock
Expand Down
22 changes: 5 additions & 17 deletions elasticsearch/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import sys

from .._async.helpers import async_bulk, async_reindex, async_scan, async_streaming_bulk
from .actions import (
_chunk_actions,
_process_bulk_chunk,
Expand All @@ -40,21 +41,8 @@
"reindex",
"_chunk_actions",
"_process_bulk_chunk",
"async_scan",
"async_bulk",
"async_reindex",
"async_streaming_bulk",
]


try:
# Asyncio only supported on Python 3.6+
if sys.version_info < (3, 6):
raise ImportError

from .._async.helpers import (
async_bulk,
async_reindex,
async_scan,
async_streaming_bulk,
)

__all__ += ["async_scan", "async_bulk", "async_reindex", "async_streaming_bulk"]
except (ImportError, SyntaxError):
pass
16 changes: 4 additions & 12 deletions elasticsearch/helpers/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

import sys

from .._async.helpers import async_bulk as async_bulk
from .._async.helpers import async_reindex as async_reindex
from .._async.helpers import async_scan as async_scan
from .._async.helpers import async_streaming_bulk as async_streaming_bulk
from .actions import _chunk_actions as _chunk_actions
from .actions import _process_bulk_chunk as _process_bulk_chunk
from .actions import bulk as bulk
Expand All @@ -27,15 +31,3 @@ from .actions import scan as scan
from .actions import streaming_bulk as streaming_bulk
from .errors import BulkIndexError as BulkIndexError
from .errors import ScanError as ScanError

try:
# Asyncio only supported on Python 3.6+
if sys.version_info < (3, 6):
raise ImportError

from .._async.helpers import async_bulk as async_bulk
from .._async.helpers import async_reindex as async_reindex
from .._async.helpers import async_scan as async_scan
from .._async.helpers import async_streaming_bulk as async_streaming_bulk
except (ImportError, SyntaxError):
pass
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
)


@nox.session(python=["2.7", "3.4", "3.5", "3.6", "3.7", "3.8", "3.9"])
@nox.session(python=["3.6", "3.7", "3.8", "3.9", "3.10", "3.11", "3.12"])
def test(session):
session.install(".")
session.install("-r", "dev-requirements.txt")
Expand Down
9 changes: 4 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,18 @@
"Intended Audience :: Developers",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
],
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4",
python_requires=">=3.6",
install_requires=install_requires,
test_suite="test_elasticsearch.run_tests.run_all",
tests_require=tests_require,
Expand Down
4 changes: 0 additions & 4 deletions test_elasticsearch/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ def run_all(argv=None):
]

ignores = []
# Python 3.6+ is required for async
if sys.version_info < (3, 6):
ignores.append("test_elasticsearch/test_async/")

# GitHub Actions, run non-server tests
if "GITHUB_ACTION" in environ:
ignores.extend(
Expand Down
7 changes: 0 additions & 7 deletions test_elasticsearch/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from urllib3._collections import HTTPHeaderDict

from elasticsearch import Elasticsearch, __versionstr__
from elasticsearch.compat import reraise_exceptions
from elasticsearch.connection import (
Connection,
RequestsHttpConnection,
Expand Down Expand Up @@ -445,9 +444,6 @@ def test_surrogatepass_into_bytes(self):
status, headers, data = con.perform_request("GET", "/")
self.assertEqual(u"你好\uda6a", data)

@pytest.mark.skipif(
not reraise_exceptions, reason="RecursionError isn't defined in Python <3.5"
)
def test_recursion_error_reraised(self):
conn = Urllib3HttpConnection()

Expand Down Expand Up @@ -875,9 +871,6 @@ def test_surrogatepass_into_bytes(self):
status, headers, data = con.perform_request("GET", "/")
self.assertEqual(u"你好\uda6a", data)

@pytest.mark.skipif(
not reraise_exceptions, reason="RecursionError isn't defined in Python <3.5"
)
def test_recursion_error_reraised(self):
conn = RequestsHttpConnection()

Expand Down
22 changes: 0 additions & 22 deletions test_elasticsearch/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,8 @@
import elasticsearch


@pytest.mark.skipif(sys.version_info < (3, 6), reason="Requires Python 3.6+")
def test_no_deprecation_python3_6_and_later():
with warnings.catch_warnings(record=True) as w:
importlib.reload(elasticsearch)

assert len(w) == 0


@pytest.mark.skipif(sys.version_info >= (3, 6), reason="Requires Python <3.6")
def test_deprecated_python3_5_and_earlier():

try: # Python 3.4+
import imp

reload = imp.reload
except ImportError: # Python 2.7
reload = reload

with pytest.warns(DeprecationWarning) as w:
reload(elasticsearch)

assert len(w) == 1
assert str(w[0].message) == (
"Support for Python 3.5 and earlier is deprecated and will be removed "
"in v8.0.0 (current instance is Python %d.%d) See https://github.com/"
"elastic/elasticsearch-py/issues/1696 for details." % (sys.version_info[:2])
)

0 comments on commit 796b45f

Please sign in to comment.