Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow path to be a list of strings, integers or any other hashables #497

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.Hashable]] = 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.Hashable]:
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.Hashable]) -> 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.Hashable]:
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.Hashable]) -> 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.Hashable]) -> 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.Hashable], 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.Hashable]] = None):
raise NotImplementedError()


Expand Down