ATLAS Offline Software
Loading...
Searching...
No Matches
ChainDefInMenu.py
Go to the documentation of this file.
1# Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
2
3import typing
4from typing import List
5import dataclasses
6from dataclasses import dataclass, field
7
8@dataclass(frozen=True)
10 """Class to hold chain properties with (limited) type-checking"""
11 name: str # mandatory
12 groups: List[str] # mandatory
13 monGroups: List[str] = field(default_factory = list)
14 l1SeedThresholds: List[str] = field(default_factory = list)
15 stream: List[str] = field(default_factory = lambda : ['Main'])
16 mergingStrategy: str = 'auto'
17 mergingOrder: List[str] = field(default_factory = list)
18 mergingOffset: int = -1
19
20 # currently we don't have a type-checker in LCG so we
21 # implement a limited version ourselves:
22 def _check_types(self):
23 """Check field types"""
24 for f in dataclasses.fields(self):
25 actual_type = typing.get_origin(f.type) or f.type
26 value = getattr(self, f.name)
27 if actual_type == list:
28 if value:
29 for el in value:
30 if not isinstance(el, typing.get_args(f.type)):
31 return f
32 elif not isinstance(value, actual_type):
33 return f
34 return None
35
36 def __post_init__(self):
37 f = self._check_types()
38 if f is not None:
39 raise TypeError(f"Expected type of '{f.name}' to be {f.type}, "
40 f"got {repr(getattr(self, f.name))}")
41
42if __name__=='__main__':
43 ch = ChainProp(name='abc', groups=['g1'])