From f253eb2359b12a4fd9f6c3c4cead724c637cfb38 Mon Sep 17 00:00:00 2001 From: Phil Chiu Date: Tue, 21 Dec 2021 21:22:34 -0700 Subject: [PATCH 1/2] Add fields to EmbeddedDocument.__dir__() --- tests/test_document.py | 7 +++++++ umongo/embedded_document.py | 3 +++ 2 files changed, 10 insertions(+) diff --git a/tests/test_document.py b/tests/test_document.py index b00c0fb0..fb268a9a 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -126,6 +126,13 @@ def test_fields_by_items(self): with pytest.raises(KeyError): del john['missing'] + def test_dir(self): + john = self.Student.build_from_mongo(data={ + 'name': 'John Doe', 'birthday': dt.datetime(1995, 12, 12), 'gpa': 3.0}) + assert 'name' in dir(john) + assert 'birthday' in dir(john) + assert 'gpa' in dir(john) + def test_property(self): @self.instance.register class HeavyStudent(BaseStudent): diff --git a/umongo/embedded_document.py b/umongo/embedded_document.py index a6d9ab0d..b47c9ed6 100644 --- a/umongo/embedded_document.py +++ b/umongo/embedded_document.py @@ -172,6 +172,9 @@ def __setattr__(self, name, value): else: super().__setattr__(name, value) + def __dir__(self): + return dir(type(self)) + list(self._fields) + def __getattr__(self, name): if name in self._fields: value = self._data.get(name) From c03e0d97a08e9cb9ec56ccb3a5dd96ab2caf8adc Mon Sep 17 00:00:00 2001 From: Phil Chiu Date: Wed, 22 Dec 2021 07:21:30 -0700 Subject: [PATCH 2/2] Change function order --- umongo/embedded_document.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/umongo/embedded_document.py b/umongo/embedded_document.py index b47c9ed6..25b92b54 100644 --- a/umongo/embedded_document.py +++ b/umongo/embedded_document.py @@ -172,9 +172,6 @@ def __setattr__(self, name, value): else: super().__setattr__(name, value) - def __dir__(self): - return dir(type(self)) + list(self._fields) - def __getattr__(self, name): if name in self._fields: value = self._data.get(name) @@ -186,3 +183,6 @@ def __delattr__(self, name): self._data.delete(name) else: super().__delattr__(name) + + def __dir__(self): + return dir(type(self)) + list(self._fields)