Repository: elastic/elasticsearch-dsl-py

Issue:
Expand__to_dot variable cannot be used to control the behavior
Hello,

This pull request: https://github.com/elastic/elasticsearch-dsl-py/pull/809 moved the default for _expand__to_dot to a variable.
At load time, the default for _expand__to_dot will already be set to True. Overwriting EXPAND__TO_DOT after the import has not impact.

The variable would be usable if the default is resolved in the function definition.

Discussion:
miguelgrinberg: Duplicate of #1587 

Patch:
diff --git a/elasticsearch_dsl/utils.py b/elasticsearch_dsl/utils.py
index c362838..3b231d2 100644
--- a/elasticsearch_dsl/utils.py
+++ b/elasticsearch_dsl/utils.py
@@ -252,7 +252,9 @@ class DslBase(metaclass=DslMeta):
                 f"DSL class `{name}` does not exist in {cls._type_name}."
             )
 
-    def __init__(self, _expand__to_dot=EXPAND__TO_DOT, **params):
+    def __init__(self, _expand__to_dot=None, **params):
+        if _expand__to_dot is None:
+            _expand__to_dot = EXPAND__TO_DOT
         self._params = {}
         for pname, pvalue in params.items():
             if "__" in pname and _expand__to_dot:


Test patch:
diff --git a/tests/test_query.py b/tests/test_query.py
index 4b76503..dde4963 100644
--- a/tests/test_query.py
+++ b/tests/test_query.py
@@ -17,7 +17,7 @@
 
 from pytest import raises
 
-from elasticsearch_dsl import function, query
+from elasticsearch_dsl import function, query, utils
 
 
 def test_empty_Q_is_match_all():
@@ -581,3 +581,12 @@ def test_script_score():
     assert isinstance(q.query, query.MatchAll)
     assert q.script == {"source": "...", "params": {}}
     assert q.to_dict() == d
+
+
+def test_expand_double_underscore_to_dot_setting():
+    q = query.Term(comment__count=2)
+    assert q.to_dict() == {"term": {"comment.count": 2}}
+    utils.EXPAND__TO_DOT = False
+    q = query.Term(comment__count=2)
+    assert q.to_dict() == {"term": {"comment__count": 2}}
+    utils.EXPAND__TO_DOT = True