8000 fix: hide FailureError private attributes in closure · proofit404/stories@1fafcc1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1fafcc1

Browse files
dry-python-botdry-python-bot
authored andcommitted
fix: hide FailureError private attributes in closure
1 parent f8144aa commit 1fafcc1

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

src/_stories/exceptions.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ class StoryDefinitionError(StoryError):
1010

1111

1212
class FailureError(StoryError):
13-
def __init__(self, reason):
14-
self.__reason = reason
15-
super(FailureError, self).__init__()
13+
pass
14+
15+
16+
def make_failure_error(reason):
17+
reason_representation = repr(reason) if reason else ""
18+
19+
def repr_method(self):
20+
return "FailureError(" + reason_representation + ")"
1621

17-
def __repr__(self):
18-
reason = repr(self.__reason) if self.__reason else ""
19-
return "FailureError(" + reason + ")"
22+
return type("FailureError", (FailureError,), {"__repr__": repr_method})()
2023

2124

2225
class FailureProtocolError(StoryError):

src/_stories/exceptions.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ from typing import Union
44

55
class StoryError: ...
66
class StoryDefinitionError: ...
7+
class FailureError: ...
78

8-
class FailureError:
9-
def __init__(self, reason: Optional[Union[str, Enum]]) -> None: ...
10-
def __repr__(self) -> str: ...
9+
def make_failure_error(reason: Optional[Union[str, Enum]]) -> FailureError: ...
1110

1211
class FailureProtocolError: ...
1312
class ContextContractError: ...

src/_stories/run.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# -*- coding: utf-8 -*-
2-
from _stories.exceptions import FailureError
3-
from _stories.summary import FailureSummary
4-
from _stories.summary import SuccessSummary
2+
from _stories.exceptions import make_failure_error
3+
from _stories.summary import make_failure_summary
4+
from _stories.summary import make_success_summary
55

66

77
class Call(object):
88
def got_failure(self, ctx, method_name, reason):
9-
raise FailureError(reason)
9+
raise make_failure_error(reason)
1010

1111
def got_result(self, value):
1212
return value

tests/test_base.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ def test_failure():
7979
result.value
8080

8181

82+
def test_failure_error_private_fields():
83+
"""Deny access to the private fields of the `FailureError` exception."""
84+
85+
with pytest.raises(FailureError) as exc_info:
86+
examples.methods.Simple().x(foo=2, bar=2)
87+
assert exc_info.value.__dict__ == {}
88+
89+
8290
def test_result():
8391

8492
result = examples.methods.Simple().x(foo=1, bar=3)

0 commit comments

Comments
 (0)
0