mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-23 04:24:00 -07:00
Add a check for type() instead of isinstance() (#18439)
This commit is contained in:
parent
e9b193d6ae
commit
05f02371ce
3 changed files with 76 additions and 0 deletions
|
@ -46,6 +46,7 @@ import yaml.reader
|
|||
|
||||
BLACKLIST_DIRS = frozenset(('.git', 'test', '.github', '.idea'))
|
||||
INDENT_REGEX = re.compile(r'([\t]*)')
|
||||
TYPE_REGEX = re.compile(r'.*(if|or)(\s+.*|\s+)(?<!_)(?<!str\()type\(.*')
|
||||
BLACKLIST_IMPORTS = {
|
||||
'requests': {
|
||||
'new_only': True,
|
||||
|
@ -197,6 +198,14 @@ class ModuleValidator(Validator):
|
|||
if not self.text.startswith('#!/usr/bin/python'):
|
||||
self.errors.append('Interpreter line is not "#!/usr/bin/python"')
|
||||
|
||||
def _check_type_instead_of_isinstance(self, powershell=False):
|
||||
if powershell:
|
||||
return
|
||||
for line_no, line in enumerate(self.text.splitlines()):
|
||||
typekeyword = TYPE_REGEX.match(line)
|
||||
if typekeyword:
|
||||
self.errors.append('Type comparison using type() found on line %d. Use isinstance() instead' % (line_no + 1))
|
||||
|
||||
def _check_for_sys_exit(self):
|
||||
if 'sys.exit(' in self.text:
|
||||
self.errors.append('sys.exit() call found. Should be '
|
||||
|
@ -592,6 +601,7 @@ class ModuleValidator(Validator):
|
|||
self._check_for_gpl3_header()
|
||||
if not self._just_docs():
|
||||
self._check_interpreter(powershell=self._powershell_module())
|
||||
self._check_type_instead_of_isinstance(powershell=self._powershell_module())
|
||||
|
||||
|
||||
class PythonPackageValidator(Validator):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue