Skip to content

Commit 12baa82

Browse files
Remove collect_violations(..., into=) argument (#365)
At risk of Chesterton's fencing myself, but I don't think this argument is needed or ever used (other than in tests). I can't think of a reason why someone would want to shove multiple validations into the same validation, instead of simply calling `collect_violations` multiple times and combining the lists afterwards. I did some `git blame` spelunking and it looks like this originally turned up in #32, but didn't see much reasoning for it in that PR. The PR mentioned both Pydantic and making things closer to protovalidate-go's API, but neither seem to have a similar style of reusing a returned error/exception like this. Stacked on #364 as these both munge with the exported API & docstrings; will tag a `v0.15.0` release once all of this is merged.
1 parent 9d49e82 commit 12baa82

File tree

3 files changed

+3
-24
lines changed

3 files changed

+3
-24
lines changed

protovalidate/internal/rules.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,9 @@ class RuleContext:
267267

268268
_violations: list[Violation]
269269

270-
def __init__(self, *, fail_fast: bool = False, violations: typing.Optional[list[Violation]] = None):
270+
def __init__(self, *, fail_fast: bool = False):
271271
self._fail_fast = fail_fast
272-
if violations is None:
273-
violations = []
274-
self._violations = violations
272+
self._violations = []
275273

276274
@property
277275
def violations(self) -> list[Violation]:

protovalidate/validator.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import typing
16-
1715
from google.protobuf import message
1816

1917
from buf.validate import validate_pb2
@@ -63,7 +61,6 @@ def collect_violations(
6361
message: message.Message,
6462
*,
6563
fail_fast: bool = False,
66-
into: typing.Optional[list[Violation]] = None,
6764
) -> list[Violation]:
6865
"""
6966
Validates the given message against the static rules defined in
@@ -77,12 +74,10 @@ def collect_violations(
7774
Parameters:
7875
message: The message to validate.
7976
fail_fast: If true, validation will stop after the first iteration.
80-
into: If provided, any violations will be appended to the
81-
Violations object and the same object will be returned.
8277
Raises:
8378
CompilationError: If the static rules could not be compiled.
8479
"""
85-
ctx = _rules.RuleContext(fail_fast=fail_fast, violations=into)
80+
ctx = _rules.RuleContext(fail_fast=fail_fast)
8681
for rule in self._factory.get(message.DESCRIPTOR):
8782
rule.validate(ctx, message)
8883
if ctx.done:

test/test_validate.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,6 @@ def test_oneofs(self):
8585

8686
self._run_valid_tests(msg)
8787

88-
def test_collect_violations_into(self):
89-
msg1 = validations_pb2.Oneof()
90-
msg1.y = 123
91-
92-
msg2 = validations_pb2.Oneof()
93-
msg2.z.val = True
94-
95-
for label, v in get_default_validator():
96-
with self.subTest(label=label):
97-
# Test collect_violations into
98-
violations = v.collect_violations(msg1)
99-
v.collect_violations(msg2, into=violations)
100-
self.assertEqual(len(violations), 0)
101-
10288
def test_protovalidate_oneof_valid(self):
10389
msg = validations_pb2.ProtovalidateOneof()
10490
msg.a = "A"

0 commit comments

Comments
 (0)