Skip to content

Commit 5599057

Browse files
authored
Fix type hints and enable mypy (#486)
1 parent d499a41 commit 5599057

File tree

5 files changed

+25
-11
lines changed

5 files changed

+25
-11
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ jobs:
1515
matrix:
1616
include:
1717
- { python-version: "3.10", session: "flake8" }
18+
- { python-version: "3.10", session: "mypy" }
1819
- { python-version: "3.10", session: "py310" }
1920
- { python-version: "3.9", session: "py39" }
2021
- { python-version: "3.8", session: "py38" }

tox.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ commands =
2222
[testenv:flake8]
2323
deps = flake8
2424
commands = flake8 --doctests setup.py voluptuous
25+
26+
[testenv:mypy]
27+
deps =
28+
mypy
29+
pytest
30+
commands = mypy voluptuous

voluptuous/error.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,22 @@ class Invalid(Error):
2121

2222
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:
2323
Error.__init__(self, message)
24-
self.path = path or []
25-
self.error_message = error_message or message
24+
self._path = path or []
25+
self._error_message = error_message or message
2626
self.error_type = error_type
2727

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

32+
@property
33+
def path(self) -> typing.List[str]:
34+
return self._path
35+
36+
@property
37+
def error_message(self) -> str:
38+
return self._error_message
39+
3240
def __str__(self) -> str:
3341
path = ' @ data[%s]' % ']['.join(map(repr, self.path)) \
3442
if self.path else ''
@@ -38,7 +46,7 @@ def __str__(self) -> str:
3846
return output + path
3947

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

4351

4452
class MultipleInvalid(Invalid):

voluptuous/schema_builder.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ def Extra(_) -> None:
165165
primitive_types = (bool, bytes, int, long, str, unicode, float, complex)
166166

167167
Schemable = typing.Union[
168-
Extra, 'Schema', 'Object',
168+
'Schema', 'Object',
169169
_Mapping,
170170
list, tuple, frozenset, set,
171171
bool, bytes, int, long, str, unicode, float, complex,
172-
type, object, dict, type(None), typing.Callable
172+
type, object, dict, None, typing.Callable
173173
]
174174

175175

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

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

759758
result = self.schema.copy()
760759

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

780779
# if both are dictionaries, we need to extend recursively
781780
# create the new extended sub schema, then remove the old key and add the new one
782-
if type(result_value) == dict and type(value) == dict:
781+
if isinstance(result_value, dict) and isinstance(value, dict):
783782
new_value = Schema(result_value).extend(value).schema
784783
del result[result_key]
785784
result[key] = new_value

voluptuous/tests/tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def test_literal():
167167
except MultipleInvalid as e:
168168
assert str(e) == "{'b': 1} not match for {'a': 1}"
169169
assert len(e.errors) == 1
170-
assert type(e.errors[0]) == LiteralInvalid
170+
assert isinstance(e.errors[0], LiteralInvalid)
171171
else:
172172
assert False, "Did not raise Invalid"
173173

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

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

0 commit comments

Comments
 (0)