Skip to content

Commit

Permalink
Fix type hints and enable mypy (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
KapJI authored Nov 12, 2023
1 parent d499a41 commit 5599057
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
matrix:
include:
- { python-version: "3.10", session: "flake8" }
- { python-version: "3.10", session: "mypy" }
- { python-version: "3.10", session: "py310" }
- { python-version: "3.9", session: "py39" }
- { python-version: "3.8", session: "py38" }
Expand Down
6 changes: 6 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ commands =
[testenv:flake8]
deps = flake8
commands = flake8 --doctests setup.py voluptuous

[testenv:mypy]
deps =
mypy
pytest
commands = mypy voluptuous
14 changes: 11 additions & 3 deletions voluptuous/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,22 @@ 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:
Error.__init__(self, message)
self.path = path or []
self.error_message = error_message or message
self._path = path or []
self._error_message = error_message or message
self.error_type = error_type

@property
def msg(self) -> str:
return self.args[0]

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

@property
def error_message(self) -> str:
return self._error_message

def __str__(self) -> str:
path = ' @ data[%s]' % ']['.join(map(repr, self.path)) \
if self.path else ''
Expand All @@ -38,7 +46,7 @@ def __str__(self) -> str:
return output + path

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


class MultipleInvalid(Invalid):
Expand Down
9 changes: 4 additions & 5 deletions voluptuous/schema_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ def Extra(_) -> None:
primitive_types = (bool, bytes, int, long, str, unicode, float, complex)

Schemable = typing.Union[
Extra, 'Schema', 'Object',
'Schema', 'Object',
_Mapping,
list, tuple, frozenset, set,
bool, bytes, int, long, str, unicode, float, complex,
type, object, dict, type(None), typing.Callable
type, object, dict, None, typing.Callable
]


Expand Down Expand Up @@ -753,8 +753,7 @@ def extend(self, schema: Schemable, required: typing.Optional[bool] = None, extr
:param extra: if set, overrides `extra` of this `Schema`
"""

assert type(self.schema) == dict and type(schema) == dict, 'Both schemas must be dictionary-based'
assert isinstance(self.schema, dict)
assert isinstance(self.schema, dict) and isinstance(schema, dict), 'Both schemas must be dictionary-based'

result = self.schema.copy()

Expand All @@ -779,7 +778,7 @@ def key_literal(key):

# if both are dictionaries, we need to extend recursively
# create the new extended sub schema, then remove the old key and add the new one
if type(result_value) == dict and type(value) == dict:
if isinstance(result_value, dict) and isinstance(value, dict):
new_value = Schema(result_value).extend(value).schema
del result[result_key]
result[key] = new_value
Expand Down
6 changes: 3 additions & 3 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def test_literal():
except MultipleInvalid as e:
assert str(e) == "{'b': 1} not match for {'a': 1}"
assert len(e.errors) == 1
assert type(e.errors[0]) == LiteralInvalid
assert isinstance(e.errors[0], LiteralInvalid)
else:
assert False, "Did not raise Invalid"

Expand All @@ -184,7 +184,7 @@ class C1(object):
except MultipleInvalid as e:
assert str(e) == "expected C1"
assert len(e.errors) == 1
assert type(e.errors[0]) == TypeInvalid
assert isinstance(e.errors[0], TypeInvalid)
else:
assert False, "Did not raise Invalid"

Expand All @@ -200,7 +200,7 @@ class C2:
except MultipleInvalid as e:
assert str(e) == "expected C2"
assert len(e.errors) == 1
assert type(e.errors[0]) == TypeInvalid
assert isinstance(e.errors[0], TypeInvalid)
else:
assert False, "Did not raise Invalid"

Expand Down

0 comments on commit 5599057

Please sign in to comment.