From 9f75543029336a7e506badd3b30cfcdfa0e44598 Mon Sep 17 00:00:00 2001 From: xuhaidong Date: Thu, 6 Jun 2024 17:54:47 +0800 Subject: [PATCH] feat: guarantee extension order --- sea/app.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sea/app.py b/sea/app.py index c7231e2..c1065b7 100644 --- a/sea/app.py +++ b/sea/app.py @@ -137,7 +137,15 @@ def load_extensions_in_module(self, module): def is_ext(ins): return not inspect.isclass(ins) and hasattr(ins, "init_app") - for n, ext in inspect.getmembers(module, is_ext): + # guarantee the order of extension initialization by load_order attribute + def sort_ext(member): + _, ext = member + DEFAULT_LOWER_ORDER = 10 + return getattr(ext, "load_order", DEFAULT_LOWER_ORDER) + + for n, ext in sorted( + inspect.getmembers(module, is_ext), sort_ext + ): self._register_extension(n, ext) return self.extensions