Skip to content

Commit 933b781

Browse files
committed
feat(async): make base object classes async compatible
Basic principle is that we won't use constructor to create object, instead classmethod would be used that return either object or corotine that returns object
1 parent 46c0369 commit 933b781

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

gitlab/base.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# You should have received a copy of the GNU Lesser General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

18+
import asyncio
1819
import importlib
1920

2021

@@ -43,6 +44,18 @@ def __init__(self, manager, attrs):
4344
self.__dict__["_parent_attrs"] = self.manager.parent_attrs
4445
self._create_managers()
4546

47+
@classmethod
48+
def create(cls, manager, attrs):
49+
if asyncio.iscoroutine(attrs):
50+
return cls._acreate(manager, attrs)
51+
else:
52+
return cls(manager, attrs)
53+
54+
@classmethod
55+
async def _acreate(cls, manager, attrs):
56+
attrs = await attrs
57+
return cls(manager, attrs)
58+
4659
def __getstate__(self):
4760
state = self.__dict__.copy()
4861
module = state.pop("_module")
@@ -174,6 +187,18 @@ def __init__(self, manager, obj_cls, _list):
174187
self._obj_cls = obj_cls
175188
self._list = _list
176189

190+
@classmethod
191+
def create(cls, manager, obj_cls, _list):
192+
if asyncio.iscoroutine(_list):
193+
return cls._acreate(manager, obj_cls, _list)
194+
else:
195+
return cls(manager, obj_cls, _list)
196+
197+
@classmethod
198+
async def _from_coroutine(cls, manager, obj_cls, _list):
199+
_list = await _list
200+
return cls(manager, obj_cls, _list)
201+
177202
def __len__(self):
178203
return len(self._list)
179204

0 commit comments

Comments
 (0)