Skip to content

Commit

Permalink
Set a default for node/list/str classes (#3042)
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong authored Jan 31, 2024
1 parent 581cb6c commit 15c5d6c
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions src/cfnlint/decode/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
SPDX-License-Identifier: MIT-0
"""

from __future__ import annotations

import logging
from collections import namedtuple
from copy import deepcopy
from typing import Any, Dict, Protocol, Set

import regex as re

Expand All @@ -15,6 +19,14 @@ class TemplateAttributeError(AttributeError):
"""Custom error to capture Attribute Errors in the Template"""


class Mark(Protocol):
line: int
column: int


_mark = namedtuple("_mark", ["line", "column"])


def create_str_node_class(cls):
"""
Create string node class
Expand All @@ -23,13 +35,16 @@ def create_str_node_class(cls):
class node_class(cls):
"""Node class created based on the input class"""

def __init__(self, x, start_mark, end_mark):
def __init__(
self, x, start_mark: Mark | None = None, end_mark: Mark | None = None
):
try:
cls.__init__(self, x)
except TypeError:
cls.__init__(self)
self.start_mark = start_mark
self.end_mark = end_mark

self.start_mark = start_mark or _mark(0, 0)
self.end_mark = end_mark or _mark(0, 0)

# pylint: disable=bad-classmethod-argument, unused-argument
def __new__(self, x, start_mark, end_mark):
Expand Down Expand Up @@ -58,13 +73,16 @@ def create_dict_node_class(cls):
class node_class(cls):
"""Node class created based on the input class"""

def __init__(self, x, start_mark, end_mark):
def __init__(
self, x, start_mark: Mark | None = None, end_mark: Mark | None = None
):
LOGGER.debug(type(start_mark))
try:
cls.__init__(self, x)
except TypeError:
cls.__init__(self)
self.start_mark = start_mark
self.end_mark = end_mark
self.start_mark = start_mark or _mark(0, 0)
self.end_mark = end_mark or _mark(0, 0)
self.condition_functions = ["Fn::If"]

def __deepcopy__(self, memo):
Expand Down Expand Up @@ -207,12 +225,14 @@ def create_sub_node_class(cls):
class sub_class(cls):
"""Node class created based on the input class"""

def __init__(self, x, start_mark, end_mark):
def __init__(
self, x, start_mark: Mark | None = None, end_mark: Mark | None = None
):
cls.__init__(self, x, start_mark, end_mark)
self.__cache_is_valid = False
self.__cache_sub_string = ""
self.__cache_sub_string_vars = set()
self.__cache_sub_vars = {}
self.__cache_sub_string_vars: Set[str] = set()
self.__cache_sub_vars: Dict[str, Any] = {}
self.__setup()

def __setup_list_sub_string(self, s):
Expand Down Expand Up @@ -280,13 +300,15 @@ def create_dict_list_class(cls):
class node_class(cls):
"""Node class created based on the input class"""

def __init__(self, x, start_mark, end_mark):
def __init__(
self, x, start_mark: Mark | None = None, end_mark: Mark | None = None
):
try:
cls.__init__(self, x)
except TypeError:
cls.__init__(self)
self.start_mark = start_mark
self.end_mark = end_mark
self.start_mark = start_mark or _mark(0, 0)
self.end_mark = end_mark or _mark(0, 0)
self.condition_functions = ["Fn::If"]

def __deepcopy__(self, memo):
Expand Down

0 comments on commit 15c5d6c

Please sign in to comment.