From 3ef85739e3e4d4fb05999167ce2e1b97a63fc272 Mon Sep 17 00:00:00 2001 From: sedrubal Date: Thu, 18 Oct 2018 17:16:42 +0200 Subject: [PATCH] Use isinstance(x, y) instead of type(x) == y Fixes pylint C0123. --- yamllint/config.py | 16 ++++++++-------- yamllint/linter.py | 2 +- yamllint/rules/document_end.py | 14 ++++++++------ yamllint/rules/indentation.py | 4 ++-- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/yamllint/config.py b/yamllint/config.py index f9e4de8..3a3f3c7 100644 --- a/yamllint/config.py +++ b/yamllint/config.py @@ -52,7 +52,7 @@ class YamlLintConfig(object): assert isinstance(base_config, YamlLintConfig) for rule in self.rules: - if (type(self.rules[rule]) == dict and + if (isinstance(self.rules[rule], dict) and rule in base_config.rules and base_config.rules[rule] is not False): base_config.rules[rule].update(self.rules[rule]) @@ -70,7 +70,7 @@ class YamlLintConfig(object): except Exception as e: raise YamlLintConfigError('invalid config: %s' % e) - if type(conf) != dict: + if not isinstance(conf, dict): raise YamlLintConfigError('invalid config: not a dict') self.rules = conf.get('rules', {}) @@ -85,7 +85,7 @@ class YamlLintConfig(object): raise YamlLintConfigError('invalid config: %s' % e) if 'ignore' in conf: - if type(conf['ignore']) != str: + if not isinstance(conf['ignore'], str): raise YamlLintConfigError( 'invalid config: ignore should contain file patterns') self.ignore = pathspec.PathSpec.from_lines( @@ -107,10 +107,10 @@ def validate_rule_conf(rule, conf): elif conf == 'enable': conf = {} - if type(conf) == dict: + if isinstance(conf, dict): if ('ignore' in conf and - type(conf['ignore']) != pathspec.pathspec.PathSpec): - if type(conf['ignore']) != str: + not isinstance(conf['ignore'], pathspec.pathspec.PathSpec)): + if not isinstance(conf['ignore'], str): raise YamlLintConfigError( 'invalid config: ignore should contain file patterns') conf['ignore'] = pathspec.PathSpec.from_lines( @@ -130,14 +130,14 @@ def validate_rule_conf(rule, conf): raise YamlLintConfigError( 'invalid config: unknown option "%s" for rule "%s"' % (optkey, rule.ID)) - if type(options[optkey]) == tuple: + if isinstance(options[optkey], tuple): if (conf[optkey] not in options[optkey] and type(conf[optkey]) not in options[optkey]): raise YamlLintConfigError( 'invalid config: option "%s" of "%s" should be in %s' % (optkey, rule.ID, options[optkey])) else: - if type(conf[optkey]) != options[optkey]: + if not isinstance(conf[optkey], options[optkey]): raise YamlLintConfigError( 'invalid config: option "%s" of "%s" should be %s' % (optkey, rule.ID, options[optkey].__name__)) diff --git a/yamllint/linter.py b/yamllint/linter.py index c8eff8d..aaf43e5 100644 --- a/yamllint/linter.py +++ b/yamllint/linter.py @@ -226,7 +226,7 @@ def run(input, conf, filepath=None): if conf.is_file_ignored(filepath): return () - if type(input) in (type(b''), type(u'')): # compat with Python 2 & 3 + if isinstance(input, (type(b''), type(u''))): # compat with Python 2 & 3 return _run(input, conf, filepath) elif hasattr(input, 'read'): # Python 2's file or Python 3's io.IOBase # We need to have everything in memory to parse correctly diff --git a/yamllint/rules/document_end.py b/yamllint/rules/document_end.py index 34323e4..8335a05 100644 --- a/yamllint/rules/document_end.py +++ b/yamllint/rules/document_end.py @@ -86,14 +86,16 @@ CONF = {'present': bool} def check(conf, token, prev, next, nextnext, context): if conf['present']: - if (isinstance(token, yaml.StreamEndToken) and - not (isinstance(prev, yaml.DocumentEndToken) or - isinstance(prev, yaml.StreamStartToken))): + is_stream_end = isinstance(token, yaml.StreamEndToken) + is_start = isinstance(token, yaml.DocumentStartToken) + prev_is_end_or_stream_start = isinstance( + prev, (yaml.DocumentEndToken, yaml.StreamStartToken) + ) + + if is_stream_end and not prev_is_end_or_stream_start: yield LintProblem(token.start_mark.line, 1, 'missing document end "..."') - elif (isinstance(token, yaml.DocumentStartToken) and - not (isinstance(prev, yaml.DocumentEndToken) or - isinstance(prev, yaml.StreamStartToken))): + elif is_start and not prev_is_end_or_stream_start: yield LintProblem(token.start_mark.line + 1, 1, 'missing document end "..."') diff --git a/yamllint/rules/indentation.py b/yamllint/rules/indentation.py index fb14faf..cf4ebc5 100644 --- a/yamllint/rules/indentation.py +++ b/yamllint/rules/indentation.py @@ -224,7 +224,7 @@ def check_scalar_indentation(conf, token, context): def compute_expected_indent(found_indent): def detect_indent(base_indent): - if type(context['spaces']) is not int: + if not isinstance(context['spaces'], int): context['spaces'] = found_indent - base_indent return base_indent + context['spaces'] @@ -312,7 +312,7 @@ def _check(conf, token, prev, next, nextnext, context): token.start_mark.line + 1 > context['cur_line']) def detect_indent(base_indent, next): - if type(context['spaces']) is not int: + if not isinstance(context['spaces'], int): context['spaces'] = next.start_mark.column - base_indent return base_indent + context['spaces']