Skip to content

Commit 17f8294

Browse files
authored
Fix RepresentationMixin PEP3102 keyword-only arguments (#3925)
# Changed Behaviour Prior to this PR, keyword-only arguments (those appearing after a `*`) such as z in: ```python def f(x, y, *, z): ``` would not be output by the representation mixin. This PR makes `RepresentationMixin` output those. ## Type of change - Bug fix
1 parent 4a59838 commit 17f8294

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

parsl/tests/test_utils/test_representation_mixin.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ def __init__(self, x, y="default 2"):
1515
self.y = y
1616

1717

18+
class GoodReprKeywordOnly(RepresentationMixin):
19+
def __init__(self, *, x, y):
20+
self.x = x
21+
self.y = y
22+
23+
1824
class BadRepr(RepresentationMixin):
1925
"""This class incorrectly subclasses RepresentationMixin.
2026
It does not store the parameter x on self.
@@ -64,6 +70,20 @@ def test_repr_good_defaults_defaulted():
6470
assert "default 2" in r
6571

6672

73+
@pytest.mark.local
74+
def test_repr_good_keyword_only():
75+
p1 = "parameter 1"
76+
p2 = "the second parameter"
77+
78+
# repr should not raise an exception
79+
r = repr(GoodReprKeywordOnly(x=p1, y=p2))
80+
81+
# representation should contain both values supplied
82+
# at object creation.
83+
assert p1 in r
84+
assert p2 in r
85+
86+
6787
@pytest.mark.local
6888
def test_repr_bad():
6989
p1 = "parameter 1"

parsl/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ def __repr__(self) -> str:
250250
args = [getattr(self, a, default) for a in argspec.args[1:]]
251251
kwargs = {key: getattr(self, key, default) for key in defaults}
252252

253+
kwonlyargs = {key: getattr(self, key, default) for key in argspec.kwonlyargs}
254+
kwargs.update(kwonlyargs)
255+
253256
def assemble_multiline(args: List[str], kwargs: Dict[str, object]) -> str:
254257
def indent(text: str) -> str:
255258
lines = text.splitlines()

0 commit comments

Comments
 (0)