From 0600fadc3786ed9e03edf2a21d990b3b8b84ac29 Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Mon, 18 Mar 2024 16:56:21 +0100 Subject: [PATCH] Added python type annotation to some configuration classes - Added for the cases where it was easy to determine the type --- FWCore/ParameterSet/python/Config.py | 171 +++---- FWCore/ParameterSet/python/Mixins.py | 111 ++--- FWCore/ParameterSet/python/Modules.py | 84 ++-- FWCore/ParameterSet/python/ModulesProxy.py | 2 +- FWCore/ParameterSet/python/SequenceTypes.py | 194 ++++---- .../ParameterSet/python/SequenceVisitors.py | 6 +- FWCore/ParameterSet/python/TreeCrawler.py | 8 +- FWCore/ParameterSet/python/Types.py | 454 +++++++++--------- FWCore/ParameterSet/python/pfnInPath.py | 2 +- FWCore/ParameterSet/python/processFromFile.py | 2 +- 10 files changed, 518 insertions(+), 516 deletions(-) diff --git a/FWCore/ParameterSet/python/Config.py b/FWCore/ParameterSet/python/Config.py index 3250d04c24694..33f712b1a4df8 100644 --- a/FWCore/ParameterSet/python/Config.py +++ b/FWCore/ParameterSet/python/Config.py @@ -10,6 +10,7 @@ ## imports import sys +from typing import Union from .Mixins import PrintOptions,_ParameterTypeBase,_SimpleParameterTypeBase, _Parameterizable, _ConfigureComponent, _TypedParameterizable, _Labelable, _Unlabelable, _ValidatingListBase, _modifyParametersFromDict from .Mixins import * from .Types import * @@ -34,11 +35,11 @@ class errors(object): UnavailableAccelerator = "{UnavailableAccelerator}" class EDMException(Exception): - def __init__(self, error, message): + def __init__(self, error:str, message: str): super().__init__(error+"\n"+message) -def checkImportPermission(minLevel = 2, allowedPatterns = []): +def checkImportPermission(minLevel: int = 2, allowedPatterns = []): """ Raise an exception if called by special config files. This checks the call or import stack for the importing file. An exception is raised if @@ -113,7 +114,7 @@ class Temp(object): class Process(object): """Root class for a CMS configuration process""" _firstProcess = True - def __init__(self,name,*Mods): + def __init__(self,name: str, *Mods): """The argument 'name' will be the name applied to this Process Can optionally pass as additional arguments cms.Modifier instances that will be used to modify the Process as it is built @@ -168,7 +169,7 @@ def __init__(self,name,*Mods): for m in self.__modifiers: m._setChosen() - def setStrict(self, value): + def setStrict(self, value: bool): self.__isStrict = value _Module.__isStrict__ = True @@ -210,9 +211,9 @@ def filters_(self): """returns a dict of the filters that have been added to the Process""" return DictTypes.FixedKeysDict(self.__filters) filters = property(filters_, doc="dictionary containing the filters for the process") - def name_(self): + def name_(self) -> str: return self.__name - def setName_(self,name): + def setName_(self,name: str): if not name.isalnum(): raise RuntimeError("Error: The process name is an empty string or contains non-alphanumeric characters") self.__dict__['_Process__name'] = name @@ -282,7 +283,7 @@ def __updateOptions(self,opt): def defaultMaxEvents_(): return untracked.PSet(input=optional.untracked.int32, output=optional.untracked.allowed(int32,PSet)) - def __updateMaxEvents(self,ps): + def __updateMaxEvents(self,ps: Union[dict,PSet]): newMax = self.defaultMaxEvents_() if isinstance(ps,dict): for k,v in ps.items(): @@ -333,12 +334,12 @@ def conditionaltasks_(self): def schedule_(self): """returns the schedule that has been added to the Process or None if none have been added""" return self.__schedule - def setPartialSchedule_(self,sch,label): + def setPartialSchedule_(self,sch: Schedule,label: str): if label == "schedule": self.setSchedule_(sch) else: self._place(label, sch, self.__partialschedules) - def setSchedule_(self,sch): + def setSchedule_(self,sch: Schedule): # See if every path and endpath has been inserted into the process index = 0 try: @@ -382,7 +383,7 @@ def vpsets_(self): return DictTypes.FixedKeysDict(self.__vpsets) vpsets = property(vpsets_,doc="dictionary containing the PSets for the process") - def isUsingModifier(self,mod): + def isUsingModifier(self,mod) -> bool: """returns True if the Modifier is in used by this Process""" if mod._isChosen(): for m in self.__modifiers: @@ -390,7 +391,7 @@ def isUsingModifier(self,mod): return True return False - def __setObjectLabel(self, object, newLabel) : + def __setObjectLabel(self, object, newLabel:str) : if not object.hasLabel_() : object.setLabel(newLabel) return @@ -417,7 +418,7 @@ def __setObjectLabel(self, object, newLabel) : object.setLabel(None) object.setLabel(newLabel) - def __setattr__(self,name,value): + def __setattr__(self,name:str,value): # check if the name is well-formed (only _ and alphanumerics are allowed) if not name.replace('_','').isalnum(): raise ValueError('The label '+name+' contains forbiden characters') @@ -558,7 +559,7 @@ def __setattr__(self,name,value): self._delattrFromSetattr(name) self.__injectValidValue(name, value, newValue) - def __injectValidValue(self, name, value, newValue = None): + def __injectValidValue(self, name:str, value, newValue = None): if newValue is None: newValue = value self.__dict__[name]=newValue @@ -581,7 +582,7 @@ def __findFirstUsingModule(self, seqsOrTasks, mod): return seqOrTask return None - def _delHelper(self,name): + def _delHelper(self,name:str): if not hasattr(self,name): raise KeyError('process does not know about '+name) elif name.startswith('_Process__'): @@ -598,7 +599,7 @@ def _delHelper(self,name): if isinstance(obj,Service): obj._inProcess = False - def __delattr__(self,name): + def __delattr__(self,name:str): self._delHelper(name) obj = getattr(self,name) if not obj is None: @@ -626,7 +627,7 @@ def __delattr__(self,name): except: pass - def _delattrFromSetattr(self,name): + def _delattrFromSetattr(self,name:str): """Similar to __delattr__ but we need different behavior when called from __setattr__""" self._delHelper(name) # now remove it from the process itself @@ -649,7 +650,7 @@ def add_(self,value): newValue =value newValue._place('',self) - def _okToPlace(self, name, mod, d): + def _okToPlace(self, name:str, mod, d) -> bool: if not self.__InExtendCall: # if going return True @@ -670,7 +671,7 @@ def _okToPlace(self, name, mod, d): else: return True - def _place(self, name, mod, d): + def _place(self, name:str, mod, d): if self._okToPlace(name, mod, d): if self.__isStrict and isinstance(mod, _ModuleSequenceType): d[name] = mod._postProcessFixup(self._cloneToObjectDict) @@ -678,59 +679,59 @@ def _place(self, name, mod, d): d[name] = mod if isinstance(mod,_Labelable): self.__setObjectLabel(mod, name) - def _placeOutputModule(self,name,mod): + def _placeOutputModule(self,name:str,mod): self._place(name, mod, self.__outputmodules) - def _placeProducer(self,name,mod): + def _placeProducer(self,name:str,mod): self._place(name, mod, self.__producers) - def _placeSwitchProducer(self,name,mod): + def _placeSwitchProducer(self,name:str,mod): self._place(name, mod, self.__switchproducers) - def _placeFilter(self,name,mod): + def _placeFilter(self,name:str,mod): self._place(name, mod, self.__filters) - def _placeAnalyzer(self,name,mod): + def _placeAnalyzer(self,name:str,mod): self._place(name, mod, self.__analyzers) - def _placePath(self,name,mod): + def _placePath(self,name:str,mod): self._validateSequence(mod, name) try: self._place(name, mod, self.__paths) except ModuleCloneError as msg: context = format_outerframe(4) raise Exception("%sThe module %s in path %s is unknown to the process %s." %(context, msg, name, self._Process__name)) - def _placeEndPath(self,name,mod): + def _placeEndPath(self,name:str,mod): self._validateSequence(mod, name) try: self._place(name, mod, self.__endpaths) except ModuleCloneError as msg: context = format_outerframe(4) raise Exception("%sThe module %s in endpath %s is unknown to the process %s." %(context, msg, name, self._Process__name)) - def _placeFinalPath(self,name,mod): + def _placeFinalPath(self,name:str,mod): self._validateSequence(mod, name) try: self._place(name, mod, self.__finalpaths) except ModuleCloneError as msg: context = format_outerframe(4) raise Exception("%sThe module %s in finalpath %s is unknown to the process %s." %(context, msg, name, self._Process__name)) - def _placeSequence(self,name,mod): + def _placeSequence(self,name:str,mod): self._validateSequence(mod, name) self._place(name, mod, self.__sequences) - def _placeESProducer(self,name,mod): + def _placeESProducer(self,name:str,mod): self._place(name, mod, self.__esproducers) - def _placeESPrefer(self,name,mod): + def _placeESPrefer(self,name:str,mod): self._place(name, mod, self.__esprefers) - def _placeESSource(self,name,mod): + def _placeESSource(self,name:str,mod): self._place(name, mod, self.__essources) - def _placeTask(self,name,task): + def _placeTask(self,name:str,task): self._validateTask(task, name) self._place(name, task, self.__tasks) - def _placeConditionalTask(self,name,task): + def _placeConditionalTask(self,name:str,task): self._validateConditionalTask(task, name) self._place(name, task, self.__conditionaltasks) - def _placeAlias(self,name,mod): + def _placeAlias(self,name:str,mod): self._place(name, mod, self.__aliases) - def _placePSet(self,name,mod): + def _placePSet(self,name:str,mod): self._place(name, mod, self.__psets) - def _placeVPSet(self,name,mod): + def _placeVPSet(self,name:str,mod): self._place(name, mod, self.__vpsets) - def _placeSource(self,name,mod): + def _placeSource(self,name:str,mod): """Allow the source to be referenced by 'source' or by type name""" if name != 'source': raise ValueError("The label '"+name+"' can not be used for a Source. Only 'source' is allowed.") @@ -738,25 +739,25 @@ def _placeSource(self,name,mod): del self.__dict__[self.__dict__['_Process__source'].type_()] self.__dict__['_Process__source'] = mod self.__dict__[mod.type_()] = mod - def _placeLooper(self,name,mod): + def _placeLooper(self,name:str,mod): if name != 'looper': raise ValueError("The label '"+name+"' can not be used for a Looper. Only 'looper' is allowed.") self.__dict__['_Process__looper'] = mod self.__dict__[mod.type_()] = mod - def _placeSubProcess(self,name,mod): + def _placeSubProcess(self,name:str,mod): self.__dict__['_Process__subProcess'] = mod self.__dict__[mod.type_()] = mod def addSubProcess(self,mod): self.__subProcesses.append(mod) - def _placeService(self,typeName,mod): + def _placeService(self,typeName:str,mod): self._place(typeName, mod, self.__services) if typeName in self.__dict__: self.__dict__[typeName]._inProcess = False self.__dict__[typeName]=mod - def _placeAccelerator(self,typeName,mod): + def _placeAccelerator(self,typeName:str,mod): self._place(typeName, mod, self.__accelerators) self.__dict__[typeName]=mod - def load(self, moduleName): + def load(self, moduleName:str): moduleName = moduleName.replace("/",".") module = __import__(moduleName) self.extend(sys.modules[moduleName]) @@ -814,19 +815,19 @@ def extend(self,other,items=()): self.__dict__['_Process__InExtendCall'] = False - def _dumpConfigNamedList(self,items,typeName,options): + def _dumpConfigNamedList(self,items,typeName:str,options:PrintOptions) -> str: returnValue = '' for name,item in items: returnValue +=options.indentation()+typeName+' '+name+' = '+item.dumpConfig(options) return returnValue - def _dumpConfigUnnamedList(self,items,typeName,options): + def _dumpConfigUnnamedList(self,items,typeName:str,options:PrintOptions) -> str: returnValue = '' for name,item in items: returnValue +=options.indentation()+typeName+' = '+item.dumpConfig(options) return returnValue - def _dumpConfigOptionallyNamedList(self,items,typeName,options): + def _dumpConfigOptionallyNamedList(self,items,typeName:str,options:PrintOptions) -> str: returnValue = '' for name,item in items: if name == item.type_(): @@ -834,7 +835,7 @@ def _dumpConfigOptionallyNamedList(self,items,typeName,options): returnValue +=options.indentation()+typeName+' '+name+' = '+item.dumpConfig(options) return returnValue - def dumpConfig(self, options=PrintOptions()): + def dumpConfig(self, options:PrintOptions=PrintOptions()) -> str: """return a string containing the equivalent process defined using the old configuration language""" config = "process "+self.__name+" = {\n" options.indent() @@ -903,19 +904,19 @@ def dumpConfig(self, options=PrintOptions()): options.unindent() return config - def _dumpConfigESPrefers(self, options): + def _dumpConfigESPrefers(self, options:PrintOptions) -> str: result = '' for item in self.es_prefers_().values(): result +=options.indentation()+'es_prefer '+item.targetLabel_()+' = '+item.dumpConfig(options) return result - def _dumpPythonSubProcesses(self, l, options): + def _dumpPythonSubProcesses(self, l, options:PrintOptions) -> str: returnValue = '' for item in l: returnValue += item.dumpPython(options)+'\n\n' return returnValue - def _dumpPythonList(self, d, options): + def _dumpPythonList(self, d, options:PrintOptions) -> str: returnValue = '' if isinstance(d, DictTypes.SortedKeysDict): for name,item in d.items(): @@ -925,7 +926,7 @@ def _dumpPythonList(self, d, options): returnValue +='process.'+name+' = '+item.dumpPython(options)+'\n\n' return returnValue - def _splitPythonList(self, subfolder, d, options): + def _splitPythonList(self, subfolder, d, options:PrintOptions) -> str: parts = DictTypes.SortedKeysDict() for name, item in d.items() if isinstance(d, DictTypes.SortedKeysDict) else sorted(d.items()): code = '' @@ -955,7 +956,7 @@ def _validateSequence(self, sequence, label): except Exception as e: raise RuntimeError("An entry in sequence {} has no label\n Seen entries: {}\n Error: {}".format(label, l, e)) - def _validateTask(self, task, label): + def _validateTask(self, task, label:str): # See if every module and service has been inserted into the process try: l = set() @@ -963,7 +964,7 @@ def _validateTask(self, task, label): task.visit(visitor) except: raise RuntimeError("An entry in task " + label + ' has not been attached to the process') - def _validateConditionalTask(self, task, label): + def _validateConditionalTask(self, task, label:str): # See if every module and service has been inserted into the process try: l = set() @@ -1038,19 +1039,19 @@ def _itemsInDependencyOrder(self, processDictionaryOfItems): deps2.remove(label) return returnValue - def _dumpPython(self, d, options): + def _dumpPython(self, d, options:PrintOptions) -> str: result = '' for name, value in sorted(d.items()): result += value.dumpPythonAs(name,options)+'\n' return result - def _splitPython(self, subfolder, d, options): + def _splitPython(self, subfolder, d, options:PrintOptions) -> dict: result = {} for name, value in sorted(d.items()): result[name] = subfolder, value.dumpPythonAs(name, options) + '\n' return result - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options=PrintOptions()) -> str: """return a string containing the equivalent process defined using python""" specialImportRegistry._reset() header = "import FWCore.ParameterSet.Config as cms" @@ -1087,7 +1088,7 @@ def dumpPython(self, options=PrintOptions()): header += "\n\n" return header+result - def splitPython(self, options = PrintOptions()): + def splitPython(self, options:PrintOptions = PrintOptions()) -> dict: """return a map of file names to python configuration fragments""" specialImportRegistry._reset() # extract individual fragments @@ -1158,7 +1159,7 @@ def splitPython(self, options = PrintOptions()): files['-'] = header + '\n\n' + result return files - def _replaceInSequences(self, label, new): + def _replaceInSequences(self, label:str, new): old = getattr(self,label) #TODO - replace by iterator concatenation #to ovoid dependency problems between sequences, first modify @@ -1175,26 +1176,26 @@ def _replaceInSequences(self, label, new): sequenceable.replace(old,new) for sequenceable in self.finalpaths.values(): sequenceable.replace(old,new) - def _replaceInTasks(self, label, new): + def _replaceInTasks(self, label:str, new): old = getattr(self,label) for task in self.tasks.values(): task.replace(old, new) - def _replaceInConditionalTasks(self, label, new): + def _replaceInConditionalTasks(self, label:str, new): old = getattr(self,label) for task in self.conditionaltasks.values(): task.replace(old, new) - def _replaceInSchedule(self, label, new): + def _replaceInSchedule(self, label:str, new): if self.schedule_() == None: return old = getattr(self,label) for task in self.schedule_()._tasks: task.replace(old, new) - def _replaceInScheduleDirectly(self, label, new): + def _replaceInScheduleDirectly(self, label:str, new): if self.schedule_() == None: return old = getattr(self,label) self.schedule_()._replaceIfHeldDirectly(old, new) - def globalReplace(self,label,new): + def globalReplace(self,label:str,new): """ Replace the item with label 'label' by object 'new' in the process and all sequences/paths/tasks""" if not hasattr(self,label): raise LookupError("process has no item of label "+label) @@ -1202,14 +1203,14 @@ def globalReplace(self,label,new): def _insertInto(self, parameterSet, itemDict): for name,value in itemDict.items(): value.insertInto(parameterSet, name) - def _insertOneInto(self, parameterSet, label, item, tracked): + def _insertOneInto(self, parameterSet, label:str, item, tracked:bool): vitems = [] if not item == None: newlabel = item.nameInProcessDesc_(label) vitems = [newlabel] item.insertInto(parameterSet, newlabel) parameterSet.addVString(tracked, label, vitems) - def _insertManyInto(self, parameterSet, label, itemDict, tracked): + def _insertManyInto(self, parameterSet, label:str, itemDict, tracked:bool): l = [] for name,value in itemDict.items(): value.appendToProcessDescList_(l, name) @@ -1217,7 +1218,7 @@ def _insertManyInto(self, parameterSet, label, itemDict, tracked): # alphabetical order is easier to compare with old language l.sort() parameterSet.addVString(tracked, label, l) - def _insertSwitchProducersInto(self, parameterSet, labelModules, labelAliases, itemDict, tracked): + def _insertSwitchProducersInto(self, parameterSet, labelModules, labelAliases, itemDict, tracked:bool): modules = parameterSet.getVString(tracked, labelModules) aliases = parameterSet.getVString(tracked, labelAliases) accelerators = parameterSet.getVString(False, "@selected_accelerators") @@ -1228,7 +1229,7 @@ def _insertSwitchProducersInto(self, parameterSet, labelModules, labelAliases, i aliases.sort() parameterSet.addVString(tracked, labelModules, modules) parameterSet.addVString(tracked, labelAliases, aliases) - def _insertSubProcessesInto(self, parameterSet, label, itemList, tracked): + def _insertSubProcessesInto(self, parameterSet, label:str, itemList, tracked:bool): l = [] subprocs = [] for value in itemList: @@ -1336,7 +1337,7 @@ def _insertPaths(self, processPSet, nodeVisitor): processPSet.addVString(False, "@filters_on_endpaths", endpathValidator.filtersOnEndpaths) - def resolve(self,keepUnresolvedSequencePlaceholders=False): + def resolve(self,keepUnresolvedSequencePlaceholders:bool=False): for x in self.paths.values(): x.resolve(self.__dict__,keepUnresolvedSequencePlaceholders) for x in self.endpaths.values(): @@ -1347,7 +1348,7 @@ def resolve(self,keepUnresolvedSequencePlaceholders=False): for task in self.schedule_()._tasks: task.resolve(self.__dict__,keepUnresolvedSequencePlaceholders) - def prune(self,verbose=False,keepUnresolvedSequencePlaceholders=False): + def prune(self,verbose=False,keepUnresolvedSequencePlaceholders:bool=False): """ Remove clutter from the process that we think is unnecessary: tracked PSets, VPSets and unused modules and sequences. If a Schedule has been set, then Paths and EndPaths not in the schedule will also be removed, along with an modules and sequences used only by @@ -1614,7 +1615,7 @@ def prefer(self, esmodule,*args,**kargs): else: raise RuntimeError("Cannot resolve prefer for "+repr(esmodule)) - def _findPreferred(self, esname, d,*args,**kargs): + def _findPreferred(self, esname:str, d,*args,**kargs) -> bool: # is esname a name in the dictionary? if esname in d: typ = d[esname].type_() @@ -1636,7 +1637,7 @@ def _findPreferred(self, esname, d,*args,**kargs): class ProcessFragment(object): - def __init__(self, process): + def __init__(self, process: Union[Process,str]): if isinstance(process, Process): self.__process = process elif isinstance(process, str): @@ -1649,17 +1650,17 @@ def __init__(self, process): raise TypeError('a ProcessFragment can only be constructed from an existig Process or from process name') def __dir__(self): return [ x for x in dir(self.__process) if isinstance(getattr(self.__process, x), _ConfigureComponent) ] - def __getattribute__(self, name): + def __getattribute__(self, name:str): if name == '_ProcessFragment__process': return object.__getattribute__(self, '_ProcessFragment__process') else: return getattr(self.__process, name) - def __setattr__(self, name, value): + def __setattr__(self, name:str, value): if name == '_ProcessFragment__process': object.__setattr__(self, name, value) else: setattr(self.__process, name, value) - def __delattr__(self, name): + def __delattr__(self, name:str): if name == '_ProcessFragment__process': pass else: @@ -1692,7 +1693,7 @@ def __new__(cls, *args, **kw): return new def __init__(self, *args, **kw): pass - def __repr__(self): + def __repr__(self) -> str: return "FilteredStream object: %s" %self["name"] def __getattr__(self,attr): return self[attr] @@ -1722,14 +1723,14 @@ def __init__(self,process, SelectEvents = untracked.PSet(), outputCommands = unt This particular service may not be reconfigured in a subprocess. The reconfiguration will be ignored.""") del self.__process.MessageLogger - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: out = "parentProcess"+str(hash(self))+" = process\n" out += self.__process.dumpPython() out += "childProcess = process\n" out += "process = parentProcess"+str(hash(self))+"\n" out += "process.addSubProcess(cms.SubProcess(process = childProcess, SelectEvents = "+self.__SelectEvents.dumpPython(options) +", outputCommands = "+self.__outputCommands.dumpPython(options) +"))" return out - def getProcessName(self): + def getProcessName(self) -> str: return self.__process.name_() def process(self): return self.__process @@ -1739,7 +1740,7 @@ def outputCommands(self): return self.__outputCommands def type_(self): return 'subProcess' - def nameInProcessDesc_(self,label): + def nameInProcessDesc_(self,label:str) -> str: return label def _place(self,label,process): process._placeSubProcess('subProcess',self) @@ -1804,21 +1805,21 @@ class _AndModifier(_BoolModifierBase): """A modifier which only applies if multiple Modifiers are chosen""" def __init__(self, lhs, rhs): super(_AndModifier,self).__init__(lhs, rhs) - def _isChosen(self): + def _isChosen(self) -> bool: return self._lhs._isChosen() and self._rhs._isChosen() class _InvertModifier(_BoolModifierBase): """A modifier which only applies if a Modifier is not chosen""" def __init__(self, lhs): super(_InvertModifier,self).__init__(lhs) - def _isChosen(self): + def _isChosen(self) -> bool: return not self._lhs._isChosen() class _OrModifier(_BoolModifierBase): """A modifier which only applies if at least one of multiple Modifiers is chosen""" def __init__(self, lhs, rhs): super(_OrModifier,self).__init__(lhs, rhs) - def _isChosen(self): + def _isChosen(self) -> bool: return self._lhs._isChosen() or self._rhs._isChosen() @@ -1903,7 +1904,7 @@ def _toReplaceWith(toObj,fromObj): def _setChosen(self): """Should only be called by cms.Process instances""" self.__chosen = True - def _isChosen(self): + def _isChosen(self) -> bool: return self.__chosen def __and__(self, other): return _AndModifier(self,other) @@ -1931,7 +1932,7 @@ def _setChosen(self): self.__chosen = True for m in self.__chain: m._setChosen() - def _isChosen(self): + def _isChosen(self) -> bool: return self.__chosen def copyAndExclude(self, toExclude): """Creates a new ModifierChain which is a copy of @@ -2006,7 +2007,7 @@ def _place(self, name, proc): proc._placeAccelerator(self.type_(), self) def type_(self): return type(self).__name__ - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: specialImportRegistry.registerUse(self) result = self.__class__.__name__+"(" # not including cms. since the deriving classes are not in cms "namespace" options.indent() @@ -2018,7 +2019,7 @@ def dumpPython(self, options=PrintOptions()): return result # The following methods are hooks to be overridden (if needed) in the deriving class - def dumpPythonImpl(self, options): + def dumpPythonImpl(self, options) -> str: """Override if need to add any 'body' content to dumpPython(). Returns a string.""" return "" def labels(self): @@ -2063,14 +2064,14 @@ def __getattr__(self, label): if not isinstance(value, Service): raise TypeError("ProcessAccelerator.apply() can get only Services. Tried to get {} with label {}".format(str(type(value)), label)) return value - def __setattr__(self, label, value): + def __setattr__(self, label:str, value): if label == "_ProcessForProcessAccelerator__process": super().__setattr__(label, value) else: if not isinstance(value, Service): raise TypeError("ProcessAccelerator.apply() can only set Services. Tried to set {} with label {}".format(str(type(value)), label)) setattr(self.__process, label, value) - def __delattr__(self, label): + def __delattr__(self, label:str): value = getattr(self.__process, label) if not isinstance(value, Service): raise TypeError("ProcessAccelerator.apply() can delete only Services. Tried to del {} with label {}".format(str(type(value)), label)) diff --git a/FWCore/ParameterSet/python/Mixins.py b/FWCore/ParameterSet/python/Mixins.py index 0d6957bccbc2f..2de525e102455 100644 --- a/FWCore/ParameterSet/python/Mixins.py +++ b/FWCore/ParameterSet/python/Mixins.py @@ -1,20 +1,21 @@ from __future__ import print_function from builtins import range, object import inspect +from typing import Union class _ConfigureComponent(object): """Denotes a class that can be used by the Processes class""" - def _isTaskComponent(self): + def _isTaskComponent(self) -> bool: return False class PrintOptions(object): - def __init__(self, indent = 0, deltaIndent = 4, process = True, targetDirectory = None, useSubdirectories = False): + def __init__(self, indent:int = 0, deltaIndent:int = 4, process:bool = True, targetDirectory: Union[str, None] = None, useSubdirectories:bool = False): self.indent_= indent self.deltaIndent_ = deltaIndent self.isCfg = process self.targetDirectory = targetDirectory self.useSubdirectories = useSubdirectories - def indentation(self): + def indentation(self) -> str: return ' '*self.indent_ def indent(self): self.indent_ += self.deltaIndent_ @@ -58,32 +59,32 @@ def __init__(self): self.__dict__["_isFrozen"] = False self.__isTracked = True self._isModified = False - def isModified(self): + def isModified(self) -> bool: return self._isModified def resetModified(self): self._isModified=False - def configTypeName(self): + def configTypeName(self) -> str: if self.isTracked(): return type(self).__name__ return 'untracked '+type(self).__name__ - def pythonTypeName(self): + def pythonTypeName(self) -> str: if self.isTracked(): return 'cms.'+type(self).__name__ return 'cms.untracked.'+type(self).__name__ - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: specialImportRegistry.registerUse(self) return self.pythonTypeName()+"("+self.pythonValue(options)+")" - def __repr__(self): + def __repr__(self) -> str: return self.dumpPython() - def isTracked(self): + def isTracked(self) -> bool: return self.__isTracked - def setIsTracked(self,trackness): + def setIsTracked(self,trackness:bool): self.__isTracked = trackness - def isFrozen(self): + def isFrozen(self) -> bool: return self._isFrozen def setIsFrozen(self): self._isFrozen = True - def isCompatibleCMSType(self,aType): + def isCompatibleCMSType(self,aType) -> bool: return isinstance(self,aType) def _checkAndReturnValueWithType(self, valueWithType): if isinstance(valueWithType, type(self)): @@ -106,31 +107,31 @@ def setValue(self,value): if value!=self._value: self._isModified=True self._value=value - def configValue(self, options=PrintOptions()): + def configValue(self, options:PrintOptions=PrintOptions()) -> str: return str(self._value) - def pythonValue(self, options=PrintOptions()): + def pythonValue(self, options:PrintOptions=PrintOptions()) -> str: return self.configValue(options) - def __eq__(self,other): + def __eq__(self,other) -> bool: if isinstance(other,_SimpleParameterTypeBase): return self._value == other._value return self._value == other - def __ne__(self,other): + def __ne__(self,other) -> bool: if isinstance(other,_SimpleParameterTypeBase): return self._value != other._value return self._value != other - def __lt__(self,other): + def __lt__(self,other) -> bool: if isinstance(other,_SimpleParameterTypeBase): return self._value < other._value return self._value < other - def __le__(self,other): + def __le__(self,other) -> bool: if isinstance(other,_SimpleParameterTypeBase): return self._value <= other._value return self._value <= other - def __gt__(self,other): + def __gt__(self,other) -> bool: if isinstance(other,_SimpleParameterTypeBase): return self._value > other._value return self._value > other - def __ge__(self,other): + def __ge__(self,other) -> bool: if isinstance(other,_SimpleParameterTypeBase): return self._value >= other._value return self._value >= other @@ -140,25 +141,25 @@ class UsingBlock(_SimpleParameterTypeBase): """For injection purposes, pretend this is a new parameter type then have a post process step which strips these out """ - def __init__(self,value, s='', loc=0, file=''): + def __init__(self,value, s:str='', loc:int=0, file:str=''): super(UsingBlock,self).__init__(value) self.s = s self.loc = loc self.file = file self.isResolved = False @staticmethod - def _isValid(value): + def _isValid(value) -> bool: return isinstance(value,str) - def _valueFromString(value): + def _valueFromString(value) -> str: """only used for cfg-parsing""" - return string(value) - def insertInto(self, parameterSet, myname): + return str(value) + def insertInto(self, parameterSet, myname:str): value = self.value() # doesn't seem to handle \0 correctly #if value == '\0': # value = '' parameterSet.addString(self.isTracked(), myname, value) - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: if options.isCfg: return "process."+self.value() else: @@ -188,7 +189,7 @@ def __init__(self,*arg,**kargs): def parameterNames_(self): """Returns the name of the parameters""" return self.__parameterNames[:] - def isModified(self): + def isModified(self) -> bool: if self._isModified: return True for name in self.parameterNames_(): @@ -198,7 +199,7 @@ def isModified(self): return True return False - def hasParameter(self, params): + def hasParameter(self, params) -> bool: """ _hasParameter_ @@ -239,7 +240,7 @@ def parameters_(self): result[name]=copy.deepcopy(self.__dict__[name]) return result - def __addParameter(self, name, value): + def __addParameter(self, name:str, value): if name == 'allowAnyLabel_': self.__validator = value self._isModified = True @@ -267,7 +268,7 @@ def __setParameters(self,parameters): self.__addParameter(name, value) if v is not None: self.__validator=v - def __setattr__(self,name,value): + def __setattr__(self,name:str,value): #since labels are not supposed to have underscores at the beginning # I will assume that if we have such then we are setting an internal variable if self.isFrozen() and not (name in ["_Labelable__label","_isFrozen"] or name.startswith('_')): @@ -290,21 +291,21 @@ def __setattr__(self,name,value): self.__dict__[name].setValue(value) self._isModified = True - def isFrozen(self): + def isFrozen(self) -> bool: return self._isFrozen def setIsFrozen(self): self._isFrozen = True for name in self.parameterNames_(): self.__dict__[name].setIsFrozen() - def __delattr__(self,name): + def __delattr__(self,name:str): if self.isFrozen(): raise ValueError("Object already added to a process. It is read only now") super(_Parameterizable,self).__delattr__(name) self.__parameterNames.remove(name) @staticmethod - def __raiseBadSetAttr(name): + def __raiseBadSetAttr(name:str): raise TypeError(name+" does not already exist, so it can only be set to a CMS python configuration type") - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: specialImportRegistry.registerUse(self) sortedNames = sorted(self.parameterNames_()) if len(sortedNames) > 200: @@ -376,7 +377,7 @@ def dumpPython(self, options=PrintOptions()): resultList.append(options.indentation()+"allowAnyLabel_="+self.__validator.dumpPython(options)) options.unindent() return ',\n'.join(resultList)+'\n' - def __repr__(self): + def __repr__(self) -> str: return self.dumpPython() def insertContentsInto(self, parameterSet): for name in self.parameterNames_(): @@ -395,7 +396,7 @@ def __init__(self,type_,*arg,**kargs): # del args['type_'] super(_TypedParameterizable,self).__init__(*arg,**kargs) saveOrigin(self, 1) - def _place(self,name,proc): + def _place(self,name:str,proc): self._placeImpl(name,proc) def type_(self): """returns the type of the object, e.g. 'FooProducer'""" @@ -444,7 +445,7 @@ def clone(self, *args, **params): return returnValue @staticmethod - def __findDefaultsFor(label,type): + def __findDefaultsFor(label:str,type): #This routine is no longer used, but I might revive it in the future import sys import glob @@ -477,7 +478,7 @@ def __findDefaultsFor(label,type): def directDependencies(self): return [] - def dumpConfig(self, options=PrintOptions()): + def dumpConfig(self, options:PrintOptions=PrintOptions()) -> str: config = self.__type +' { \n' for name in self.parameterNames_(): param = self.__dict__[name] @@ -487,7 +488,7 @@ def dumpConfig(self, options=PrintOptions()): config += options.indentation()+'}\n' return config - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: specialImportRegistry.registerUse(self) result = "cms."+str(type(self).__name__)+'("'+self.type_()+'"' nparam = len(self.parameterNames_()) @@ -497,7 +498,7 @@ def dumpPython(self, options=PrintOptions()): result += ",\n"+_Parameterizable.dumpPython(self,options)+options.indentation() + ")\n" return result - def dumpPythonAttributes(self, myname, options): + def dumpPythonAttributes(self, myname:str, options:PrintOptions) -> str: """ dumps the object with all attributes declared after the constructor""" result = "" for name in sorted(self.parameterNames_()): @@ -505,13 +506,13 @@ def dumpPythonAttributes(self, myname, options): result += options.indentation() + myname + "." + name + " = " + param.dumpPython(options) + "\n" return result - def nameInProcessDesc_(self, myname): + def nameInProcessDesc_(self, myname:str): return myname; - def moduleLabel_(self, myname): + def moduleLabel_(self, myname:str): return myname - def appendToProcessDescList_(self, lst, myname): + def appendToProcessDescList_(self, lst, myname:str): lst.append(self.nameInProcessDesc_(myname)) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): newpset = parameterSet.newPSet() newpset.addString(True, "@module_label", self.moduleLabel_(myname)) newpset.addString(True, "@module_type", self.type_()) @@ -523,13 +524,13 @@ def insertInto(self, parameterSet, myname): class _Labelable(object): """A 'mixin' used to denote that the class can be paired with a label (e.g. an EDProducer)""" - def label_(self): + def label_(self) -> str: if not hasattr(self, "_Labelable__label"): raise RuntimeError("module has no label. Perhaps it wasn't inserted into the process?") return self.__label - def hasLabel_(self): + def hasLabel_(self) -> bool: return hasattr(self, "_Labelable__label") and self.__label is not None - def setLabel(self,label): + def setLabel(self,label:str): if self.hasLabel_() : if self.label_() != label and label is not None : msg100 = "Attempting to change the label of a Labelable object, possibly an attribute of the Process\n" @@ -547,7 +548,7 @@ def setLabel(self,label): msg112 = " 4. Compose Sequences: newName = cms.Sequence(oldName)\n" raise ValueError(msg100+msg101+msg102+msg103+msg104+msg105+msg106+msg107+msg108+msg109+msg110+msg111+msg112) self.__label = label - def label(self): + def label(self) -> str: #print "WARNING: _Labelable::label() needs to be changed to label_()" return self.__label def __str__(self): @@ -557,7 +558,7 @@ def __str__(self): return str(self.__label) def dumpSequenceConfig(self): return str(self.__label) - def dumpSequencePython(self, options=PrintOptions()): + def dumpSequencePython(self, options:PrintOptions=PrintOptions()): if options.isCfg: return 'process.'+str(self.__label) else: @@ -599,7 +600,7 @@ def __setitem__(self,key,value): raise TypeError("can not insert the type "+str(type(value))+" in container "+self._labelIfAny()) super(_ValidatingListBase,self).__setitem__(key,value) @classmethod - def _isValid(cls,seq): + def _isValid(cls,seq) -> bool: # see if strings get reinterpreted as lists if isinstance(seq, str): return False @@ -633,7 +634,7 @@ def insert(self,i,x): if not self._itemIsValid(x): raise TypeError("wrong type being inserted to container "+self._labelIfAny()) super(_ValidatingListBase,self).insert(i,self._itemFromArgument(x)) - def _labelIfAny(self): + def _labelIfAny(self) -> str: result = type(self).__name__ if hasattr(self, '__label'): result += ' ' + self.__label @@ -654,7 +655,7 @@ def setValue(self,v): self[:] = [] self.extend(v) self._isModified=True - def configValue(self, options=PrintOptions()): + def configValue(self, options:PrintOptions=PrintOptions()) -> str: config = '{\n' first = True for value in iter(self): @@ -667,13 +668,13 @@ def configValue(self, options=PrintOptions()): options.unindent() config += options.indentation()+'}\n' return config - def configValueForItem(self,item, options): + def configValueForItem(self,item, options:PrintOptions) -> str: return str(item) - def pythonValueForItem(self,item, options): + def pythonValueForItem(self,item, options:PrintOptions) -> str: return self.configValueForItem(item, options) def __repr__(self): return self.dumpPython() - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: specialImportRegistry.registerUse(self) result = self.pythonTypeName()+"(" n = len(self) diff --git a/FWCore/ParameterSet/python/Modules.py b/FWCore/ParameterSet/python/Modules.py index 8e3ee9f1122d9..cef1799a78918 100644 --- a/FWCore/ParameterSet/python/Modules.py +++ b/FWCore/ParameterSet/python/Modules.py @@ -12,7 +12,7 @@ class Service(_ConfigureComponent,_TypedParameterizable,_Unlabelable): def __init__(self,type_,*arg,**kargs): super(Service,self).__init__(type_,*arg,**kargs) self._inProcess = False - def _placeImpl(self,name,proc): + def _placeImpl(self,name:str,proc): self._inProcess = True proc._placeService(self.type_(),self) def insertInto(self, processDesc): @@ -20,11 +20,11 @@ def insertInto(self, processDesc): newpset.addString(True, "@service_type", self.type_()) self.insertContentsInto(newpset) processDesc.addService(newpset) - def dumpSequencePython(self, options=PrintOptions()): + def dumpSequencePython(self, options:PrintOptions=PrintOptions()) -> str: return "process." + self.type_() - def _isTaskComponent(self): + def _isTaskComponent(self) -> bool: return True - def isLeaf(self): + def isLeaf(self) -> bool: return True def __str__(self): return str(self.type_()) @@ -33,41 +33,41 @@ class ESSource(_ConfigureComponent,_TypedParameterizable,_Unlabelable,_Labelable def __init__(self,type_,*arg,**kargs): super(ESSource,self).__init__(type_,*arg,**kargs) saveOrigin(self, 1) - def _placeImpl(self,name,proc): + def _placeImpl(self,name:str,proc): if name == '': name=self.type_() proc._placeESSource(name,self) - def moduleLabel_(self,myname): + def moduleLabel_(self,myname:str) -> str: result = myname if self.type_() == myname: result = "" return result - def nameInProcessDesc_(self, myname): + def nameInProcessDesc_(self, myname:str) -> str: result = self.type_() + "@" + self.moduleLabel_(myname) return result - def _isTaskComponent(self): + def _isTaskComponent(self) -> bool: return True - def isLeaf(self): + def isLeaf(self) -> bool: return True class ESProducer(_ConfigureComponent,_TypedParameterizable,_Unlabelable,_Labelable): def __init__(self,type_,*arg,**kargs): super(ESProducer,self).__init__(type_,*arg,**kargs) - def _placeImpl(self,name,proc): + def _placeImpl(self,name:str,proc): if name == '': name=self.type_() proc._placeESProducer(name,self) - def moduleLabel_(self,myname): + def moduleLabel_(self,myname:str) -> str: result = myname if self.type_() == myname: result = '' return result - def nameInProcessDesc_(self, myname): + def nameInProcessDesc_(self, myname:str) -> str: result = self.type_() + "@" + self.moduleLabel_(myname) return result - def _isTaskComponent(self): + def _isTaskComponent(self) -> bool: return True - def isLeaf(self): + def isLeaf(self) -> bool: return True class ESPrefer(_ConfigureComponent,_TypedParameterizable,_Unlabelable,_Labelable): @@ -87,7 +87,7 @@ class ESPrefer(_ConfigureComponent,_TypedParameterizable,_Unlabelable,_Labelable #prefer only "Orange" data with label "ExtraPulp" in "OrangeRecord" from "juicer" ESPrefer("ESJuicerProd", OrangeRecord=cms.vstring("Orange/ExtraPulp")) """ - def __init__(self,type_,targetLabel='',*arg,**kargs): + def __init__(self,type_,targetLabel:str='',*arg,**kargs): super(ESPrefer,self).__init__(type_,*arg,**kargs) self._targetLabel = targetLabel if targetLabel is None: @@ -96,20 +96,20 @@ def __init__(self,type_,targetLabel='',*arg,**kargs): for k,v in kargs.items(): if not isinstance(v,vstring): raise RuntimeError('ESPrefer only allows vstring attributes. "'+k+'" is a '+str(type(v))) - def _placeImpl(self,name,proc): + def _placeImpl(self,name:str,proc): proc._placeESPrefer(name,self) - def nameInProcessDesc_(self, myname): + def nameInProcessDesc_(self, myname:str) -> str: # the C++ parser can give it a name like "label@prefer". Get rid of that. return "esprefer_" + self.type_() + "@" + self._targetLabel def copy(self): returnValue = ESPrefer.__new__(type(self)) returnValue.__init__(self.type_(), self._targetLabel) return returnValue - def moduleLabel_(self, myname): + def moduleLabel_(self, myname:str) -> str: return self._targetLabel - def targetLabel_(self): + def targetLabel_(self) -> str: return self._targetLabel - def dumpPythonAs(self, label, options=PrintOptions()): + def dumpPythonAs(self, label, options:PrintOptions=PrintOptions()) -> str: result = options.indentation() basename = self._targetLabel if basename == '': @@ -151,7 +151,7 @@ def _errorstr(self): def setPrerequisites(self, *libs): self.__dict__["libraries_"] = libs - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): if "libraries_" in self.__dict__: from ctypes import LibraryLoader, CDLL import platform @@ -163,7 +163,7 @@ def insertInto(self, parameterSet, myname): class EDProducer(_Module): def __init__(self,type_,*arg,**kargs): super(EDProducer,self).__init__(type_,*arg,**kargs) - def _placeImpl(self,name,proc): + def _placeImpl(self,name:str,proc): proc._placeProducer(name,self) def _isTaskComponent(self): return True @@ -171,7 +171,7 @@ def _isTaskComponent(self): class EDFilter(_Module): def __init__(self,type_,*arg,**kargs): super(EDFilter,self).__init__(type_,*arg,**kargs) - def _placeImpl(self,name,proc): + def _placeImpl(self,name:str,proc): proc._placeFilter(name,self) def _isTaskComponent(self): return True @@ -179,36 +179,36 @@ def _isTaskComponent(self): class EDAnalyzer(_Module): def __init__(self,type_,*arg,**kargs): super(EDAnalyzer,self).__init__(type_,*arg,**kargs) - def _placeImpl(self,name,proc): + def _placeImpl(self,name:str,proc): proc._placeAnalyzer(name,self) class OutputModule(_Module): def __init__(self,type_,*arg,**kargs): super(OutputModule,self).__init__(type_,*arg,**kargs) - def _placeImpl(self,name,proc): + def _placeImpl(self,name:str,proc): proc._placeOutputModule(name,self) class Source(_ConfigureComponent,_TypedParameterizable): def __init__(self,type_,*arg,**kargs): super(Source,self).__init__(type_,*arg,**kargs) - def _placeImpl(self,name,proc): + def _placeImpl(self,name:str,proc): proc._placeSource(name,self) - def moduleLabel_(self,myname): + def moduleLabel_(self,myname:str): return "@main_input" - def nameInProcessDesc_(self,myname): + def nameInProcessDesc_(self,myname:str): return "@main_input" class Looper(_ConfigureComponent,_TypedParameterizable): def __init__(self,type_,*arg,**kargs): super(Looper,self).__init__(type_,*arg,**kargs) - def _placeImpl(self,name,proc): + def _placeImpl(self,name:str,proc): proc._placeLooper(name,self) - def moduleLabel_(self,myname): + def moduleLabel_(self,myname:str): return "@main_looper" - def nameInProcessDesc_(self, myname): + def nameInProcessDesc_(self, myname:str): return "@main_looper" @@ -252,7 +252,7 @@ def __init__(self, caseFunctionDict, **kargs): self.__setParameters(kargs) self._isModified = False - def setLabel(self, label): + def setLabel(self, label:str): super().setLabel(label) # SwitchProducer owns the contained modules, and therefore # need to set / unset the label for them explicitly here @@ -282,10 +282,10 @@ def _getProducer(self, accelerators): return self.__dict__[self._chooseCase(accelerators)] @staticmethod - def __typeIsValid(typ): + def __typeIsValid(typ) -> bool: return (isinstance(typ, EDProducer) and not isinstance(typ, SwitchProducer)) or isinstance(typ, EDAlias) - def __addParameter(self, name, value): + def __addParameter(self, name:str, value): if not self.__typeIsValid(value): raise TypeError(name+" does not already exist, so it can only be set to a cms.EDProducer or cms.EDAlias") if name not in self._caseFunctionDict: @@ -305,7 +305,7 @@ def __setParameters(self, parameters): for name, value in parameters.items(): self.__addParameter(name, value) - def __setattr__(self, name, value): + def __setattr__(self, name:str, value): # Following snippet copied and customized from # _Parameterizable in order to support Modifier.toModify # @@ -355,7 +355,7 @@ def clone(self, **params): saveOrigin(returnValue, 1) return returnValue - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: # Note that if anyone uses the generic SwitchProducer instead # of a derived-one, the information on the functions for the # producer decision is lost @@ -374,11 +374,11 @@ def directDependencies(self): # XXX FIXME handle SwitchProducer dependencies return [] - def nameInProcessDesc_(self, myname): + def nameInProcessDesc_(self, myname:str): return myname - def moduleLabel_(self, myname): + def moduleLabel_(self, myname:str): return myname - def caseLabel_(self, name, case): + def caseLabel_(self, name:str, case:str): return name+"@"+case def modulesForConditionalTask_(self): # Need the contained modules (not EDAliases) for ConditionalTask @@ -388,7 +388,7 @@ def modulesForConditionalTask_(self): if not isinstance(caseobj, EDAlias): ret.append(caseobj) return ret - def appendToProcessDescLists_(self, modules, aliases, myname): + def appendToProcessDescLists_(self, modules, aliases, myname:str): # This way we can insert the chosen EDProducer to @all_modules # so that we get easily a worker for it modules.append(myname) @@ -398,7 +398,7 @@ def appendToProcessDescLists_(self, modules, aliases, myname): else: modules.append(self.caseLabel_(myname, case)) - def insertInto(self, parameterSet, myname, accelerators): + def insertInto(self, parameterSet, myname:str, accelerators): for case in self.parameterNames_(): producer = self.__dict__[case] producer.insertInto(parameterSet, self.caseLabel_(myname, case)) @@ -410,7 +410,7 @@ def insertInto(self, parameterSet, myname, accelerators): newpset.addString(False, "@chosen_case", self.caseLabel_(myname, self._chooseCase(accelerators))) parameterSet.addPSet(True, self.nameInProcessDesc_(myname), newpset) - def _placeImpl(self,name,proc): + def _placeImpl(self,name:str,proc): proc._placeSwitchProducer(name,self) # for case in self.parameterNames_(): # caseLabel = self.caseLabel_(name, case) diff --git a/FWCore/ParameterSet/python/ModulesProxy.py b/FWCore/ParameterSet/python/ModulesProxy.py index 910c04236081b..ca8069fea0dd0 100644 --- a/FWCore/ParameterSet/python/ModulesProxy.py +++ b/FWCore/ParameterSet/python/ModulesProxy.py @@ -13,7 +13,7 @@ def __call__(self,**kwargs): return self._caller(**kwargs) -def _setupProxies(fullName): +def _setupProxies(fullName:str): _cmssw_package_name='.'.join(fullName.split(sep)[-3:-1]) basename = fullName.split(sep)[-1] pathname = fullName[:-1*len(basename)] diff --git a/FWCore/ParameterSet/python/SequenceTypes.py b/FWCore/ParameterSet/python/SequenceTypes.py index f72d54cc192e4..d0ffb7a7751a4 100644 --- a/FWCore/ParameterSet/python/SequenceTypes.py +++ b/FWCore/ParameterSet/python/SequenceTypes.py @@ -30,7 +30,7 @@ def _clonesequence(self, lookuptable): return lookuptable[id(self)] except: raise KeyError("no "+str(type(self))+" with id "+str(id(self))+" found") - def resolve(self, processDict,keepIfCannotResolve=False): + def resolve(self, processDict,keepIfCannotResolve:bool=False): return self def isOperation(self): """Returns True if the object is an operator (e.g. *,+ or !) type""" @@ -101,7 +101,7 @@ def isOperation(self): def _visitSubNodes(self,visitor): for i in self._items: i.visitNode(visitor) - def dumpSequencePython(self, options=PrintOptions()): + def dumpSequencePython(self, options:PrintOptions=PrintOptions()): returnValue = '' join = '' operatorJoin =self.operatorString() @@ -162,7 +162,7 @@ def __str__(self): def _appendToCollection(self,collection): collection.extend(self._collection) - def dumpSequencePython(self, options=PrintOptions()): + def dumpSequencePython(self, options:PrintOptions=PrintOptions()) -> str: returnValue = '' separator = '' for item in self._collection: @@ -172,27 +172,27 @@ def dumpSequencePython(self, options=PrintOptions()): separator = '+' return returnValue - def dumpSequenceConfig(self): + def dumpSequenceConfig(self) -> str: returnValue = self._collection[0].dumpSequenceConfig() for m in self._collection[1:]: returnValue += '&'+m.dumpSequenceConfig() return returnValue - def directDependencies(self,sortByType=True): + def directDependencies(self,sortByType:bool=True): return findDirectDependencies(self, self._collection,sortByType=sortByType) def visitNode(self,visitor): for m in self._collection: m.visitNode(visitor) - def resolve(self, processDict,keepIfCannotResolve=False): + def resolve(self, processDict,keepIfCannotResolve:bool=False): self._collection = [x.resolve(processDict,keepIfCannotResolve) for x in self._collection] return self def index(self,item): return self._collection.index(item) - def insert(self,index,item): + def insert(self,index:int,item): self._collection.insert(index,item) def _replaceIfHeldDirectly(self,original,replacement): didReplace = False @@ -211,7 +211,7 @@ def _replaceIfHeldDirectly(self,original,replacement): return didReplace -def findDirectDependencies(element, collection,sortByType=True): +def findDirectDependencies(element, collection,sortByType:bool=True): dependencies = [] for item in collection: # skip null items @@ -296,11 +296,11 @@ def associate(self,*tasks): if not isinstance(task, _TaskBase): raise TypeError("associate only works with objects of type Task") self._tasks.add(task) - def isFrozen(self): + def isFrozen(self) -> bool: return self._isFrozen def setIsFrozen(self): self._isFrozen = True - def _place(self,name,proc): + def _place(self,name:str,proc): self._placeImpl(name,proc) def __imul__(self,rhs): _checkIfSequenceable(self, rhs) @@ -319,18 +319,18 @@ def __str__(self): self.visit(v) return v.resultString() - def dumpConfig(self, options): + def dumpConfig(self, options:PrintOptions) -> str: s = '' if self._seq is not None: s = self._seq.dumpSequenceConfig() return '{'+s+'}\n' - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: """Returns a string which is the python representation of the object""" s = self.dumpPythonNoNewline(options) return s + "\n" - def dumpPythonNoNewline(self, options=PrintOptions()): + def dumpPythonNoNewline(self, options:PrintOptions=PrintOptions()) -> str: s='' if self._seq is not None: s =self._seq.dumpSequencePython(options) @@ -348,7 +348,7 @@ def dumpPythonNoNewline(self, options=PrintOptions()): return 'cms.'+type(self).__name__+'(*['+s+'])' return 'cms.'+type(self).__name__+'('+s+')' - def dumpSequencePython(self, options=PrintOptions()): + def dumpSequencePython(self, options:PrintOptions=PrintOptions()) -> str: """Returns a string which contains the python representation of just the internal sequence""" # only dump the label, if possible if self.hasLabel_(): @@ -362,7 +362,7 @@ def dumpSequencePython(self, options=PrintOptions()): return '' return self.dumpPythonNoNewline(options) - def dumpSequenceConfig(self): + def dumpSequenceConfig(self) -> str: """Returns a string which contains the old config language representation of just the internal sequence""" # only dump the label, if possible if self.hasLabel_(): @@ -379,7 +379,7 @@ def __repr__(self): s = str(self._seq) return "cms."+type(self).__name__+'('+s+')\n' - def directDependencies(self,sortByType=True): + def directDependencies(self,sortByType:bool=True): """Returns the list of modules and other entities that are directly used""" result = [] if self._seq: @@ -508,13 +508,13 @@ def remove(self, something): self._tasks.clear() self.associate(*v.result(self)[1]) return v.didRemove() - def resolve(self, processDict,keepIfCannotResolve=False): + def resolve(self, processDict,keepIfCannotResolve:bool=False): if self._seq is not None: self._seq = self._seq.resolve(processDict,keepIfCannotResolve) for task in self._tasks: task.resolve(processDict,keepIfCannotResolve) return self - def __setattr__(self,name,value): + def __setattr__(self,name:str,value): if not name.startswith("_"): raise AttributeError("You cannot set parameters for sequence like objects.") else: @@ -530,9 +530,9 @@ def __setattr__(self,name,value): #def __contains__(self,item): #"""returns whether or not 'item' is in the sequence""" #def modules_(self): - def nameInProcessDesc_(self, myname): + def nameInProcessDesc_(self, myname:str): return myname - def insertInto(self, parameterSet, myname, decoratedList): + def insertInto(self, parameterSet, myname:str, decoratedList): parameterSet.addVString(True, myname, decoratedList) def visit(self,visitor): """Passes to visitor's 'enter' and 'leave' method each item describing the module sequence. @@ -554,10 +554,10 @@ def __init__(self, operand): raise RuntimeError("This operator cannot accept a sequence") if not isinstance(operand, _Sequenceable): raise RuntimeError("This operator cannot accept a non sequenceable type") - def __eq__(self, other): + def __eq__(self, other) -> bool: # allows replace(~a, b) return type(self) is type(other) and self._operand==other._operand - def __ne__(self, other): + def __ne__(self, other) -> bool: return not self.__eq__(other) def __hash__(self): # this definition implies that self._operand MUST NOT be changed after the construction @@ -566,73 +566,73 @@ def _findDependencies(self,knownDeps, presentDeps): self._operand._findDependencies(knownDeps, presentDeps) def _clonesequence(self, lookuptable): return type(self)(self._operand._clonesequence(lookuptable)) - def _has(self, op): + def _has(self, op) -> bool: return self._operand == op def resolve(self, processDict,keepIfCannotResolve=False): return type(self)(self._operand.resolve(processDict,keepIfCannotResolve)) - def isOperation(self): + def isOperation(self) -> bool: return True def _visitSubNodes(self,visitor): self._operand.visitNode(visitor) - def decoration(self): + def decoration(self) -> str: self._operand.decoration() - def directDependencies(self,sortByType=True): + def directDependencies(self,sortByType:bool=True): return self._operand.directDependencies(sortByType=sortByType) - def label_(self): + def label_(self) -> str: return self._operand.label_() class _SequenceNegation(_UnarySequenceOperator): """Used in the expression tree for a sequence as a stand in for the '!' operator""" def __init__(self, operand): super(_SequenceNegation,self).__init__(operand) - def __str__(self): + def __str__(self) -> str: return '~%s' %self._operand - def dumpSequenceConfig(self): + def dumpSequenceConfig(self) -> str: return '!%s' %self._operand.dumpSequenceConfig() - def dumpSequencePython(self, options=PrintOptions()): + def dumpSequencePython(self, options:PrintOptions=PrintOptions()) -> str: if self._operand.isOperation(): return '~(%s)' %self._operand.dumpSequencePython(options) return '~%s' %self._operand.dumpSequencePython(options) - def decoration(self): + def decoration(self) -> str: return '!' class _SequenceIgnore(_UnarySequenceOperator): """Used in the expression tree for a sequence as a stand in for the '-' operator""" def __init__(self, operand): super(_SequenceIgnore,self).__init__(operand) - def __str__(self): + def __str__(self) -> str: return 'ignore(%s)' %self._operand - def dumpSequenceConfig(self): + def dumpSequenceConfig(self) ->str: return '-%s' %self._operand.dumpSequenceConfig() - def dumpSequencePython(self, options=PrintOptions()): + def dumpSequencePython(self, options:PrintOptions=PrintOptions()) -> str: return 'cms.ignore(%s)' %self._operand.dumpSequencePython(options) - def decoration(self): + def decoration(self) -> str: return '-' class _SequenceWait(_UnarySequenceOperator): """Used in the expression tree for a sequence as a stand in for the '|' operator""" def __init__(self, operand): super(_SequenceWait,self).__init__(operand) - def __str__(self): + def __str__(self) -> str: return 'wait(%s)' %self._operand - def dumpSequenceConfig(self): + def dumpSequenceConfig(self) -> str: return '|%s' %self._operand.dumpSequenceConfig() - def dumpSequencePython(self, options=PrintOptions()): + def dumpSequencePython(self, options:PrintOptions=PrintOptions()) -> str: return 'cms.wait(%s)' %self._operand.dumpSequencePython(options) - def decoration(self): + def decoration(self) -> str: return '|' class _SequenceWaitAndIgnore(_UnarySequenceOperator): """Used in the expression tree for a sequence as a stand in for the '+' operator""" def __init__(self, operand): super(_SequenceWaitAndIgnore,self).__init__(operand) - def __str__(self): + def __str__(self) -> str: return 'wait(ignore(%s))' %self._operand - def dumpSequenceConfig(self): + def dumpSequenceConfig(self) -> str: return '+%s' %self._operand.dumpSequenceConfig() - def dumpSequencePython(self, options=PrintOptions()): + def dumpSequencePython(self, options:PrintOptions=PrintOptions()) -> str: return 'cms.wait(cms.ignore(%s))' %self._operand.dumpSequencePython(options) - def decoration(self): + def decoration(self) -> str: return '+' def ignore(seq): @@ -652,19 +652,19 @@ def wait(seq): class Path(_ModuleSequenceType): def __init__(self,*arg,**argv): super(Path,self).__init__(*arg,**argv) - def _placeImpl(self,name,proc): + def _placeImpl(self,name:str,proc): proc._placePath(name,self) class EndPath(_ModuleSequenceType): def __init__(self,*arg,**argv): super(EndPath,self).__init__(*arg,**argv) - def _placeImpl(self,name,proc): + def _placeImpl(self,name:str,proc): proc._placeEndPath(name,self) class FinalPath(_ModuleSequenceType): def __init__(self,*arg,**argv): super(FinalPath,self).__init__(*arg,**argv) - def _placeImpl(self,name,proc): + def _placeImpl(self,name:str,proc): proc._placeFinalPath(name,self) def associate(self,task): raise TypeError("FinalPath does not allow associations with Tasks") @@ -672,7 +672,7 @@ def associate(self,task): class Sequence(_ModuleSequenceType,_Sequenceable): def __init__(self,*arg,**argv): super(Sequence,self).__init__(*arg,**argv) - def _placeImpl(self,name,proc): + def _placeImpl(self,name:str,proc): proc._placeSequence(name,self) def _clonesequence(self, lookuptable): if id(self) not in lookuptable: @@ -688,16 +688,16 @@ def _clonesequence(self, lookuptable): def _visitSubNodes(self,visitor): self.visit(visitor) class SequencePlaceholder(_Sequenceable): - def __init__(self, name): + def __init__(self, name:str): self._name = name - def _placeImpl(self,name,proc): + def _placeImpl(self,name:str,proc): pass def __str__(self): return self._name - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): raise RuntimeError("The SequencePlaceholder "+self._name +" was never overridden") - def resolve(self, processDict,keepIfCannotResolve=False): + def resolve(self, processDict,keepIfCannotResolve:bool=False): if not self._name in processDict: #print str(processDict.keys()) if keepIfCannotResolve: @@ -720,11 +720,11 @@ def copy(self): returnValue =SequencePlaceholder.__new__(type(self)) returnValue.__init__(self._name) return returnValue - def dumpSequenceConfig(self): + def dumpSequenceConfig(self) -> str: return 'cms.SequencePlaceholder("%s")' %self._name - def dumpSequencePython(self, options=PrintOptions()): + def dumpSequencePython(self, options=PrintOptions()) -> str: return 'cms.SequencePlaceholder("%s")'%self._name - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: result = 'cms.SequencePlaceholder(\"' if options.isCfg: result += 'process.' @@ -763,16 +763,16 @@ def associate(self,*tasks): raise TypeError("The associate function in the class Schedule only works with arguments of type Task") self._tasks.add(task) @staticmethod - def _itemIsValid(item): + def _itemIsValid(item) -> bool: return isinstance(item,Path) or isinstance(item,EndPath) or isinstance(item,FinalPath) def copy(self): import copy aCopy = copy.copy(self) aCopy._tasks = OrderedSet(self._tasks) return aCopy - def _place(self,label,process): + def _place(self,label:str,process): process.setPartialSchedule_(self,label) - def _replaceIfHeldDirectly(self,original,replacement): + def _replaceIfHeldDirectly(self,original,replacement) -> bool: """Only replaces an 'original' with 'replacement' if 'original' is directly held. If a contained Path or Task holds 'original' it will not be replaced.""" didReplace = False @@ -800,7 +800,7 @@ def moduleNames(self): for t in self._tasks: t.visit(visitor) return result - def contains(self, mod): + def contains(self, mod) -> bool: visitor = ContainsModuleVisitor(mod) for seq in self: seq.visit(visitor) @@ -814,7 +814,7 @@ def contains(self, mod): def tasks(self): """Returns the list of Tasks (that may contain other Tasks) that are associated directly to the Schedule.""" return self._tasks - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: pathNames = ['process.'+p.label_() for p in self] if pathNames: s=', '.join(pathNames) @@ -838,7 +838,7 @@ def dumpPython(self, options=PrintOptions()): else: return 'cms.Schedule()\n' - def __str__(self): + def __str__(self) -> str: return self.dumpPython() # Fills a list of all Sequences visited @@ -1451,7 +1451,7 @@ def result(self, visitedContainer): tasks.extend(c[2]) return [seq, tasks] - def _didApply(self): + def _didApply(self) -> bool: return self.__didApply # This visitor can also be used on Tasks. @@ -1468,7 +1468,7 @@ def __call__(self,test): return None return test super(type(self),self).__init__(_RemoveFirstOperator(moduleToRemove)) - def didRemove(self): + def didRemove(self) -> bool: return self._didApply() # This visitor can also be used on Tasks. @@ -1483,7 +1483,7 @@ def __call__(self,test): return None return test super(type(self),self).__init__(_ExcludeOperator(modulesToRemove)) - def didExclude(self): + def didExclude(self) -> bool: return self._didApply() # This visitor can also be used on Tasks. @@ -1499,7 +1499,7 @@ def __call__(self,test): return self.__replace return test super(type(self),self).__init__(_ReplaceOperator(target,replace)) - def didReplace(self): + def didReplace(self) -> bool: return self._didApply() class _TaskBase(_ConfigureComponent, _Labelable) : @@ -1507,7 +1507,7 @@ def __init__(self, *items): self._collection = OrderedSet() self.add(*items) - def __setattr__(self,name,value): + def __setattr__(self,name:str,value): if not name.startswith("_"): raise AttributeError("You cannot set parameters for {} objects.".format(self._taskType())) else: @@ -1520,7 +1520,7 @@ def add(self, *items): "It is illegal to add this type to a {1}.".format(type(item).__name__, self._taskType())) self._collection.add(item) - def fillContents(self, taskContents, options=PrintOptions()): + def fillContents(self, taskContents, options:PrintOptions=PrintOptions()): # only dump the label, if possible if self.hasLabel_(): taskContents.add(_Labelable.dumpSequencePython(self, options)) @@ -1531,11 +1531,11 @@ def fillContents(self, taskContents, options=PrintOptions()): else: taskContents.add(i.dumpSequencePython(options)) - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: s = self.dumpPythonNoNewline(options) return s + "\n" - def dumpPythonNoNewline(self, options=PrintOptions()): + def dumpPythonNoNewline(self, options:PrintOptions=PrintOptions()) -> str: """Returns a string which is the python representation of the object""" taskContents = set() for i in self._collection: @@ -1554,13 +1554,13 @@ def dumpPythonNoNewline(self, options=PrintOptions()): s = "*[" + s + "]" return "cms.{}({})".format(self._taskType(),s) - def directDependencies(self,sortByType=True): + def directDependencies(self,sortByType:bool=True): return findDirectDependencies(self, self._collection,sortByType=sortByType) - def _isTaskComponent(self): + def _isTaskComponent(self) -> bool: return False - def isLeaf(self): + def isLeaf(self) -> bool: return False def visit(self,visitor): @@ -1570,14 +1570,14 @@ def visit(self,visitor): i.visit(visitor) visitor.leave(i) - def _errorstr(self): + def _errorstr(self) -> str: return "{}(...)".format(self.taskType_()) def __iter__(self): for key in self._collection: yield key - def __str__(self): + def __str__(self) -> str: l = [] v = ModuleNodeVisitor(l) self.visit(v) @@ -1588,7 +1588,7 @@ def __str__(self): s += str (i) return s - def __repr__(self): + def __repr__(self) -> str: s = str(self) return "cms."+type(self).__name__+'('+s+')\n' @@ -1675,7 +1675,7 @@ def remove(self, something): self.add(*v.result(self)) return v.didRemove() - def resolve(self, processDict,keepIfCannotResolve=False): + def resolve(self, processDict,keepIfCannotResolve:bool=False): temp = OrderedSet() for i in self._collection: if self._mustResolve(i): @@ -1686,19 +1686,19 @@ def resolve(self, processDict,keepIfCannotResolve=False): return self class _TaskBasePlaceholder(object): - def __init__(self, name): + def __init__(self, name:str): self._name = name - def _isTaskComponent(self): + def _isTaskComponent(self) -> bool: return False - def isLeaf(self): + def isLeaf(self) -> bool: return False def visit(self,visitor): pass - def __str__(self): + def __str__(self) -> str: return self._name def insertInto(self, parameterSet, myname): raise RuntimeError("The {} {} was never overridden".format(self._typeName(), self._name)) - def resolve(self, processDict,keepIfCannotResolve=False): + def resolve(self, processDict,keepIfCannotResolve:bool=False): if not self._name in processDict: if keepIfCannotResolve: return self @@ -1711,9 +1711,9 @@ def resolve(self, processDict,keepIfCannotResolve=False): return o def copy(self): return self._makeInstance(self._name) - def dumpSequencePython(self, options=PrintOptions()): + def dumpSequencePython(self, options:PrintOptions=PrintOptions()) -> str: return 'cms.{}("{}")'.format(self._typeName(), self._name) - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: result = 'cms.{}(\"'.format(self._typeName()) if options.isCfg: result += 'process.' @@ -1732,33 +1732,33 @@ class Task(_TaskBase) : to the process. """ @staticmethod - def _taskType(): + def _taskType() -> str: return "Task" - def _place(self, name, proc): + def _place(self, name:str, proc): proc._placeTask(name,self) - def _isTaskComponent(self): + def _isTaskComponent(self) -> bool: return True @staticmethod def _makeInstance(*items): return Task(*items) @staticmethod - def _allowedInTask(item ): + def _allowedInTask(item ) -> bool: return (isinstance(item, _ConfigureComponent) and item._isTaskComponent()) or isinstance(item, TaskPlaceholder) @staticmethod - def _mustResolve(item): + def _mustResolve(item) -> bool: return isinstance(item, Task) or isinstance(item, TaskPlaceholder) class TaskPlaceholder(_TaskBasePlaceholder): - def _isTaskComponent(self): + def _isTaskComponent(self) -> bool: return True @staticmethod - def _typeName(): + def _typeName() -> str: return "TaskPlaceholder" @staticmethod def _makeInstance(name): return TaskPlaceholder(name) @staticmethod - def _allowedInTask(obj): + def _allowedInTask(obj) -> bool: return Task._allowedInTask(obj) @staticmethod def _taskClass(): @@ -1776,32 +1776,32 @@ class ConditionalTask(_TaskBase) : @staticmethod def _taskType(): return "ConditionalTask" - def _place(self, name, proc): + def _place(self, name:str, proc): proc._placeConditionalTask(name,self) - def _isTaskComponent(self): + def _isTaskComponent(self) -> bool: return False @staticmethod def _makeInstance(*items): return ConditionalTask(*items) @staticmethod - def _allowedInTask(item): + def _allowedInTask(item) -> bool: return isinstance(item, ConditionalTask) or isinstance(item, ConditionalTaskPlaceholder) or Task._allowedInTask(item) @staticmethod - def _mustResolve(item): + def _mustResolve(item) -> bool: return Task._mustResolve(item) or isinstance(item, ConditionalTask) or isinstance(item, ConditionalTaskPlaceholder) class ConditionalTaskPlaceholder(_TaskBasePlaceholder): - def _isTaskComponent(self): + def _isTaskComponent(self) -> bool: return False @staticmethod - def _typeName(): + def _typeName() -> str: return "ConditionalTaskPlaceholder" @staticmethod def _makeInstance(name): return ConditionalTaskPlaceholder(name) @staticmethod - def _allowedInTask(obj): + def _allowedInTask(obj) -> bool: return Task._allowedInTask(obj) or ConditionalTask._allowedInTask(obj) @staticmethod def _taskClass(): diff --git a/FWCore/ParameterSet/python/SequenceVisitors.py b/FWCore/ParameterSet/python/SequenceVisitors.py index 3717f193cc16d..85eed841bb6c9 100644 --- a/FWCore/ParameterSet/python/SequenceVisitors.py +++ b/FWCore/ParameterSet/python/SequenceVisitors.py @@ -22,7 +22,7 @@ def leave(self,visitee): class PathValidator(object): def __init__(self): self.__label = '' - def setLabel(self,label): + def setLabel(self,label:str): self.__label = "'"+label+"' " def enter(self,visitee): if isinstance(visitee,OutputModule): @@ -44,7 +44,7 @@ def __init__(self): self.filtersOnEndpaths = [] self.__label = '' self._levelInTasks = 0 - def setLabel(self,label): + def setLabel(self,label:str): self.__label = "'"+label+"' " def enter(self,visitee): if visitee.isLeaf(): @@ -73,7 +73,7 @@ def __init__(self): self.__label = '' self._levelInTasks = 0 self.invalidModulesOnFinalpaths = [] - def setLabel(self,label): + def setLabel(self,label:str): self.__label = "'"+label+"' " def enter(self,visitee): if visitee.isLeaf(): diff --git a/FWCore/ParameterSet/python/TreeCrawler.py b/FWCore/ParameterSet/python/TreeCrawler.py index 9d7bacc65047b..d91b1c7487264 100755 --- a/FWCore/ParameterSet/python/TreeCrawler.py +++ b/FWCore/ParameterSet/python/TreeCrawler.py @@ -28,7 +28,7 @@ import sys, os, inspect, copy, struct, dis, imp import modulefinder -def packageNameFromFilename(name): +def packageNameFromFilename(name:str) -> str: return ".".join(name.replace("python/","").replace(".py","").split("/")[-3:]) @@ -59,7 +59,7 @@ def __init__(self,name,top=False): self.module = None else: self.module = __import__(name,[],[],"*") - def dump(self,level): + def dump(self,level:int): indent = " " * level print(indent, "+", Color.info, self.name, Color.none) # sort dependencies alphabetically @@ -284,7 +284,7 @@ def transformIntoGraph(depgraph,toplevel): return packageDict[toplevel] -def getDependenciesFromPythonFile(filename,toplevelname,path): +def getDependenciesFromPythonFile(filename:str,toplevelname,path): modulefinder = mymf(path) modulefinder.run_script(filename) globalDependencyDict = modulefinder._depgraph @@ -292,7 +292,7 @@ def getDependenciesFromPythonFile(filename,toplevelname,path): return globalDependencyDict -def getImportTree(filename,path): +def getImportTree(filename:str,path): toplevelname = packageNameFromFilename(filename) # get dependencies from given file globalDependencyDict = getDependenciesFromPythonFile(filename,toplevelname,path) diff --git a/FWCore/ParameterSet/python/Types.py b/FWCore/ParameterSet/python/Types.py index 07dce4c862a7b..a10c784b55ed9 100644 --- a/FWCore/ParameterSet/python/Types.py +++ b/FWCore/ParameterSet/python/Types.py @@ -19,7 +19,7 @@ def __call__(param): """used to set a 'param' parameter to be 'untracked'""" param.setIsTracked(False) return param - def __getattr__(self,name): + def __getattr__(self,name:str): """A factory which allows syntax untracked.name(value) to construct an instance of 'name' class which is set to be untracked""" if name == "__bases__": raise AttributeError # isclass uses __bases__ to recognize class objects @@ -61,7 +61,7 @@ def _checkAndReturnValueWithType(self, valueWithType): v = untracked(v) self.__dict__["_ProxyParameter__value"] = v return self - def __getattr__(self, name): + def __getattr__(self, name:str): v =self.__dict__.get('_ProxyParameter__value', None) if name == '_ProxyParameter__value': return v @@ -69,7 +69,7 @@ def __getattr__(self, name): return getattr(v, name) else: return object.__getattribute__ (self, name) - def __setattr__(self,name, value): + def __setattr__(self,name:str, value): v = self.__dict__.get('_ProxyParameter__value',None) if v is not None: return setattr(v,name,value) @@ -99,7 +99,7 @@ def __getitem__(self, key): def __bool__(self): v = self.__dict__.get('_ProxyParameter__value',None) return _builtin_bool(v) - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: v =self.__dict__.get('_ProxyParameter__value',None) if v is not None: return v.dumpPython(options) @@ -110,14 +110,14 @@ def dumpPython(self, options=PrintOptions()): if hasattr(self.__type, "__name__"): return v+'.'+self.__type.__name__ return v+'.'+self.__type.dumpPython(options) - def validate_(self,value): + def validate_(self,value) -> bool: return isinstance(value,self.__type) def convert_(self,value): v = self.__type(value) if not _ParameterTypeBase.isTracked(self): v = untracked(v) return v - def isCompatibleCMSType(self,aType): + def isCompatibleCMSType(self,aType) -> bool: v = self.__dict__.get('_ProxyParameter__value',None) if v is not None: return v.isCompatibleCMSType(aType) @@ -125,9 +125,9 @@ def isCompatibleCMSType(self,aType): class _RequiredParameter(_ProxyParameter): @staticmethod - def _dumpPythonName(): + def _dumpPythonName() -> str: return 'required' - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): v = self.__dict__.get('_ProxyParameter__value', None) if v is None: raise RuntimeError("Required parameter "+myname+" was not set") @@ -135,9 +135,9 @@ def insertInto(self, parameterSet, myname): class _OptionalParameter(_ProxyParameter): @staticmethod - def _dumpPythonName(): + def _dumpPythonName() -> str: return 'optional' - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): v = self.__dict__.get('_ProxyParameter__value', None) if v is not None: v.insertInto(parameterSet,myname) @@ -149,7 +149,7 @@ def value(self): class _ObsoleteParameter(_OptionalParameter): @staticmethod - def _dumpPythonName(): + def _dumpPythonName() -> str: return 'obsolete' class _AllowedParameterTypes(object): @@ -159,7 +159,7 @@ def __init__(self, *args, default=None): self.__dict__['__name__'] = self.dumpPython() if default is not None: self.__dict__['_default'] = self._setValueWithType(default) - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: specialImportRegistry.registerUse(self) return "allowed("+','.join( ("cms."+t.__name__ if not isinstance(t, _PSetTemplate) else "cms."+t.dumpPython() for t in self.__types))+')' def __call__(self,value): @@ -191,9 +191,9 @@ def __init__(self, *args, **kargs): def __call__(self, value): self.__dict__ return self._pset.clone(**value) - def _isValid(self, value): + def _isValid(self, value) -> bool: return isinstance(value,dict) or isinstance(value, PSet) - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: return "PSetTemplate(\n"+_Parameterizable.dumpPython(self._pset, options)+options.indentation()+")" def _setValueWithType(self, valueWithType): if not isinstance(valueWithType, PSet): @@ -204,10 +204,10 @@ def _setValueWithType(self, valueWithType): class _ProxyParameterFactory(object): """Class type for ProxyParameter types to allow nice syntax""" - def __init__(self, type, isUntracked = False): + def __init__(self, type, isUntracked:bool = False): self.__isUntracked = isUntracked self.__type = type - def __getattr__(self,name): + def __getattr__(self,name:str): if name[0] == '_': return object.__getattribute__(self,name) if name == 'untracked': @@ -247,95 +247,95 @@ def __call__(self,*args,**kargs): class int32(_SimpleParameterTypeBase): @staticmethod - def _isValid(value): + def _isValid(value) -> bool: return isinstance(value,int) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): """only used for cfg-parsing""" if len(value) >1 and '0x' == value[:2]: return int32(int(value,16)) return int32(int(value)) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addInt32(self.isTracked(), myname, self.value()) - def __nonzero__(self): + def __nonzero__(self) -> bool: return self.value()!=0 - def __bool__(self): + def __bool__(self) -> bool: return self.__nonzero__() class uint32(_SimpleParameterTypeBase): @staticmethod - def _isValid(value): + def _isValid(value) -> bool: return ((isinstance(value,int) and value >= 0) or (isinstance(value,long) and value >= 0) and value <= 0xFFFFFFFF) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): """only used for cfg-parsing""" if len(value) >1 and '0x' == value[:2]: return uint32(long(value,16)) return uint32(long(value)) def insertInto(self, parameterSet, myname): parameterSet.addUInt32(self.isTracked(), myname, self.value()) - def __nonzero__(self): + def __nonzero__(self) -> bool: return self.value()!=0 - def __bool__(self): + def __bool__(self) -> bool: return self.__nonzero__() class int64(_SimpleParameterTypeBase): @staticmethod - def _isValid(value): + def _isValid(value) -> bool: return isinstance(value,int) or ( isinstance(value,long) and (-0x7FFFFFFFFFFFFFFF < value <= 0x7FFFFFFFFFFFFFFF) ) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): """only used for cfg-parsing""" if len(value) >1 and '0x' == value[:2]: return uint32(long(value,16)) return int64(long(value)) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addInt64(self.isTracked(), myname, self.value()) - def __nonzero__(self): + def __nonzero__(self) -> bool: return self.value()!=0 class uint64(_SimpleParameterTypeBase): @staticmethod - def _isValid(value): + def _isValid(value) -> bool: return ((isinstance(value,int) and value >= 0) or (isinstance(value,long) and value >= 0) and value <= 0xFFFFFFFFFFFFFFFF) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): """only used for cfg-parsing""" if len(value) >1 and '0x' == value[:2]: return uint32(long(value,16)) return uint64(long(value)) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addUInt64(self.isTracked(), myname, self.value()) - def __nonzero__(self): + def __nonzero__(self) -> bool: return self.value()!=0 class double(_SimpleParameterTypeBase): @staticmethod - def _isValid(value): + def _isValid(value) -> bool: return isinstance(value, (int, long, float)) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): """only used for cfg-parsing""" return double(float(value)) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addDouble(self.isTracked(), myname, float(self.value())) - def __nonzero__(self): + def __nonzero__(self) -> bool: return self.value()!=0. - def configValue(self, options=PrintOptions()): + def configValue(self, options:PrintOptions=PrintOptions()) -> str: return double._pythonValue(self._value) @staticmethod - def _pythonValue(value): + def _pythonValue(value) -> str: if math.isinf(value): if value > 0: return "float('inf')" @@ -349,10 +349,10 @@ def _pythonValue(value): class bool(_SimpleParameterTypeBase): @staticmethod - def _isValid(value): + def _isValid(value) -> builtins.bool: return (isinstance(value,type(False)) or isinstance(value,type(True))) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): """only used for cfg-parsing""" if value.lower() in ('true', 't', 'on', 'yes', '1'): return bool(True) @@ -363,11 +363,11 @@ def _valueFromString(value): except: pass raise RuntimeError('can not make bool from string '+value) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addBool(self.isTracked(), myname, self.value()) - def __nonzero__(self): + def __nonzero__(self) -> builtins.bool: return self.value() - def __bool__(self): + def __bool__(self) -> builtins.bool: return self.__nonzero__() @@ -375,14 +375,14 @@ class string(_SimpleParameterTypeBase): def __init__(self,value): super(string,self).__init__(value) @staticmethod - def _isValid(value): + def _isValid(value) -> builtins.bool: return isinstance(value,type('')) - def configValue(self, options=PrintOptions()): + def configValue(self, options:PrintOptions=PrintOptions()) -> str: return self.formatValueForConfig(self.value()) - def pythonValue(self, options=PrintOptions()): + def pythonValue(self, options:PrintOptions=PrintOptions()) -> str: return self.configValue(options) @staticmethod - def formatValueForConfig(value): + def formatValueForConfig(value) -> str: l = len(value) import sys if sys.version_info >= (3, 0): #python2 and python3 are different due to byptes vs strings @@ -399,18 +399,18 @@ def formatValueForConfig(value): return '"'+value+'"' return "'"+value+"'" @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): """only used for cfg-parsing""" return string(value) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): value = self.value() # doesn't seem to handle \0 correctly #if value == '\0': # value = '' parameterSet.addString(self.isTracked(), myname, value) - def __nonzero__(self): + def __nonzero__(self) -> builtins.bool: return len(self.value()) !=0 - def __bool__(self): + def __bool__(self) -> builtins.bool: return self.__nonzero__() @@ -431,17 +431,17 @@ def __init__(self, run, *args): self.__event = args[1] else: raise RuntimeError('EventID ctor must have 2 or 3 arguments') - def run(self): + def run(self) -> int: return self.__run - def luminosityBlock(self): + def luminosityBlock(self) -> int: return self.__luminosityBlock - def event(self): + def event(self) -> int: return self.__event @staticmethod - def _isValid(value): + def _isValid(value) -> builtins.bool: return True @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): parts = value.split(":") run = parts[0] try: @@ -451,11 +451,11 @@ def _valueFromString(value): lumi = 0 event = parts[1] return EventID(int(run), int(lumi), int(event)) - def pythonValue(self, options=PrintOptions()): + def pythonValue(self, options:PrintOptions=PrintOptions()) -> str: return str(self.__run)+ ', '+str(self.__luminosityBlock)+ ', '+str(self.__event) def cppID(self, parameterSet): return parameterSet.newEventID(self.run(), self.luminosityBlock(), self.event()) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addEventID(self.isTracked(), myname, self.cppID(parameterSet)) @@ -468,23 +468,23 @@ def __init__(self, run, block=None): else: self.__run = run self.__block = block - def run(self): + def run(self) -> int: return self.__run - def luminosityBlock(self): + def luminosityBlock(self) -> int: return self.__block @staticmethod - def _isValid(value): + def _isValid(value) -> builtins.bool: return True @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): """only used for cfg-parsing""" parts = value.split(":") return LuminosityBlockID(int(parts[0]), int(parts[1])) - def pythonValue(self, options=PrintOptions()): + def pythonValue(self, options:PrintOptions=PrintOptions()) -> str: return str(self.__run)+ ', '+str(self.__block) def cppID(self, parameterSet): return parameterSet.newLuminosityBlockID(self.run(), self.luminosityBlock()) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addLuminosityBlockID(self.isTracked(), myname, self.cppID(parameterSet)) @@ -507,19 +507,19 @@ def __init__(self, start, startSub=None, end=None, endSub=None): # 0 luminosity block number is a special case that means no limit if self.__end == self.__start and (self.__endSub != 0 and self.__endSub < self.__startSub): raise RuntimeError('LuminosityBlockRange '+str(self.__start)+':'+str(self.__startSub)+'-'+str(self.__end)+':'+str(self.__endSub)+' out of order') - def start(self): + def start(self) -> int: return self.__start - def startSub(self): + def startSub(self) -> int: return self.__startSub - def end(self): + def end(self) -> int: return self.__end - def endSub(self): + def endSub(self) -> int: return self.__endSub @staticmethod - def _isValid(value): + def _isValid(value) -> builtins.bool: return True @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): """only used for cfg-parsing""" value = value.replace(' ','') parts = value.split("-") @@ -541,12 +541,12 @@ def _valueFromString(value): endParts[1] = "1" return LuminosityBlockRange(int(startParts[0]), int(startParts[1]), int(endParts[0]), int(endParts[1])) - def pythonValue(self, options=PrintOptions()): + def pythonValue(self, options:PrintOptions=PrintOptions()) -> str: return str(self.__start) + ', ' + str(self.__startSub) + ', ' \ + str(self.__end) + ', ' + str(self.__endSub) def cppID(self, parameterSet): return parameterSet.newLuminosityBlockRange(self.start(), self.startSub(),self.end(), self.endSub()) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addLuminosityBlockRange(self.isTracked(), myname, self.cppID(parameterSet)) class EventRange(_ParameterTypeBase): @@ -581,23 +581,23 @@ def __init__(self, start, *args): # 0 event number is a special case that means no limit if self.__end == self.__start and self.__endLumi == self.__startLumi and (self.__endSub != 0 and self.__endSub < self.__startSub): raise RuntimeError('EventRange '+str(self.__start)+':'+str(self.__startLumi)+':'+str(self.__startSub)+'-'+str(self.__end)+':'+str(self.__endLumi)+':'+str(self.__endSub)+' out of order') - def start(self): + def start(self) -> int: return self.__start - def startLumi(self): + def startLumi(self) -> int: return self.__startLumi - def startSub(self): + def startSub(self) -> int: return self.__startSub - def end(self): + def end(self) -> int: return self.__end - def endLumi(self): + def endLumi(self) -> int: return self.__endLumi - def endSub(self): + def endSub(self) -> int: return self.__endSub @staticmethod - def _isValid(value): + def _isValid(value) -> builtins.bool: return True @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): """only used for cfg-parsing""" value = value.replace(' ','') parts = value.split("-") @@ -641,52 +641,52 @@ def _valueFromString(value): return EventRange(int(brun), int(blumi), int(bevent), int(erun), int(elumi), int(eevent)) - def pythonValue(self, options=PrintOptions()): + def pythonValue(self, options:PrintOptions=PrintOptions()) -> str: return str(self.__start) + ', ' + str(self.__startLumi) + ', ' + str(self.__startSub) + ', ' \ + str(self.__end) + ', ' + str(self.__endLumi) + ', ' + str(self.__endSub) def cppID(self, parameterSet): return parameterSet.newEventRange(self.start(), self.startLumi(), self.startSub(), self.end(), self.endLumi(), self.endSub()) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addEventRange(self.isTracked(), myname, self.cppID(parameterSet)) class InputTag(_ParameterTypeBase): - def __init__(self,moduleLabel,productInstanceLabel='',processName=''): + def __init__(self,moduleLabel:str,productInstanceLabel:str='',processName:str=''): super(InputTag,self).__init__() self._setValues(moduleLabel, productInstanceLabel, processName) - def getModuleLabel(self): + def getModuleLabel(self) -> str: return self.__moduleLabel - def setModuleLabel(self,label): + def setModuleLabel(self,label:str): if self.__moduleLabel != label: self.__moduleLabel = label self._isModified=True moduleLabel = property(getModuleLabel,setModuleLabel,"module label for the product") - def getProductInstanceLabel(self): + def getProductInstanceLabel(self) -> str: return self.__productInstance - def setProductInstanceLabel(self,label): + def setProductInstanceLabel(self,label:str): if self.__productInstance != label: self.__productInstance = label self._isModified=True productInstanceLabel = property(getProductInstanceLabel,setProductInstanceLabel,"product instance label for the product") - def getProcessName(self): + def getProcessName(self) -> str: return self.__processName - def setProcessName(self,label): + def setProcessName(self,label:str): if self.__processName != label: self.__processName = label self._isModified=True processName = property(getProcessName,setProcessName,"process name for the product") @staticmethod - def skipCurrentProcess(): + def skipCurrentProcess() -> str: """When used as the process name this value will make the framework skip the current process when looking backwards in time for the data product. """ return "@skipCurrentProcess" @staticmethod - def currentProcess(): + def currentProcess() -> str: """When used as the process name this value will make the framework use the current process as the process when looking for the data product. """ return "@currentProcess" - def configValue(self, options=PrintOptions()): + def configValue(self, options:PrintOptions=PrintOptions()) -> str: result = self.__moduleLabel if self.__productInstance != "" or self.__processName != "": result += ':' + self.__productInstance @@ -695,7 +695,7 @@ def configValue(self, options=PrintOptions()): if result == "": result = '\"\"' return result; - def pythonValue(self, options=PrintOptions()): + def pythonValue(self, options:PrintOptions=PrintOptions()) -> str: cfgValue = self.configValue(options) # empty strings already have quotes if cfgValue == '\"\"': @@ -704,36 +704,36 @@ def pythonValue(self, options=PrintOptions()): # change label:instance:process to "label","instance","process" return colonedValue.replace(":","\",\"") @staticmethod - def _isValid(value): + def _isValid(value) -> builtins.bool: return True - def __eq__(self,other): + def __eq__(self,other) -> builtins.bool: return ((self.__moduleLabel,self.__productInstance,self.__processName) == (other.moduleLabel,other.productInstanceLabel,other.processName)) - def __ne__(self,other): + def __ne__(self,other) -> builtins.bool: return ((self.__moduleLabel,self.__productInstance,self.__processName) != (other.moduleLabel,other.productInstanceLabel,other.processName)) - def __lt__(self,other): + def __lt__(self,other) -> builtins.bool: return ((self.__moduleLabel,self.__productInstance,self.__processName) < (other.moduleLabel,other.productInstanceLabel,other.processName)) - def __gt__(self,other): + def __gt__(self,other) -> builtins.bool: return ((self.__moduleLabel,self.__productInstance,self.__processName) > (other.moduleLabel,other.productInstanceLabel,other.processName)) - def __le__(self,other): + def __le__(self,other) -> builtins.bool: return ((self.__moduleLabel,self.__productInstance,self.__processName) <= (other.moduleLabel,other.productInstanceLabel,other.processName)) - def __ge__(self,other): + def __ge__(self,other) -> builtins.bool: return ((self.__moduleLabel,self.__productInstance,self.__processName) >= (other.moduleLabel,other.productInstanceLabel,other.processName)) - def value(self): + def value(self) -> str: "Return the string rep" return self.configValue() @staticmethod def formatValueForConfig(value): return value.configValue() @staticmethod - def _valueFromString(string): + def _valueFromString(string:str): parts = string.split(":") return InputTag(*parts) @staticmethod @@ -751,7 +751,7 @@ def _stringFromArgument(arg): def setValue(self,v): self._setValues(v) self._isModified=True - def _setValues(self,moduleLabel,productInstanceLabel='',processName=''): + def _setValues(self,moduleLabel,productInstanceLabel:str='',processName:str=''): self.__moduleLabel = InputTag._stringFromArgument(moduleLabel) self.__productInstance = productInstanceLabel self.__processName=processName @@ -769,33 +769,33 @@ def cppTag(self, parameterSet): return parameterSet.newInputTag(self.getModuleLabel(), self.getProductInstanceLabel(), self.getProcessName()) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addInputTag(self.isTracked(), myname, self.cppTag(parameterSet)) class ESInputTag(_ParameterTypeBase): def __init__(self,module='',data= None): super(ESInputTag,self).__init__() self._setValues(module, data) - def getModuleLabel(self): + def getModuleLabel(self) -> str: return self.__moduleLabel - def setModuleLabel(self,label): + def setModuleLabel(self,label:str): if self.__moduleLabel != label: self.__moduleLabel = label self._isModified=True moduleLabel = property(getModuleLabel,setModuleLabel,"module label for the product") def getDataLabel(self): return self.__data - def setDataLabel(self,label): + def setDataLabel(self,label:str): if self.__data != label: self.__data = label self._isModified=True dataLabel = property(getDataLabel,setDataLabel,"data label for the product") - def configValue(self, options=PrintOptions()): + def configValue(self, options:PrintOptions=PrintOptions()) -> str: result = self.__moduleLabel + ':' + self.__data if result == "": result = '\"\"' return result; - def pythonValue(self, options=PrintOptions()): + def pythonValue(self, options:PrintOptions=PrintOptions()) -> str: cfgValue = self.configValue(options) # empty strings already have quotes if cfgValue == '\"\"': @@ -804,19 +804,19 @@ def pythonValue(self, options=PrintOptions()): # change label:instance:process to "label","instance","process" return colonedValue.replace(":","\",\"") @staticmethod - def _isValid(value): + def _isValid(value) -> builtins.bool: return True - def __eq__(self,other): + def __eq__(self,other) -> builtins.bool: return ((self.__moduleLabel,self.__data) == (other.__moduleLabel,other.__data)) - def __ne__(self,other): + def __ne__(self,other) -> builtins.bool: return ((self.__moduleLabel,self.__data) != (other.__moduleLabel,other.__data)) - def __lt__(self,other): + def __lt__(self,other) -> builtins.bool: return ((self.__moduleLabel,self.__data) < (other.__moduleLabel,other.__data)) - def __gt__(self,other): + def __gt__(self,other) -> builtins.bool: return ((self.__moduleLabel,self.__data) > (other.__moduleLabel,other.__data)) - def __le__(self,other): + def __le__(self,other) -> builtins.bool: return ((self.__moduleLabel,self.__data) <= (other.__moduleLabel,other.__data)) - def __ge__(self,other): + def __ge__(self,other) -> builtins.bool: return ((self.__moduleLabel,self.__data) >= (other.__moduleLabel,other.__data)) def value(self): "Return the string rep" @@ -825,7 +825,7 @@ def value(self): def formatValueForConfig(value): return value.configValue() @staticmethod - def _valueFromString(string): + def _valueFromString(string:str): parts = string.split(":") return ESInputTag(*parts) @staticmethod @@ -866,24 +866,24 @@ def _setValues(self,moduleLabel='',dataLabel=None): def cppTag(self, parameterSet): return parameterSet.newESInputTag(self.getModuleLabel(), self.getDataLabel()) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addESInputTag(self.isTracked(), myname, self.cppTag(parameterSet)) class FileInPath(_SimpleParameterTypeBase): - def __init__(self,value=""): + def __init__(self,value:str=""): super(FileInPath,self).__init__(value) @staticmethod - def _isValid(value): + def _isValid(value) -> builtins.bool: return True - def configValue(self, options=PrintOptions()): + def configValue(self, options:PrintOptions=PrintOptions()) -> str: return string.formatValueForConfig(self.value()) @staticmethod def formatValueForConfig(value): return string.formatValueForConfig(value) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): return FileInPath(value) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addNewFileInPath( self.isTracked(), myname, self.value() ) class SecSource(_ParameterTypeBase,_TypedParameterizable,_ConfigureComponent,_Labelable): @@ -893,20 +893,20 @@ def __init__(self,type_,*arg,**args): def value(self): return self @staticmethod - def _isValid(value): + def _isValid(value) -> builtins.bool: return True - def configTypeName(self): + def configTypeName(self)-> str: return "secsource" - def configValue(self, options=PrintOptions()): + def configValue(self, options:PrintOptions=PrintOptions()) -> str: return self.dumpConfig(options) - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: return _TypedParameterizable.dumpPython(self, options) def copy(self): # TODO is the one in TypedParameterizable better? return copy.copy(self) - def _place(self,name,proc): + def _place(self,name:str,proc): proc._placePSet(name,self) - def __str__(self): + def __str__(self) -> str: return object.__str__(self) class PSet(_ParameterTypeBase,_Parameterizable,_ConfigureComponent,_Labelable): @@ -916,19 +916,19 @@ def __init__(self,*arg,**args): _Parameterizable.__init__(self,*arg,**args) def value(self): return self - def isRef_(self): + def isRef_(self) -> builtins.bool: """Returns true if this PSet is actually a reference to a different PSet """ return hasattr(self,"refToPSet_") @staticmethod - def _isValid(value): + def _isValid(value) -> builtins.bool: return isinstance(value,PSet) or isinstance(value,dict) def setValue(self,value): if isinstance(value,dict): for k,v in value.items(): setattr(self,k,v) - def configValue(self, options=PrintOptions()): + def configValue(self, options:PrintOptions=PrintOptions()) -> str: config = '{ \n' for name in self.parameterNames_(): param = getattr(self,name) @@ -937,7 +937,7 @@ def configValue(self, options=PrintOptions()): options.unindent() config += options.indentation()+'}\n' return config - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: return self.pythonTypeName()+"(\n"+_Parameterizable.dumpPython(self, options)+options.indentation()+")" # XXX FIXME handle refToPSet def directDependencies(self): @@ -956,11 +956,11 @@ def clone(self, **params): return returnValue def copy(self): return copy.copy(self) - def _place(self,name,proc): + def _place(self,name:str,proc): proc._placePSet(name,self) - def __str__(self): + def __str__(self) -> str: return object.__str__(self) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): newpset = parameterSet.newPSet() self.insertContentsInto(newpset) parameterSet.addPSet(self.isTracked(), myname, newpset) @@ -977,12 +977,12 @@ def __init__(self,*arg,**args): super(vint32,self).__init__(*arg,**args) @classmethod - def _itemIsValid(cls,item): + def _itemIsValid(cls,item) -> builtins.bool: return int32._isValid(item) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): return vint32(*_ValidatingParameterListBase._itemsFromStrings(value,int32._valueFromString)) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addVInt32(self.isTracked(), myname, self.value()) @@ -991,12 +991,12 @@ class vuint32(_ValidatingParameterListBase): def __init__(self,*arg,**args): super(vuint32,self).__init__(*arg,**args) @classmethod - def _itemIsValid(cls,item): + def _itemIsValid(cls,item) -> builtins.bool: return uint32._isValid(item) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): return vuint32(*_ValidatingParameterListBase._itemsFromStrings(value,uint32._valueFromString)) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addVUInt32(self.isTracked(), myname, self.value()) @@ -1005,10 +1005,10 @@ class vint64(_ValidatingParameterListBase): def __init__(self,*arg,**args): super(vint64,self).__init__(*arg,**args) @classmethod - def _itemIsValid(cls,item): + def _itemIsValid(cls,item) -> builtins.bool: return int64._isValid(item) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): return vint64(*_ValidatingParameterListBase._itemsFromStrings(value,int64._valueFromString)) def insertInto(self, parameterSet, myname): parameterSet.addVInt64(self.isTracked(), myname, self.value()) @@ -1019,12 +1019,12 @@ class vuint64(_ValidatingParameterListBase): def __init__(self,*arg,**args): super(vuint64,self).__init__(*arg,**args) @classmethod - def _itemIsValid(cls,item): + def _itemIsValid(cls,item) -> builtins.bool: return uint64._isValid(item) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): return vuint64(*_ValidatingParameterListBase._itemsFromStrings(value,vuint64._valueFromString)) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addVUInt64(self.isTracked(), myname, self.value()) @@ -1033,14 +1033,14 @@ class vdouble(_ValidatingParameterListBase): def __init__(self,*arg,**args): super(vdouble,self).__init__(*arg,**args) @classmethod - def _itemIsValid(cls,item): + def _itemIsValid(cls,item) -> builtins.bool: return double._isValid(item) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): return vdouble(*_ValidatingParameterListBase._itemsFromStrings(value,double._valueFromString)) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addVDouble(self.isTracked(), myname, self.value()) - def pythonValueForItem(self,item, options): + def pythonValueForItem(self,item, options) -> str: return double._pythonValue(item) @@ -1050,12 +1050,12 @@ class vbool(_ValidatingParameterListBase): def __init__(self,*arg,**args): super(vbool,self).__init__(*arg,**args) @classmethod - def _itemIsValid(cls,item): + def _itemIsValid(cls,item) -> builtins.bool: return bool._isValid(item) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): return vbool(*_ValidatingParameterListBase._itemsFromStrings(value,bool._valueFromString)) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addVBool(self.isTracked(), myname, self.value()) @@ -1065,14 +1065,14 @@ def __init__(self,*arg,**args): super(vstring,self).__init__(*arg,**args) self._nPerLine = 1 @classmethod - def _itemIsValid(cls,item): + def _itemIsValid(cls,item) -> builtins.bool: return string._isValid(item) - def configValueForItem(self,item,options): + def configValueForItem(self,item,options) -> str: return string.formatValueForConfig(item) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): return vstring(*_ValidatingParameterListBase._itemsFromStrings(value,string._valueFromString)) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): parameterSet.addVString(self.isTracked(), myname, self.value()) class VLuminosityBlockID(_ValidatingParameterListBase): @@ -1081,14 +1081,14 @@ def __init__(self,*arg,**args): @classmethod def _itemIsValid(cls,item): return LuminosityBlockID._isValid(item) - def configValueForItem(self,item,options): + def configValueForItem(self,item,options:PrintOptions) -> str: return LuminosityBlockID.formatValueForConfig(item) - def pythonValueForItem(self,item, options): + def pythonValueForItem(self,item, options:PrintOptions) -> str: return item.dumpPython(options) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): return VLuminosityBlockID(*_ValidatingParameterListBase._itemsFromStrings(value,LuminosityBlockID._valueFromString)) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): cppIDs = list() for i in self: item = i @@ -1107,26 +1107,26 @@ def __init__(self,*arg,**args): pass super(VInputTag,self).__init__((InputTag._stringFromArgument(x) for x in arg),**args) @classmethod - def _itemIsValid(cls,item): + def _itemIsValid(cls,item) -> builtins.bool: return InputTag._isValid(item) - def configValueForItem(self,item,options): + def configValueForItem(self,item,options:PrintOptions) -> str: # we tolerate strings as members if isinstance(item, str): return '"'+item+'"' else: return InputTag.formatValueForConfig(item) - def pythonValueForItem(self,item, options): + def pythonValueForItem(self,item, options:PrintOptions) -> str: # we tolerate strings as members if isinstance(item, str): return '"'+item+'"' else: return item.dumpPython(options) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): return VInputTag(*_ValidatingParameterListBase._itemsFromStrings(value,InputTag._valueFromString)) def _itemFromArgument(self, x): return InputTag._stringFromArgument(x) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): cppTags = list() for i in self: item = i @@ -1144,26 +1144,26 @@ def __init__(self,*arg,**args): pass super(VESInputTag,self).__init__((ESInputTag._stringFromArgument(x) for x in arg),**args) @classmethod - def _itemIsValid(cls,item): + def _itemIsValid(cls,item) -> builtins.bool: return ESInputTag._isValid(item) - def configValueForItem(self,item,options): + def configValueForItem(self,item,options:PrintOptions) -> str: # we tolerate strings as members if isinstance(item, str): return '"'+item+'"' else: return ESInputTag.formatValueForConfig(item) - def pythonValueForItem(self,item, options): + def pythonValueForItem(self,item, options:PrintOptions) -> str: # we tolerate strings as members if isinstance(item, str): return '"'+item+'"' else: return item.dumpPython(options) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): return VESInputTag(*_ValidatingParameterListBase._itemsFromStrings(value,ESInputTag._valueFromString)) - def _itemFromArgument(self, x): + def _itemFromArgument(self, x) -> str: return ESInputTag._stringFromArgument(x) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): cppTags = list() for i in self: item = i @@ -1178,18 +1178,18 @@ def __init__(self,*arg,**args): @classmethod def _itemIsValid(cls,item): return EventID._isValid(item) - def configValueForItem(self,item,options): + def configValueForItem(self,item,options:PrintOptions) -> str: return EventID.formatValueForConfig(item) - def pythonValueForItem(self,item, options): + def pythonValueForItem(self,item, options:PrintOptions) -> str: # we tolerate strings as members if isinstance(item, str): return '"'+item+'"' else: return item.dumpPython(options) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): return VEventID(*_ValidatingParameterListBase._itemsFromStrings(value,EventID._valueFromString)) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): cppIDs = list() for i in self: item = i @@ -1203,19 +1203,19 @@ class VLuminosityBlockRange(_ValidatingParameterListBase): def __init__(self,*arg,**args): super(VLuminosityBlockRange,self).__init__(*arg,**args) @classmethod - def _itemIsValid(cls,item): + def _itemIsValid(cls,item) -> builtins.bool: return LuminosityBlockRange._isValid(item) - def configValueForItem(self,item,options): + def configValueForItem(self,item,options:PrintOptions) -> str: return LuminosityBlockRange.formatValueForConfig(item) - def pythonValueForItem(self,item, options): + def pythonValueForItem(self,item, options:PrintOptions) -> str: if isinstance(item, str): return '"'+item+'"' else: return item.dumpPython(options) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): return VLuminosityBlockRange(*_ValidatingParameterListBase._itemsFromStrings(value,VLuminosityBlockRange._valueFromString)) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): cppIDs = list() for i in self: item = i @@ -1229,19 +1229,19 @@ class VEventRange(_ValidatingParameterListBase): def __init__(self,*arg,**args): super(VEventRange,self).__init__(*arg,**args) @classmethod - def _itemIsValid(cls,item): + def _itemIsValid(cls,item) -> builtins.bool: return EventRange._isValid(item) - def configValueForItem(self,item,options): + def configValueForItem(self,item,options:PrintOptions) -> str: return EventRange.formatValueForConfig(item) - def pythonValueForItem(self,item, options): + def pythonValueForItem(self,item, options:PrintOptions) -> str: if isinstance(item, str): return '"'+item+'"' else: return item.dumpPython(options) @staticmethod - def _valueFromString(value): + def _valueFromString(value:str): return VEventRange(*_ValidatingParameterListBase._itemsFromStrings(value,VEventRange._valueFromString)) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): cppIDs = list() for i in self: item = i @@ -1256,17 +1256,17 @@ def __init__(self,*arg,**args): super(VPSet,self).__init__(*arg,**args) self._nPerLine = 1 @classmethod - def _itemIsValid(cls,item): + def _itemIsValid(cls,item) -> builtins.bool: return isinstance(item, PSet) and PSet._isValid(item) - def configValueForItem(self,item, options): + def configValueForItem(self,item, options:PrintOptions) -> str: return PSet.configValue(item, options) - def pythonValueForItem(self,item, options): + def pythonValueForItem(self,item, options:PrintOptions) -> str: return PSet.dumpPython(item,options) def copy(self): return copy.copy(self) - def _place(self,name,proc): + def _place(self,name:str,proc): proc._placeVPSet(name,self) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): # translate the PSet members into C++ parameterSets parametersets = list() for pset in self: @@ -1277,7 +1277,7 @@ def insertInto(self, parameterSet, myname): # XXX FIXME handle refToPSet def directDependencies(self): return [] - def __repr__(self): + def __repr__(self) -> str: return self.dumpPython() def makeCppPSet(module,cppPSetMaker): @@ -1295,111 +1295,111 @@ def makeCppPSet(module,cppPSetMaker): class _ConvertToPSet(object): def __init__(self): self.pset = PSet() - def addInt32(self,tracked,label,value): + def addInt32(self,tracked:bool,label:str,value): v = int32(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addUInt32(self,tracked,label,value): + def addUInt32(self,tracked:bool,label:str,value): v = uint32(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addInt64(self,tracked,label,value): + def addInt64(self,tracked:bool,label:str,value): v = int64(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addUInt64(self,tracked,label,value): + def addUInt64(self,tracked:bool,label:str,value): v = uint64(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addBool(self,tracked,label,value): + def addBool(self,tracked:bool,label:str,value): v = bool(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addDouble(self,tracked,label,value): + def addDouble(self,tracked:bool,label:str,value): v = double(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addString(self,tracked,label,value): + def addString(self,tracked:bool,label:str,value): v = string(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addInputTag(self,tracked,label,value): + def addInputTag(self,tracked:bool,label:str,value): v = copy.deepcopy(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addESInputTag(self,tracked,label,value): + def addESInputTag(self,tracked:bool,label:str,value): v = copy.deepcopy(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addEventID(self,tracked,label,value): + def addEventID(self,tracked:bool,label:str,value): v = copy.deepcopy(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addEventRange(self,tracked,label,value): + def addEventRange(self,tracked:bool,label:str,value): v = copy.deepcopy(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addLuminosityBlockID(self,tracked,label,value): + def addLuminosityBlockID(self,tracked:bool,label:str,value): v = copy.deepcopy(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addLuminosityBlockRange(self,tracked,label,value): + def addLuminosityBlockRange(self,tracked:bool,label:str,value): v = copy.deepcopy(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addVInt32(self,tracked,label,value): + def addVInt32(self,tracked:bool,label:str,value): v = vint32(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addVUInt32(self,tracked,label,value): + def addVUInt32(self,tracked:bool,label:str,value): v = vuint32(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addVInt64(self,tracked,label,value): + def addVInt64(self,tracked:bool,label:str,value): v = vint64(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addVUInt64(self,tracked,label,value): + def addVUInt64(self,tracked:bool,label:str,value): v = vuint64(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addVBool(self,tracked,label,value): + def addVBool(self,tracked:bool,label:str,value): v = vbool(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addVDouble(self,tracked,label,value): + def addVDouble(self,tracked:bool,label:str,value): v = vdouble(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addVString(self,tracked,label,value): + def addVString(self,tracked:bool,label:str,value): v = vstring(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addVInputTag(self,tracked,label,value): + def addVInputTag(self,tracked:bool,label:str,value): v = VInputTag(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addVESInputTag(self,tracked,label,value): + def addVESInputTag(self,tracked:bool,label:str,value): v = VESInputTag(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addVEventID(self,tracked,label,value): + def addVEventID(self,tracked:bool,label:str,value): v = VEventID(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addVEventRange(self,tracked,label,value): + def addVEventRange(self,tracked:bool,label:str,value): v = VEventRange(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addVLuminosityBlockID(self,tracked,label,value): + def addVLuminosityBlockID(self,tracked:bool,label:str,value): v = VLuminosityBlockID(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addVLuminosityBlockRange(self,tracked,label,value): + def addVLuminosityBlockRange(self,tracked:bool,label:str,value): v = VLuminosityBlockRange(value) v.setIsTracked(tracked) setattr(self.pset,label,v) - def addNewFileInPath(self,tracked,label,value): + def addNewFileInPath(self,tracked:bool,label:str,value): v = FileInPath(value) v.setIsTracked(tracked) setattr(self.pset,label,v) @@ -1429,7 +1429,7 @@ def addVPSet(self,tracked,label,value): v.setIsTracked(tracked) setattr(self.pset,label,v) -def convertToPSet(name,module): +def convertToPSet(name:str,module): convert = _ConvertToPSet() module.insertInto(convert,name) return getattr(convert.pset,name) @@ -1466,16 +1466,16 @@ def clone(self, *args, **params): saveOrigin(returnValue, 1) return returnValue - def _place(self,name,proc): + def _place(self,name:str,proc): proc._placeAlias(name,self) - def nameInProcessDesc_(self, myname): + def nameInProcessDesc_(self, myname:str): return myname; - def appendToProcessDescList_(self, lst, myname): + def appendToProcessDescList_(self, lst, myname:str): lst.append(self.nameInProcessDesc_(myname)) - def insertInto(self, parameterSet, myname): + def insertInto(self, parameterSet, myname:str): newpset = parameterSet.newPSet() newpset.addString(True, "@module_label", myname) newpset.addString(True, "@module_type", type(self).__name__) @@ -1485,7 +1485,7 @@ def insertInto(self, parameterSet, myname): param.insertInto(newpset, name) parameterSet.addPSet(True, self.nameInProcessDesc_(myname), newpset) - def dumpPython(self, options=PrintOptions()): + def dumpPython(self, options:PrintOptions=PrintOptions()) -> str: specialImportRegistry.registerUse(self) resultList = ['cms.EDAlias('] separator = "" diff --git a/FWCore/ParameterSet/python/pfnInPath.py b/FWCore/ParameterSet/python/pfnInPath.py index ca7d675e118ff..49a5bf4220bb7 100644 --- a/FWCore/ParameterSet/python/pfnInPath.py +++ b/FWCore/ParameterSet/python/pfnInPath.py @@ -1,7 +1,7 @@ import FWCore.ParameterSet.Config as cms import os, os.path -def pfnInPath(name): +def pfnInPath(name:str): for path in os.environ['CMSSW_SEARCH_PATH'].split(':'): fn = os.path.join(path, name) if os.path.isfile(fn): diff --git a/FWCore/ParameterSet/python/processFromFile.py b/FWCore/ParameterSet/python/processFromFile.py index 597a48c81dd9e..7d5bff792eb96 100644 --- a/FWCore/ParameterSet/python/processFromFile.py +++ b/FWCore/ParameterSet/python/processFromFile.py @@ -2,7 +2,7 @@ import types from importlib.machinery import SourceFileLoader -def processFromFile(filename, args=None): +def processFromFile(filename:str, args=None): old_sys_argv = None if args is not None: old_sys_argv = sys.argv[:]