Skip to content

Commit e90f185

Browse files
committed
refactor(SubprocessCommand): Skip defaults in __repr__ of dataclass
In dataclass, skip printing of attributes that are defaults. Credit to Pietro Oldrati - https://stackoverflow.com/a/72161437/1396928 - License is unilicense: - https://stats.stackexchange.com/users/357193/user2246849 - https://unlicense.org/
1 parent 09d3384 commit e90f185

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

libvcs/utils/subprocess.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import dataclasses
4242
import subprocess
4343
import sys
44+
from operator import attrgetter
4445
from typing import (
4546
IO,
4647
Any,
@@ -351,6 +352,22 @@ def run(
351352
check=check,
352353
)
353354

355+
def __repr__(self):
356+
"""Skip defaults.
357+
358+
Credit: Pietro Oldrati, 2022-05-08, Unlicense
359+
360+
See also: https://stackoverflow.com/a/72161437/1396928
361+
"""
362+
nodef_f_vals = (
363+
(f.name, attrgetter(f.name)(self))
364+
for f in dataclasses.fields(self)
365+
if attrgetter(f.name)(self) != f.default
366+
)
367+
368+
nodef_f_repr = ",".join(f"{name}={value}" for name, value in nodef_f_vals)
369+
return f"{self.__class__.__name__}({nodef_f_repr})"
370+
354371

355372
#
356373
# Composable mixins

0 commit comments

Comments
 (0)