Skip to content

Commit

Permalink
More consistent use of custom data types
Browse files Browse the repository at this point in the history
  • Loading branch information
wwkimball committed Apr 25, 2021
1 parent 8a29c36 commit 332e421
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
12 changes: 4 additions & 8 deletions yamlpath/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ruamel.yaml.comments import CommentedMap

from yamlpath.types import AncestryEntry, PathSegment
from yamlpath.types import AncestryEntry, PathAttributes, PathSegment
from yamlpath.common import Anchors, KeywordSearches, Nodes, Searches
from yamlpath import YAMLPath
from yamlpath.path import SearchKeywordTerms, SearchTerms, CollectorTerms
Expand Down Expand Up @@ -798,7 +798,8 @@ def _get_nodes_by_key(
next_translated_path = (translated_path +
YAMLPath.escape_path_section(
str_stripped, translated_path.seperator))
next_ancestry = ancestry + [(data, stripped_attrs)]
next_ancestry: List[AncestryEntry] = ancestry + [
(data, stripped_attrs)]
if stripped_attrs in data:
self.logger.debug(
"Processor::_get_nodes_by_key: FOUND key node by name at"
Expand Down Expand Up @@ -1696,12 +1697,7 @@ def _get_optional_nodes(
if segments and len(segments) > depth:
pathseg: PathSegment = yaml_path.unescaped[depth]
(segment_type, unstripped_attrs) = pathseg
stripped_attrs: Union[
str,
int,
SearchTerms,
CollectorTerms
] = segments[depth][1]
stripped_attrs: PathAttributes = segments[depth][1]
except_segment = str(unstripped_attrs)

self.logger.debug(
Expand Down
2 changes: 1 addition & 1 deletion yamlpath/types/pathattributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
import yamlpath.path.searchterms as searchterms


PathAttributes = Union[str, CollectorTerms, searchterms.SearchTerms]
PathAttributes = Union[str, int, CollectorTerms, searchterms.SearchTerms, None]
8 changes: 5 additions & 3 deletions yamlpath/wrappers/nodecoords.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
from typing import Any, List, Optional

from yamlpath.types import PathSegment
from yamlpath.types import AncestryEntry, PathSegment
from yamlpath import YAMLPath

class NodeCoords:
Expand All @@ -26,7 +26,7 @@ class NodeCoords:
def __init__(
self, node: Any, parent: Any, parentref: Any,
path: Optional[YAMLPath] = None,
ancestry: Optional[List[tuple]] = None,
ancestry: Optional[List[AncestryEntry]] = None,
path_segment: Optional[PathSegment] = None
) -> None:
"""
Expand All @@ -53,7 +53,9 @@ def __init__(
self.parent: Any = parent
self.parentref: Any = parentref
self.path: Optional[YAMLPath] = path
self.ancestry: List[tuple] = [] if ancestry is None else ancestry
self.ancestry: List[AncestryEntry] = ([]
if ancestry is None
else ancestry)
self.path_segment: Optional[PathSegment] = path_segment

def __str__(self) -> str:
Expand Down
12 changes: 6 additions & 6 deletions yamlpath/yamlpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from collections import deque
from typing import Deque, List, Optional, Union

from yamlpath.types import PathSegment
from yamlpath.types import PathAttributes, PathSegment
from yamlpath.exceptions import YAMLPathException
from yamlpath.enums import (
PathSegmentTypes,
Expand Down Expand Up @@ -732,8 +732,8 @@ def _parse_path(self,

@staticmethod
def _expand_splats(
yaml_path: str, segment_id: str,
segment_type: Optional[PathSegmentTypes] = None
yaml_path: str, segment_id: PathAttributes,
segment_type: PathSegmentTypes
) -> PathSegment:
"""
Replace segment IDs with search operators when * is present.
Expand All @@ -746,10 +746,10 @@ def _expand_splats(
Returns: (PathSegment) Coallesced YAML Path segment.
"""
coal_type = segment_type
coal_value: Union[str, SearchTerms, None] = segment_id
coal_type: PathSegmentTypes = segment_type
coal_value: PathAttributes = segment_id

if '*' in segment_id:
if isinstance(segment_id, str) and '*' in segment_id:
splat_count = segment_id.count("*")
splat_pos = segment_id.index("*")
segment_len = len(segment_id)
Expand Down

0 comments on commit 332e421

Please sign in to comment.