Skip to content

Fix importing async classes #1

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
Jan 25, 2021
Merged

Fix importing async classes #1

merged 2 commits into from
Jan 25, 2021

Conversation

clmnin
Copy link

@clmnin clmnin commented Jan 22, 2021

Previously we were unable to import AsyncSearch, AsyncMultiSearch, AsyncUpdateByQuery because the import of AsyncMapping was failing.

# elasticsearch_dsl/__init__.py
try:
    from .document import AsyncDocument  # noqa: F401
    from .faceted_search import AsyncFacetedSearch  # noqa: F401
    from .index import AsyncIndex, AsyncIndexTemplate  # noqa: F401
    
    # fails to import AsyncMapping
    from .mapping import AsyncMapping  # noqa: F401
    from .search import AsyncMultiSearch, AsyncSearch  # noqa: F401
    from .update_by_query import AsyncUpdateByQuery  # noqa: F401

resulting failing imports

# this fails
from elasticsearch_dsl import AsyncSearch

# ERROR: AsyncSearch not found in elasticsearch_dsl/__init__.py

After this small fix we can successfully import every class except AsyncMapping (as it is not implemented)

As @sethmlarson wishes to deprecate Mapping (and never implement AsyncMapping) in 7.x and remove it in 8.x so we might as well remove the import for AsyncMapping, but after @sethmlarson approves this change.

	Previously we were unable to import AsyncSearch,
	AsyncMultiSearch, AsyncUpdateByQuery because the import
	of AsyncMapping was failing.

	As @sethmlarson wishes to deprecate Mapping (and never implement
	AsyncMapping) in 7.x and remove it in 8.x so we might
	as well remove the import for AsyncMapping, but after @sethmlarson
	approves this change.
@clmnin
Copy link
Author

clmnin commented Jan 23, 2021

The linting error (isort) are lines of code I hadn't changed.

diff --git a/elasticsearch_dsl/__init__.py b/elasticsearch_dsl/__init__.py
index 26fe8aa..c258341 100644
--- a/elasticsearch_dsl/__init__.py
+++ b/elasticsearch_dsl/__init__.py
@@ -17,64 +17,21 @@
 
 from . import connections
 from .aggs import A
-from .analysis import analyzer, char_filter, normalizer, token_filter, tokenizer
+from .analysis import (analyzer, char_filter, normalizer, token_filter,
+                       tokenizer)
 from .document import Document, InnerDoc, MetaField
-from .exceptions import (
-    ElasticsearchDslException,
-    IllegalOperation,
-    UnknownDslObject,
-    ValidationException,
-)
-from .faceted_search import (
-    DateHistogramFacet,
-    Facet,
-    FacetedResponse,
-    FacetedSearch,
-    HistogramFacet,
-    NestedFacet,
-    RangeFacet,
-    TermsFacet,
-)
-from .field import (
-    Binary,
-    Boolean,
-    Byte,
-    Completion,
-    CustomField,
-    Date,
-    DateRange,
-    DenseVector,
-    Double,
-    DoubleRange,
-    Field,
-    Float,
-    FloatRange,
-    GeoPoint,
-    GeoShape,
-    HalfFloat,
-    Integer,
-    IntegerRange,
-    Ip,
-    IpRange,
-    Join,
-    Keyword,
-    Long,
-    LongRange,
-    Murmur3,
-    Nested,
-    Object,
-    Percolator,
-    RangeField,
-    RankFeature,
-    RankFeatures,
-    ScaledFloat,
-    SearchAsYouType,
-    Short,
-    SparseVector,
-    Text,
-    TokenCount,
-    construct_field,
-)
+from .exceptions import (ElasticsearchDslException, IllegalOperation,
+                         UnknownDslObject, ValidationException)
+from .faceted_search import (DateHistogramFacet, Facet, FacetedResponse,
+                             FacetedSearch, HistogramFacet, NestedFacet,
+                             RangeFacet, TermsFacet)
+from .field import (Binary, Boolean, Byte, Completion, CustomField, Date,
+                    DateRange, DenseVector, Double, DoubleRange, Field, Float,
+                    FloatRange, GeoPoint, GeoShape, HalfFloat, Integer,
+                    IntegerRange, Ip, IpRange, Join, Keyword, Long, LongRange,
+                    Murmur3, Nested, Object, Percolator, RangeField,
+                    RankFeature, RankFeatures, ScaledFloat, SearchAsYouType,
+                    Short, SparseVector, Text, TokenCount, construct_field)
 from .function import SF
 from .index import Index, IndexTemplate
 from .mapping import Mapping

Should I run isort elasticsearch_dsl/__init__.py and push again?

Copy link
Owner

@sethmlarson sethmlarson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment for you, I appreciate you helping me out :)

from .search import AsyncMultiSearch, AsyncSearch # noqa: F401
from .update_by_query import AsyncUpdateByQuery # noqa: F401
from .mapping import AsyncMapping # noqa: F401
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since AsyncMapping doesn't exist you can remove this import.

@@ -33,7 +33,7 @@
]

try:
from ._sync import AsyncMultiSearch, AsyncSearch # noqa: F401
from ._async import AsyncMultiSearch, AsyncSearch # noqa: F401
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch here, thank you.

@sethmlarson
Copy link
Owner

sethmlarson commented Jan 23, 2021

I use isort --profile=black in order to make sure that our auto-formatter is also happy with the import structure. The config for it is in setup.cfg.

@clmnin
Copy link
Author

clmnin commented Jan 23, 2021

One comment for you, I appreciate you helping me out :)

No problem. I wanted to use async elasticsearch_dsl anyway. Thank you for maintaining the package

Copy link
Owner

@sethmlarson sethmlarson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@sethmlarson sethmlarson merged commit 9eff645 into sethmlarson:async-io Jan 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants