Skip to content

Commit

Permalink
fix: allow path to be a list of strings as well as integers
Browse files Browse the repository at this point in the history
  • Loading branch information
antoni-szych-rtbhouse committed Dec 11, 2023
1 parent b90b3f0 commit 3183582
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
16 changes: 11 additions & 5 deletions voluptuous/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ class Invalid(Error):
"""

def __init__(self, message: str, path: typing.Optional[typing.List[str]] = None, error_message: typing.Optional[str] = None, error_type: typing.Optional[str] = None) -> None:
def __init__(
self,
message: str,
path: typing.Optional[typing.List[typing.Union[str, int]]] = None,
error_message: typing.Optional[str] = None,
error_type: typing.Optional[str] = None
) -> None:
Error.__init__(self, message)
self._path = path or []
self._error_message = error_message or message
Expand All @@ -30,7 +36,7 @@ def msg(self) -> str:
return self.args[0]

@property
def path(self) -> typing.List[str]:
def path(self) -> typing.List[typing.Union[str, int]]:
return self._path

@property
Expand All @@ -45,7 +51,7 @@ def __str__(self) -> str:
output += ' for ' + self.error_type
return output + path

def prepend(self, path: typing.List[str]) -> None:
def prepend(self, path: typing.List[typing.Union[str, int]]) -> None:
self._path = path + self.path


Expand All @@ -61,7 +67,7 @@ def msg(self) -> str:
return self.errors[0].msg

@property
def path(self) -> typing.List[str]:
def path(self) -> typing.List[typing.Union[str, int]]:
return self.errors[0].path

@property
Expand All @@ -74,7 +80,7 @@ def add(self, error: Invalid) -> None:
def __str__(self) -> str:
return str(self.errors[0])

def prepend(self, path: typing.List[str]) -> None:
def prepend(self, path: typing.List[typing.Union[str, int]]) -> None:
for error in self.errors:
error.prepend(path)

Expand Down
5 changes: 1 addition & 4 deletions voluptuous/humanize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
MAX_VALIDATION_ERROR_ITEM_LENGTH = 500


IndexT = typing.TypeVar("IndexT")


def _nested_getitem(data: typing.Dict[IndexT, typing.Any], path: typing.List[IndexT]) -> typing.Optional[typing.Any]:
def _nested_getitem(data: typing.Any, path: typing.List[typing.Union[str, int]]) -> typing.Optional[typing.Any]:
for item_index in path:
try:
data = data[item_index]
Expand Down
4 changes: 2 additions & 2 deletions voluptuous/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def __voluptuous_compile__(self, schema: Schema) -> typing.Callable:
schema.required = old_required
return self._run

def _run(self, path: typing.List[str], value):
def _run(self, path: typing.List[typing.Union[str, int]], value):
if self.discriminant is not None:
self._compiled = [
self.schema._compile(v)
Expand All @@ -243,7 +243,7 @@ def __repr__(self):
self.msg
)

def _exec(self, funcs: typing.Iterable, v, path: typing.Optional[typing.List[str]] = None):
def _exec(self, funcs: typing.Iterable, v, path: typing.Optional[typing.List[typing.Union[str, int]]] = None):
raise NotImplementedError()


Expand Down

0 comments on commit 3183582

Please sign in to comment.