You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by ak...@apache.org on 2024/03/21 07:42:24 UTC
(buildstream) 02/14: _pluginfactory: Additional type checking
This is an automated email from the ASF dual-hosted git repository.
akitouni pushed a commit to branch abderrahim/buildstream-mirrors-merge
in repository https://gitbox.apache.org/repos/asf/buildstream.git
commit 0c91eea6269422d4aab34377b9d92a8f474bf9d1
Author: Tristan van Berkom <tr...@codethink.co.uk>
AuthorDate: Mon Oct 2 14:21:18 2023 +0900
_pluginfactory: Additional type checking
This also fixes an incorrect type signature in the element constructor
---
src/buildstream/_pluginfactory/elementfactory.py | 16 ++++++++++++----
src/buildstream/_pluginfactory/pluginfactory.py | 8 ++++----
src/buildstream/_pluginfactory/sourcefactory.py | 18 ++++++++++++++----
src/buildstream/element.py | 6 +++---
4 files changed, 33 insertions(+), 15 deletions(-)
diff --git a/src/buildstream/_pluginfactory/elementfactory.py b/src/buildstream/_pluginfactory/elementfactory.py
index b85b177e9..cc95273b0 100644
--- a/src/buildstream/_pluginfactory/elementfactory.py
+++ b/src/buildstream/_pluginfactory/elementfactory.py
@@ -14,8 +14,16 @@
# Authors:
# Tristan Van Berkom <tr...@codethink.co.uk>
+from typing import TYPE_CHECKING, Type, cast
+
from .pluginfactory import PluginFactory
from .pluginorigin import PluginType
+from .._loader import LoadElement
+from ..element import Element
+
+if TYPE_CHECKING:
+ from .._context import Context
+ from .._project import Project
# A ElementFactory creates Element instances
@@ -30,8 +38,7 @@ class ElementFactory(PluginFactory):
# create():
#
- # Create an Element object, the pipeline uses this to create Element
- # objects on demand for a given pipeline.
+ # Create an Element object.
#
# Args:
# context (object): The Context object for processing
@@ -44,7 +51,8 @@ class ElementFactory(PluginFactory):
# PluginError (if the kind lookup failed)
# LoadError (if the element itself took issue with the config)
#
- def create(self, context, project, load_element):
- element_type, default_config = self.lookup(context.messenger, load_element.kind, load_element.node)
+ def create(self, context: "Context", project: "Project", load_element: LoadElement) -> Element:
+ plugin_type, default_config = self.lookup(context.messenger, load_element.kind, load_element.node)
+ element_type = cast(Type[Element], plugin_type)
element = element_type(context, project, load_element, default_config)
return element
diff --git a/src/buildstream/_pluginfactory/pluginfactory.py b/src/buildstream/_pluginfactory/pluginfactory.py
index 23de60936..fbcf544cc 100644
--- a/src/buildstream/_pluginfactory/pluginfactory.py
+++ b/src/buildstream/_pluginfactory/pluginfactory.py
@@ -15,7 +15,7 @@
# Tristan Van Berkom <tr...@codethink.co.uk>
import os
-from typing import Tuple, Type, Iterator
+from typing import Tuple, Type, Iterator, Optional
from pluginbase import PluginSource
from .. import utils
@@ -127,7 +127,7 @@ class PluginFactory:
#
# Raises: PluginError
#
- def lookup(self, messenger: Messenger, kind: str, provenance_node: Node) -> Tuple[Type[Plugin], str]:
+ def lookup(self, messenger: Messenger, kind: str, provenance_node: Node) -> Tuple[Type[Plugin], Optional[str]]:
plugin_type, defaults = self._ensure_plugin(kind, provenance_node)
# We can be called with None for the messenger here in the
@@ -180,7 +180,7 @@ class PluginFactory:
# the plugin's preferred defaults.
# (str): The explanatory display string describing how this plugin was loaded
#
- def get_plugin_paths(self, kind: str):
+ def get_plugin_paths(self, kind: str) -> Tuple[Optional[str], Optional[str], Optional[str]]:
try:
origin = self._origins[kind]
except KeyError:
@@ -208,7 +208,7 @@ class PluginFactory:
# Raises:
# (PluginError): In case something went wrong loading the plugin
#
- def _ensure_plugin(self, kind: str, provenance_node: Node) -> Tuple[Type[Plugin], str]:
+ def _ensure_plugin(self, kind: str, provenance_node: Node) -> Tuple[Type[Plugin], Optional[str]]:
if kind not in self._types:
diff --git a/src/buildstream/_pluginfactory/sourcefactory.py b/src/buildstream/_pluginfactory/sourcefactory.py
index 4d3722ad6..f27b5a843 100644
--- a/src/buildstream/_pluginfactory/sourcefactory.py
+++ b/src/buildstream/_pluginfactory/sourcefactory.py
@@ -14,8 +14,18 @@
# Authors:
# Tristan Van Berkom <tr...@codethink.co.uk>
+from typing import TYPE_CHECKING, Type, cast
+
from .pluginfactory import PluginFactory
from .pluginorigin import PluginType
+from ..source import Source
+from .._loader import MetaSource
+from .._variables import Variables
+
+if TYPE_CHECKING:
+ from .._context import Context
+ from .._project import Project
+
# A SourceFactory creates Source instances
# in the context of a given factory
@@ -29,8 +39,7 @@ class SourceFactory(PluginFactory):
# create():
#
- # Create a Source object, the pipeline uses this to create Source
- # objects on demand for a given pipeline.
+ # Create a Source object.
#
# Args:
# context (object): The Context object for processing
@@ -45,7 +54,8 @@ class SourceFactory(PluginFactory):
# PluginError (if the kind lookup failed)
# LoadError (if the source itself took issue with the config)
#
- def create(self, context, project, meta, variables):
- source_type, _ = self.lookup(context.messenger, meta.kind, meta.config)
+ def create(self, context: "Context", project: "Project", meta: MetaSource, variables: Variables) -> Source:
+ plugin_type, _ = self.lookup(context.messenger, meta.kind, meta.config)
+ source_type = cast(Type[Source], plugin_type)
source = source_type(context, project, meta, variables)
return source
diff --git a/src/buildstream/element.py b/src/buildstream/element.py
index 656e94f79..f70a3fcb9 100644
--- a/src/buildstream/element.py
+++ b/src/buildstream/element.py
@@ -71,7 +71,7 @@ from contextlib import contextmanager, suppress
from functools import partial
from itertools import chain
import string
-from typing import cast, TYPE_CHECKING, Any, Dict, Iterator, Iterable, List, Optional, Set, Sequence
+from typing import cast, TYPE_CHECKING, Dict, Iterator, Iterable, List, Optional, Set, Sequence
from pyroaring import BitMap # pylint: disable=no-name-in-module
@@ -210,7 +210,7 @@ class Element(Plugin):
context: "Context",
project: "Project",
load_element: "LoadElement",
- plugin_conf: Dict[str, Any],
+ plugin_conf: Optional[str],
*,
artifact_key: str = None,
):
@@ -2857,7 +2857,7 @@ class Element(Plugin):
#
# Normal element initialization procedure.
#
- def __initialize_from_yaml(self, load_element: "LoadElement", plugin_conf: Dict[str, Any]):
+ def __initialize_from_yaml(self, load_element: "LoadElement", plugin_conf: Optional[str]):
context = self._get_context()
project = self._get_project()