Skip to content

Commit

Permalink
review imports for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Mar 20, 2024
1 parent 3690105 commit 0184a77
Show file tree
Hide file tree
Showing 18 changed files with 126 additions and 86 deletions.
20 changes: 15 additions & 5 deletions elasticsearch_dsl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
from . import connections
from .aggs import A
from .analysis import analyzer, char_filter, normalizer, token_filter, tokenizer
from .document import Document, InnerDoc, MetaField
from .document import AsyncDocument, Document
from .document_base import InnerDoc, MetaField
from .exceptions import (
ElasticsearchDslException,
IllegalOperation,
UnknownDslObject,
ValidationException,
)
from .faceted_search import (
AsyncFacetedSearch,
DateHistogramFacet,
Facet,
FacetedResponse,
Expand Down Expand Up @@ -76,11 +78,11 @@
construct_field,
)
from .function import SF
from .index import Index, IndexTemplate
from .mapping import Mapping
from .index import AsyncIndex, AsyncIndexTemplate, Index, IndexTemplate
from .mapping import AsyncMapping, Mapping
from .query import Q
from .search import MultiSearch, Search
from .update_by_query import UpdateByQuery
from .search import AsyncMultiSearch, AsyncSearch, MultiSearch, Search
from .update_by_query import AsyncUpdateByQuery, UpdateByQuery
from .utils import AttrDict, AttrList, DslBase
from .wrappers import Range

Expand All @@ -89,6 +91,14 @@
__versionstr__ = ".".join(map(str, VERSION))
__all__ = [
"A",
"AsyncDocument",
"AsyncFacetedSearch",
"AsyncIndex",
"AsyncIndexTemplate",
"AsyncMapping",
"AsyncMultiSearch",
"AsyncSearch",
"AsyncUpdateByQuery",
"AttrDict",
"AttrList",
"Binary",
Expand Down
1 change: 0 additions & 1 deletion elasticsearch_dsl/_async/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

from .._async.index import AsyncIndex
from ..async_connections import get_connection
from ..document_base import * # noqa: F401, F403
from ..document_base import DocumentBase, DocumentMeta
from ..exceptions import IllegalOperation
from ..utils import DOC_META_FIELDS, META_FIELDS, merge
Expand Down
1 change: 0 additions & 1 deletion elasticsearch_dsl/_sync/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

from .._sync.index import Index
from ..connections import get_connection
from ..document_base import * # noqa: F401, F403
from ..document_base import DocumentBase, DocumentMeta
from ..exceptions import IllegalOperation
from ..utils import DOC_META_FIELDS, META_FIELDS, merge
Expand Down
4 changes: 3 additions & 1 deletion elasticsearch_dsl/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
# specific language governing permissions and limitations
# under the License.

from elasticsearch_dsl._sync.document import Document, InnerDoc, MetaField # noqa: F401
from elasticsearch_dsl._async.document import AsyncDocument # noqa: F401
from elasticsearch_dsl._sync.document import Document # noqa: F401
from elasticsearch_dsl.document_base import InnerDoc, MetaField # noqa: F401
1 change: 1 addition & 0 deletions elasticsearch_dsl/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
# specific language governing permissions and limitations
# under the License.

from ._async.index import AsyncIndex, AsyncIndexTemplate # noqa: F401
from ._sync.index import Index, IndexTemplate # noqa: F401
1 change: 1 addition & 0 deletions elasticsearch_dsl/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
# specific language governing permissions and limitations
# under the License.

from elasticsearch_dsl._async.mapping import AsyncMapping # noqa: F401
from elasticsearch_dsl._sync.mapping import Mapping # noqa: F401
1 change: 1 addition & 0 deletions elasticsearch_dsl/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
# specific language governing permissions and limitations
# under the License.

from elasticsearch_dsl._async.search import AsyncMultiSearch, AsyncSearch # noqa: F401
from elasticsearch_dsl._sync.search import MultiSearch, Search # noqa: F401
from elasticsearch_dsl.search_base import Q # noqa: F401
1 change: 1 addition & 0 deletions elasticsearch_dsl/update_by_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
# specific language governing permissions and limitations
# under the License.

from ._async.update_by_query import AsyncUpdateByQuery # noqa: F401
from ._sync.update_by_query import UpdateByQuery # noqa: F401
69 changes: 39 additions & 30 deletions tests/_async/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,25 @@

from pytest import raises

from elasticsearch_dsl import Index, InnerDoc, Mapping, Range, analyzer, field, utils
from elasticsearch_dsl._async import document
from elasticsearch_dsl import (
AsyncDocument,
Index,
InnerDoc,
Mapping,
MetaField,
Range,
analyzer,
field,
utils,
)
from elasticsearch_dsl.exceptions import IllegalOperation, ValidationException


class MyInner(InnerDoc):
old_field = field.Text()


class MyDoc(document.AsyncDocument):
class MyDoc(AsyncDocument):
title = field.Keyword()
name = field.Text()
created_at = field.Date()
Expand All @@ -46,27 +55,27 @@ class Index:
name = "default-index"


class MyDoc2(document.AsyncDocument):
class MyDoc2(AsyncDocument):
extra = field.Long()


class MyMultiSubDoc(MyDoc2, MySubDoc):
pass


class Comment(document.InnerDoc):
class Comment(InnerDoc):
title = field.Text()
tags = field.Keyword(multi=True)


class DocWithNested(document.AsyncDocument):
class DocWithNested(AsyncDocument):
comments = field.Nested(Comment)

class Index:
name = "test-doc-with-nested"


class SimpleCommit(document.AsyncDocument):
class SimpleCommit(AsyncDocument):
files = field.Text(multi=True)

class Index:
Expand All @@ -89,36 +98,36 @@ def _deserialize(self, data):
return Secret(codecs.decode(data, "rot_13"))


class SecretDoc(document.AsyncDocument):
class SecretDoc(AsyncDocument):
title = SecretField(index="no")

class Index:
name = "test-secret-doc"


class NestedSecret(document.AsyncDocument):
class NestedSecret(AsyncDocument):
secrets = field.Nested(SecretDoc)

class Index:
name = "test-nested-secret"


class OptionalObjectWithRequiredField(document.AsyncDocument):
class OptionalObjectWithRequiredField(AsyncDocument):
comments = field.Nested(properties={"title": field.Keyword(required=True)})

class Index:
name = "test-required"


class Host(document.AsyncDocument):
class Host(AsyncDocument):
ip = field.Ip()

class Index:
name = "test-host"


def test_range_serializes_properly():
class D(document.AsyncDocument):
class D(AsyncDocument):
lr = field.LongRange()

d = D(lr=Range(lt=42))
Expand All @@ -131,7 +140,7 @@ class D(document.AsyncDocument):


def test_range_deserializes_properly():
class D(document.InnerDoc):
class D(InnerDoc):
lr = field.LongRange()

d = D.from_es({"lr": {"lt": 42}}, True)
Expand All @@ -147,10 +156,10 @@ def test_resolve_nested():


def test_conflicting_mapping_raises_error_in_index_to_dict():
class A(document.AsyncDocument):
class A(AsyncDocument):
name = field.Text()

class B(document.AsyncDocument):
class B(AsyncDocument):
name = field.Keyword()

i = Index("i")
Expand All @@ -173,15 +182,15 @@ def test_matches_uses_index():


def test_matches_with_no_name_always_matches():
class D(document.AsyncDocument):
class D(AsyncDocument):
pass

assert D._matches({})
assert D._matches({"_index": "whatever"})


def test_matches_accepts_wildcards():
class MyDoc(document.AsyncDocument):
class MyDoc(AsyncDocument):
class Index:
name = "my-*"

Expand Down Expand Up @@ -339,14 +348,14 @@ def test_meta_is_accessible_even_on_empty_doc():


def test_meta_field_mapping():
class User(document.AsyncDocument):
class User(AsyncDocument):
username = field.Text()

class Meta:
all = document.MetaField(enabled=False)
_index = document.MetaField(enabled=True)
dynamic = document.MetaField("strict")
dynamic_templates = document.MetaField([42])
all = MetaField(enabled=False)
_index = MetaField(enabled=True)
dynamic = MetaField("strict")
dynamic_templates = MetaField([42])

assert {
"properties": {"username": {"type": "text"}},
Expand All @@ -358,7 +367,7 @@ class Meta:


def test_multi_value_fields():
class Blog(document.AsyncDocument):
class Blog(AsyncDocument):
tags = field.Keyword(multi=True)

b = Blog()
Expand All @@ -369,7 +378,7 @@ class Blog(document.AsyncDocument):


def test_docs_with_properties():
class User(document.AsyncDocument):
class User(AsyncDocument):
pwd_hash = field.Text()

def check_password(self, pwd):
Expand Down Expand Up @@ -441,7 +450,7 @@ def test_to_dict_ignores_empty_collections():


def test_declarative_mapping_definition():
assert issubclass(MyDoc, document.AsyncDocument)
assert issubclass(MyDoc, AsyncDocument)
assert hasattr(MyDoc, "_doc_type")
assert {
"properties": {
Expand All @@ -454,7 +463,7 @@ def test_declarative_mapping_definition():


def test_you_can_supply_own_mapping_instance():
class MyD(document.AsyncDocument):
class MyD(AsyncDocument):
title = field.Text()

class Meta:
Expand Down Expand Up @@ -497,7 +506,7 @@ def test_invalid_date_will_raise_exception():

def test_document_inheritance():
assert issubclass(MySubDoc, MyDoc)
assert issubclass(MySubDoc, document.AsyncDocument)
assert issubclass(MySubDoc, AsyncDocument)
assert hasattr(MySubDoc, "_doc_type")
assert {
"properties": {
Expand All @@ -510,7 +519,7 @@ def test_document_inheritance():


def test_child_class_can_override_parent():
class A(document.AsyncDocument):
class A(AsyncDocument):
o = field.Object(dynamic=False, properties={"a": field.Text()})

class B(A):
Expand Down Expand Up @@ -540,7 +549,7 @@ def test_meta_fields_are_stored_in_meta_and_ignored_by_to_dict():
def test_index_inheritance():
assert issubclass(MyMultiSubDoc, MySubDoc)
assert issubclass(MyMultiSubDoc, MyDoc2)
assert issubclass(MyMultiSubDoc, document.AsyncDocument)
assert issubclass(MyMultiSubDoc, AsyncDocument)
assert hasattr(MyMultiSubDoc, "_doc_type")
assert hasattr(MyMultiSubDoc, "_index")
assert {
Expand Down Expand Up @@ -601,7 +610,7 @@ def test_from_es_respects_underscored_non_meta_fields():
},
}

class Company(document.AsyncDocument):
class Company(AsyncDocument):
class Index:
name = "test-company"

Expand Down
11 changes: 8 additions & 3 deletions tests/_async/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@

from pytest import raises

from elasticsearch_dsl import Date, Text, analyzer
from elasticsearch_dsl._async.document import AsyncDocument
from elasticsearch_dsl._async.index import AsyncIndex, AsyncIndexTemplate
from elasticsearch_dsl import (
AsyncDocument,
AsyncIndex,
AsyncIndexTemplate,
Date,
Text,
analyzer,
)


class Post(AsyncDocument):
Expand Down
3 changes: 1 addition & 2 deletions tests/_async/test_update_by_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

from copy import deepcopy

from elasticsearch_dsl import Q
from elasticsearch_dsl._async.update_by_query import AsyncUpdateByQuery
from elasticsearch_dsl import AsyncUpdateByQuery, Q
from elasticsearch_dsl.response import UpdateByQueryResponse


Expand Down
Loading

0 comments on commit 0184a77

Please sign in to comment.