Skip to content

Commit 103bc89

Browse files
authored
Fixes a suspicious setattr that should be taking an Any (opensearch-project#604)
* Fix invalid value type. Signed-off-by: dblock <dblock@amazon.com> * Workaround Incompatible types in assignment (expression has type float, variable has type Double) [assignment] Signed-off-by: dblock <dblock@amazon.com> --------- Signed-off-by: dblock <dblock@amazon.com>
1 parent 7509a15 commit 103bc89

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
99
### Fixed
1010
- Fix `TypeError` on `parallel_bulk` ([#601](https://github.com/opensearch-project/opensearch-py/pull/601))
1111
- Fix Amazon OpenSearch Serverless integration with LangChain ([#603](https://github.com/opensearch-project/opensearch-py/pull/603))
12+
- Fix type of `Field.__setattr__` ([604](https://github.com/opensearch-project/opensearch-py/pull/604))
1213
### Security
1314

1415
## [2.4.1]

opensearchpy/helpers/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def __eq__(self, other: Any) -> bool:
309309
def __ne__(self, other: Any) -> bool:
310310
return not self == other
311311

312-
def __setattr__(self, name: str, value: Optional[bool]) -> None:
312+
def __setattr__(self, name: str, value: Any) -> None:
313313
if name.startswith("_"):
314314
return super(DslBase, self).__setattr__(name, value)
315315
return self._setattr(name, value)

test_opensearchpy/test_helpers/test_document.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import pickle
3333
from datetime import datetime
3434
from hashlib import sha256
35-
from typing import Any
35+
from typing import Any, Union
3636

3737
from pytest import raises
3838

@@ -648,3 +648,28 @@ class MySubDocWithNested(MyDoc):
648648
},
649649
"title": {"type": "keyword"},
650650
}
651+
652+
653+
def test_save_double(mock_client: Any) -> None:
654+
class MyDocumentWithDouble(MyDoc):
655+
a_double: Union[float, field.Double] = field.Double()
656+
657+
def save(
658+
self,
659+
using: Any = None,
660+
index: Any = None,
661+
validate: bool = True,
662+
skip_empty: bool = True,
663+
return_doc_meta: bool = False,
664+
**kwargs: Any,
665+
) -> Any:
666+
if not self.a_double:
667+
self.a_double = 3.14159265359
668+
return super().save(
669+
using, index, validate, skip_empty, return_doc_meta, **kwargs
670+
)
671+
672+
md: Any = MyDocumentWithDouble()
673+
with raises(ValidationException):
674+
md.save(using="mock")
675+
assert md.a_double == 3.14159265359

0 commit comments

Comments
 (0)