2121import os
2222import re
2323import sys
24- from abc import ABC
25- from typing import Union
2624from enum import Enum
25+ from typing import Union
2726
2827github_mode = False # GitHub mode
2928prefix_disable = "o2-linter: disable=" # prefix for disabling tests
@@ -154,7 +153,7 @@ def block_ranges(line: str, char_open: str, char_close: str) -> "list[list[int]]
154153 """Get list of index ranges of longest blocks opened with char_open and closed with char_close."""
155154 # print(f"Looking for {char_open}{char_close} blocks in \"{line}\".")
156155 # print(line)
157- list_ranges : " list[list[int]]" = []
156+ list_ranges : list [list [int ]] = []
158157 if not all ((line , len (char_open ) == 1 , len (char_close ) == 1 )):
159158 return list_ranges
160159
@@ -189,7 +188,7 @@ def direction(char: str) -> int:
189188 return list_ranges
190189
191190
192- class TestSpec ( ABC ) :
191+ class TestSpec :
193192 """Prototype of a test class"""
194193
195194 name : str = "test-template" # short name of the test
@@ -558,9 +557,7 @@ def test_line(self, line: str) -> bool:
558557 pattern_pdg_code = r"[+-]?(k[A-Z][a-zA-Z0-9]*|[0-9]+)"
559558 if re .search (rf"->GetParticle\({ pattern_pdg_code } \)->Mass\(\)" , line ):
560559 return False
561- if re .search (rf"->Mass\({ pattern_pdg_code } \)" , line ):
562- return False
563- return True
560+ return not re .search (rf"->Mass\({ pattern_pdg_code } \)" , line )
564561
565562
566563class TestLogging (TestSpec ):
@@ -1395,7 +1392,7 @@ def file_matches(self, path: str) -> bool:
13951392 return super ().file_matches (path ) and "/Core/" not in path
13961393
13971394 def test_file (self , path : str , content ) -> bool :
1398- file_name = os .path .basename (path ). rstrip ( ".cxx" )
1395+ file_name = os .path .basename (path )[: - 4 ] # file name without suffix
13991396 base_struct_name = f"{ file_name [0 ].upper ()} { file_name [1 :]} " # expected base of struct names
14001397 if match := re .search ("PWG([A-Z]{2})/" , path ):
14011398 name_pwg = match .group (1 )
@@ -1418,10 +1415,7 @@ def test_file(self, path: str, content) -> bool:
14181415 struct_name = words [1 ]
14191416 struct_names .append (struct_name )
14201417 # print(f"Found structs: {struct_names}.")
1421- for struct_name in struct_names :
1422- if re .match (base_struct_name , struct_name ):
1423- return True
1424- return False
1418+ return any (re .match (base_struct_name , struct_name ) for struct_name in struct_names )
14251419
14261420
14271421class TestNameConfigurable (TestSpec ):
@@ -1513,9 +1507,7 @@ def file_matches(self, path: str) -> bool:
15131507
15141508 def test_file (self , path : str , content ) -> bool :
15151509 file_name = os .path .basename (path )
1516- if "/Tasks/" in path and not file_name .startswith ("task" ):
1517- return False
1518- return True
1510+ return not ("/Tasks/" in path and not file_name .startswith ("task" ))
15191511
15201512
15211513class TestHfStructMembers (TestSpec ):
@@ -1614,7 +1606,7 @@ def main():
16141606 )
16151607 args = parser .parse_args ()
16161608 if args .github :
1617- global github_mode # pylint: disable=global-statement
1609+ global github_mode # pylint: disable=global-statement # noqa: PLW0603
16181610 github_mode = True
16191611
16201612 tests = [] # list of activated tests
@@ -1676,7 +1668,7 @@ def main():
16761668 test_names = [t .name for t in tests ] # short names of activated tests
16771669 suffixes = tuple ({s for test in tests for s in test .suffixes }) # all suffixes from all enabled tests
16781670 passed = True # global result of all tests
1679- n_files_bad = { name : 0 for name in test_names } # counter of files with issues
1671+ n_files_bad = dict . fromkeys ( test_names , 0 ) # counter of files with issues
16801672
16811673 # Report overview before running.
16821674 print (f"Testing { len (args .paths )} files." )
@@ -1693,15 +1685,15 @@ def main():
16931685 # print(f"Skipping path \"{path}\".")
16941686 continue
16951687 try :
1696- with open (path , "r" , encoding = "utf-8" ) as file :
1688+ with open (path , encoding = "utf-8" ) as file :
16971689 content = file .readlines ()
16981690 for test in tests :
16991691 result = test .run (path , content )
17001692 if not result :
17011693 n_files_bad [test .name ] += 1
17021694 passed = False
17031695 # print(f"File \"{path}\" {'passed' if result else 'failed'} the test {test.name}.")
1704- except IOError :
1696+ except OSError :
17051697 print (f'Failed to open file "{ path } ".' )
17061698 sys .exit (1 )
17071699
0 commit comments