Skip to content

Commit 0165bc7

Browse files
author
Joel Collins
committed
Allow (but warn) instances in LABTHINGS_EXTENSIONS list
1 parent 1f6f627 commit 0165bc7

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/labthings/extensions.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import sys
55
import traceback
6+
import inspect
67
from importlib import util
78
from typing import Callable, Dict, List, Union
89

@@ -269,15 +270,26 @@ def find_extensions_in_file(extension_path: str, module_name="extensions") -> li
269270
else:
270271
# TODO: Add documentation links to warnings
271272
if hasattr(mod, "LABTHINGS_EXTENSIONS"):
272-
return [
273-
ext_class()
274-
for ext_class in getattr(mod, "LABTHINGS_EXTENSIONS")
275-
if issubclass(ext_class, BaseExtension)
276-
]
273+
ext_objects = []
274+
for ext_element in getattr(mod, "LABTHINGS_EXTENSIONS"):
275+
if inspect.isclass(ext_element) and issubclass(
276+
ext_element, BaseExtension
277+
):
278+
ext_objects.append(ext_element())
279+
elif isinstance(ext_element, BaseExtension):
280+
logging.warning(
281+
"%s: Extension instance passed instead of class. LABTHINGS_EXTENSIONS should contain classes, not instances.",
282+
ext_element,
283+
)
284+
ext_objects.append(ext_element)
285+
else:
286+
logging.error(
287+
"Unsupported extension type %s. Skipping.", type(ext_element)
288+
)
289+
return ext_objects
277290
elif hasattr(mod, "__extensions__"):
278291
logging.warning(
279-
"Explicit extension list using the __extensions__ global is deprecated.",
280-
"Please use LABTHINGS_EXTENSIONS instead.",
292+
"Explicit extension list using the __extensions__ global is deprecated. Please use LABTHINGS_EXTENSIONS instead."
281293
)
282294
return [
283295
getattr(mod, ext_name) for ext_name in getattr(mod, "__extensions__")

0 commit comments

Comments
 (0)