Skip to content

Commit 2849a31

Browse files
Ghesselinkaothms
authored andcommitted
Use _ValidationError only internally
1 parent b4bc8e9 commit 2849a31

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
try:
22
from parser.parse import parse
33
from parser.file import file, open
4-
from parser.errors import ValidationError, CollectedValidationErrors, DuplicateNameError, HeaderFieldError
4+
from parser.errors import _ValidationError, CollectedValidationErrors, DuplicateNameError, HeaderFieldError
55
except:
66
from .parser.parse import parse
77
from .parser.file import file, open
8-
from .parser.errors import ValidationError, CollectedValidationErrors, DuplicateNameError, HeaderFieldError
8+
from .parser.errors import _ValidationError, CollectedValidationErrors, DuplicateNameError, HeaderFieldError
99

10-
__all__ = ["parse", "open", "file", "ValidationError",
10+
__all__ = ["parse", "open", "file", "_ValidationError",
1111
"CollectedValidationErrors", "DuplicateNameError", "HeaderFieldError"] # for testing

parser/errors.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from lark.exceptions import UnexpectedToken
22

3-
class ValidationError(Exception):
4-
pass
3+
class _ValidationError(Exception):
4+
def __init__(self, *args, **kwargs):
5+
if self.__class__ is _ValidationError:
6+
raise TypeError("Do not raise _ValidationError directly.")
7+
super().__init__(*args, **kwargs)
58

69
class ErrorCollector:
710
def __init__(self):
@@ -14,7 +17,7 @@ def raise_if_any(self):
1417
if self.errors:
1518
raise CollectedValidationErrors(self.errors)
1619

17-
class CollectedValidationErrors(ValidationError):
20+
class CollectedValidationErrors(_ValidationError):
1821
def __init__(self, errors):
1922
self.errors = errors
2023

@@ -24,7 +27,7 @@ def asdict(self, with_message=True):
2427
def __str__(self):
2528
return f"{len(self.errors)} validation error(s) collected:\n" + "\n\n".join(str(e) for e in self.errors)
2629

27-
class SyntaxError(ValidationError):
30+
class SyntaxError(_ValidationError):
2831
def __init__(self, filecontent, exception):
2932
self.filecontent = filecontent
3033
self.exception = exception
@@ -57,7 +60,7 @@ def __str__(self):
5760
return f"On line {d['lineno']} column {d['column']}:\nUnexpected {sth}{d['found_type']} ('{d['found_value']}')\nExpecting {exp}\n{d['lineno']:05d} | {d['line']}\n {' ' * (self.exception.column - 1)}^"
5861

5962

60-
class DuplicateNameError(ValidationError):
63+
class DuplicateNameError(_ValidationError):
6164
def __init__(self, filecontent, name, linenumbers):
6265
self.name = name
6366
self.filecontent = filecontent
@@ -83,7 +86,7 @@ def build():
8386
return "\n".join(build())
8487

8588

86-
class HeaderFieldError(ValidationError):
89+
class HeaderFieldError(_ValidationError):
8790
def __init__(self, field, found_len, expected_len):
8891
self.field = field
8992
self.found_len = found_len

parser/parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# import transformer
99
from .transformer import Transformer, entity_instance, make_header_ent, create_step_entity
1010
from .grammar import grammar, HEADER_FIELDS
11-
from .errors import HeaderFieldError, DuplicateNameError, ErrorCollector, ValidationError, SyntaxError
11+
from .errors import HeaderFieldError, DuplicateNameError, ErrorCollector, SyntaxError
1212

1313
def validate_header_fields(header, error_collector, only_header = False):
1414
for field in HEADER_FIELDS.keys():

test_parser.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import glob
22
import pytest
33

4-
from __init__ import parse, open, ValidationError, CollectedValidationErrors, DuplicateNameError, HeaderFieldError
4+
from __init__ import parse, open, _ValidationError, CollectedValidationErrors, DuplicateNameError, HeaderFieldError
55
from contextlib import nullcontext
66

77

88
def create_context(fn):
99
if "fail_" in fn:
10-
return pytest.raises(ValidationError)
10+
return pytest.raises(_ValidationError)
1111
else:
1212
return nullcontext()
1313

@@ -120,7 +120,7 @@ def test_file_mvd_attr():
120120
])
121121
def test_invalid_headers_(filename):
122122
# error in header
123-
with pytest.raises(ValidationError):
123+
with pytest.raises(_ValidationError):
124124
parse(filename=filename, with_tree=False, only_header=True)
125125

126126
@pytest.mark.parametrize("filename", [
@@ -134,11 +134,11 @@ def test_valid_headers(filename):
134134
parse(filename=filename, with_tree=False, only_header=True)
135135

136136
def test_header_entity_fields():
137-
with pytest.raises(ValidationError):
137+
with pytest.raises(_ValidationError):
138138
parse(filename='fixtures/fail_too_many_header_entity_fields.ifc', only_header=True)
139139

140140
def test_header_entity_fields_whole_file():
141-
with pytest.raises(ValidationError):
141+
with pytest.raises(_ValidationError):
142142
parse(filename='fixtures/fail_too_many_header_entity_fields.ifc')
143143

144144
def test_header_entity_fields_whole_file():

0 commit comments

Comments
 (0)