From 7a1a2d7a06d30de22cfb81fa4306db9e0d671e13 Mon Sep 17 00:00:00 2001 From: Quentin Pradet Date: Wed, 27 Mar 2024 14:59:52 +0400 Subject: [PATCH] Ensure optional dependencies are optional (#2508) (cherry picked from commit 0e6c4cd5f238fb167f458d9dfafe4ccbb3d9c224) --- elasticsearch/__init__.py | 2 +- elasticsearch/serializer.py | 2 +- noxfile.py | 7 +++++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/elasticsearch/__init__.py b/elasticsearch/__init__.py index f4ac43ca7..723b3a2b7 100644 --- a/elasticsearch/__init__.py +++ b/elasticsearch/__init__.py @@ -65,7 +65,7 @@ try: from .serializer import OrjsonSerializer -except ModuleNotFoundError: +except ImportError: OrjsonSerializer = None # type: ignore[assignment,misc] # Only raise one warning per deprecation message so as not diff --git a/elasticsearch/serializer.py b/elasticsearch/serializer.py index 4e6122fff..902fe8e09 100644 --- a/elasticsearch/serializer.py +++ b/elasticsearch/serializer.py @@ -45,7 +45,7 @@ from elastic_transport import OrjsonSerializer as _OrjsonSerializer __all__.append("OrjsonSerializer") -except ModuleNotFoundError: +except ImportError: _OrjsonSerializer = None # type: ignore[assignment,misc] diff --git a/noxfile.py b/noxfile.py index bf88249bd..c303fe26c 100644 --- a/noxfile.py +++ b/noxfile.py @@ -84,14 +84,17 @@ def format(session): @nox.session() def lint(session): - session.install("flake8", "black~=24.0", "mypy", "isort", "types-requests") + # Check that importing the client still works without optional dependencies + session.install(".", env=INSTALL_ENV) + session.run("python", "-c", "from elasticsearch import Elasticsearch") + session.run("python", "-c", "from elasticsearch._otel import OpenTelemetry") + session.install("flake8", "black~=24.0", "mypy", "isort", "types-requests") session.run("isort", "--check", "--profile=black", *SOURCE_FILES) session.run("black", "--check", *SOURCE_FILES) session.run("flake8", *SOURCE_FILES) session.run("python", "utils/license-headers.py", "check", *SOURCE_FILES) - # Workaround to make '-r' to still work despite uninstalling aiohttp below. session.install(".[async,requests,orjson]", env=INSTALL_ENV) # Run mypy on the package and then the type examples separately for