Skip to content

Commit 5db36f6

Browse files
committed
VPSet.dumpPython does not print empty template
If the PSetTemplate assigned to template is empty, do not print it when doing dumpPython.
1 parent c4396e0 commit 5db36f6

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

FWCore/ParameterSet/python/Mixins.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ def __setParameters(self,parameters):
268268
self.__addParameter(name, value)
269269
if v is not None:
270270
self.__validator=v
271+
def hasNoParameters(self) -> bool:
272+
return len(self.__parameterNames) == 0
271273
def __setattr__(self,name:str,value):
272274
#since labels are not supposed to have underscores at the beginning
273275
# I will assume that if we have such then we are setting an internal variable

FWCore/ParameterSet/python/Types.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ def __init__(self, *args, **kargs):
197197
def __call__(self, value):
198198
self.__dict__
199199
return self._pset.clone(**value)
200+
def _isEmpty(self) -> bool:
201+
return self._pset.hasNoParameters()
200202
def _isValid(self, value) -> bool:
201203
return isinstance(value,dict) or isinstance(value, PSet)
202204
def dumpPython(self, options:PrintOptions=PrintOptions()) -> str:
@@ -1459,7 +1461,7 @@ def template(self):
14591461
def copy(self):
14601462
return copy.copy(self)
14611463
def _additionalInitArguments(self, options):
1462-
if self._template:
1464+
if self._template and not self._template._isEmpty():
14631465
#NOTE: PSetTemplate.dumpPython does not include the 'cms.' part
14641466
return 'template = cms.'+self._template.dumpPython(options)
14651467
return None
@@ -2420,6 +2422,10 @@ def testVPSetWithTemplate(self):
24202422
ptest = VPSet(PSet(b=int32(3)), template = PSetTemplate(a=required.int32))
24212423
self.assertEqual(len(ptest), 1)
24222424
self.assertEqual(ptest[0].b.value(), 3)
2425+
self.assertEqual(ptest.dumpPython(),"cms.VPSet(cms.PSet(\n b = cms.int32(3)\n), \ntemplate = cms.PSetTemplate(\n a = cms.required.int32\n))"
2426+
)
2427+
ptest = VPSet(template=PSetTemplate())
2428+
self.assertEqual(ptest.dumpPython(),"cms.VPSet()")
24232429
#will inject `a=required.int32` into the PSet when starting from a dict()
24242430
ptest = VPSet(dict(), template = PSetTemplate(a=required.int32))
24252431
self.assertEqual(len(ptest), 1)

0 commit comments

Comments
 (0)