You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by tv...@apache.org on 2021/01/27 07:38:42 UTC

[buildstream] 02/03: node.pyi: Fleshing out more type information about nodes.

This is an automated email from the ASF dual-hosted git repository.

tvb pushed a commit to branch tristan/change-remote-config
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit dcffd45eef85d51abfa3fe862c7663fff15ccc24
Author: Tristan van Berkom <tr...@codethink.co.uk>
AuthorDate: Wed Jan 27 16:36:40 2021 +0900

    node.pyi: Fleshing out more type information about nodes.
---
 src/buildstream/node.pyi | 51 +++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 46 insertions(+), 5 deletions(-)

diff --git a/src/buildstream/node.pyi b/src/buildstream/node.pyi
index a3b0ed9..378a1fb 100644
--- a/src/buildstream/node.pyi
+++ b/src/buildstream/node.pyi
@@ -1,4 +1,18 @@
-from typing import overload, Generic, List, Mapping, Optional, Sequence, TypeVar, Type, Dict, Any
+from typing import (
+    overload,
+    Generic,
+    Union,
+    Iterable,
+    Tuple,
+    List,
+    Mapping,
+    Optional,
+    Sequence,
+    TypeVar,
+    Type,
+    Dict,
+    Any,
+)
 
 from ._project import Project
 
@@ -11,19 +25,40 @@ class Node:
     def clone(self) -> "Node": ...
     def get_provenance(self) -> ProvenanceInformation: ...
     def strip_node_info(self) -> Dict[str, Any]: ...
+    # FIXME: We should be able to annotate more specifically what is allowed
+    #        in the dictionary here, but this requires recursive type annotations
+    #        which appears to not yet be properly supported.
+    #
+    #        See: https://github.com/python/mypy/issues/731
+    #
+    @classmethod
+    def from_dict(cls, value: Dict[str, Any]) -> "MappingNode": ...
 
 class ScalarNode(Node):
     def as_str(self) -> str: ...
     def clone(self) -> "ScalarNode": ...
 
 class SequenceNode(Node, Generic[TNode]):
+    def __iter__(self) -> "SequenceNode": ...
+    def __next__(self) -> Any: ...
     def as_str_list(self) -> List[str]: ...
     def clone(self) -> "SequenceNode[TNode]": ...
 
 class MappingNode(Node, Generic[TNode]):
     def __init__(self, file_index: int, line: int, column: int, value: Mapping[str, TValidNodeValue]) -> None: ...
+    def __contains__(self, what: Any) -> bool: ...
     def clone(self) -> MappingNode[TNode]: ...
+    def keys(self) -> Iterable[str]: ...
+    def items(self) -> Iterable[Tuple[str, Any]]: ...
+    def safe_del(self, key: str) -> None: ...
     def validate_keys(self, valid_keys: List[str]): ...
+    @overload
+    def get_scalar(self, key: str) -> ScalarNode: ...
+    @overload
+    def get_scalar(self, key: str, default: Union[str, int, bool, None]) -> ScalarNode: ...
+    @overload
+    def get_bool(self, key: str) -> bool: ...
+    @overload
     def get_bool(self, key: str, default: bool) -> bool: ...
     @overload
     def get_str_list(self, key: str) -> List[str]: ...
@@ -44,15 +79,17 @@ class MappingNode(Node, Generic[TNode]):
     @overload
     def get_int(self, key: str, default: Optional[int]) -> Optional[int]: ...
     @overload
-    def get_enum(self, key: str, constraint: object) -> object: ...
+    def get_enum(self, key: str, constraint: object) -> str: ...
     @overload
-    def get_enum(self, key: str, constraint: object, default: Optional[object]) -> Optional[object]: ...
+    def get_enum(self, key: str, constraint: object, default: Optional[object]) -> Optional[str]: ...
     @overload
     def get_mapping(self, key: str) -> "MappingNode": ...
     @overload
-    def get_mapping(self, key: str, default: "MappingNode") -> "MappingNode": ...
+    def get_mapping(self, key: str, default: Union["MappingNode", Dict[str, Any]]) -> "MappingNode": ...
     @overload
-    def get_mapping(self, key: str, default: Optional["MappingNode"]) -> Optional["MappingNode"]: ...
+    def get_mapping(
+        self, key: str, default: Union["MappingNode", Dict[str, Any], None]
+    ) -> Optional["MappingNode"]: ...
     @overload
     def get_sequence(self, key: str, *, allowed_types: Optional[List[Type[Node]]]) -> SequenceNode: ...
     @overload
@@ -69,6 +106,10 @@ class MappingNode(Node, Generic[TNode]):
     def get_node(self, key: str, allowed_types: Optional[List[Type[Node]]]) -> Node: ...
     @overload
     def get_node(self, key: str, allowed_types: Optional[List[Type[Node]]], allow_none: bool) -> Optional[Node]: ...
+    #
+    # Private
+    #
+    def _composite(self, target: "MappingNode") -> None: ...
 
 def _assert_symbol_name(
     symbol_name: str, purpose: str, *, ref_node: Optional[Node], allow_dashes: bool = True