Skip to content

Commit

Permalink
Fix some issues reported by ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer committed Nov 29, 2024
1 parent 9575b23 commit 5112c8a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
1 change: 0 additions & 1 deletion setup_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# This software is licensed under the terms of the BSD license.
# http://opensource.org/licenses/BSD-3-Clause

import sys
from distutils.core import setup
from validate import __version__ as VERSION

Expand Down
20 changes: 10 additions & 10 deletions src/configobj/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def __init__(self, section):

def interpolate(self, key, value):
# short-cut
if not self._cookie in value:
if self._cookie not in value:
return value

def recursive_interpolate(key, value, section, backtrail):
Expand Down Expand Up @@ -528,7 +528,7 @@ def _interpolate(self, key, value):
except AttributeError:
# not yet: first time running _interpolate(), so pick the engine
name = self.main.interpolation
if name == True: # note that "if name:" would be incorrect here
if name is True: # note that "if name:" would be incorrect here
# backwards-compatibility: interpolation=True means use default
name = DEFAULT_INTERPOLATION
name = name.lower() # so that "Template", "template", etc. all work
Expand Down Expand Up @@ -948,9 +948,9 @@ def as_bool(self, key):
0
"""
val = self[key]
if val == True:
if val is True:
return True
elif val == False:
elif val is False:
return False
else:
try:
Expand Down Expand Up @@ -2237,7 +2237,7 @@ def validate_entry(entry, spec, val, missing, ret_true, ret_false):
if entry in ('__many__', '___many___'):
# reserved names
continue
if (not entry in section.scalars) or (entry in section.defaults):
if (entry not in section.scalars) or (entry in section.defaults):
# missing entries
# or entries from defaults
missing = True
Expand Down Expand Up @@ -2300,9 +2300,9 @@ def validate_entry(entry, spec, val, missing, ret_true, ret_false):
section.inline_comments[entry] = configspec.inline_comments.get(entry, '')
check = self.validate(validator, preserve_errors=preserve_errors, copy=copy, section=section[entry])
out[entry] = check
if check == False:
if check is False:
ret_true = False
elif check == True:
elif check is True:
ret_false = False
else:
ret_true = False
Expand Down Expand Up @@ -2421,15 +2421,15 @@ def flatten_errors(cfg, res, levels=None, results=None):
# first time called
levels = []
results = []
if res == True:
if res is True:
return sorted(results)
if res == False or isinstance(res, Exception):
if res is False or isinstance(res, Exception):
results.append((levels[:], None, res))
if levels:
levels.pop()
return sorted(results)
for (key, val) in list(res.items()):
if val == True:
if val is True:
continue
if isinstance(cfg.get(key), dict):
# Go down one level
Expand Down
17 changes: 9 additions & 8 deletions src/configobj/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@

import re
import sys
from pprint import pprint


_list_arg = re.compile(r'''
Expand Down Expand Up @@ -264,7 +263,8 @@ def dottedQuadToNum(ip):
"""

# import here to avoid it when ip_addr values are not used
import socket, struct
import socket
import struct

try:
return struct.unpack('!L',
Expand Down Expand Up @@ -314,7 +314,8 @@ def numToDottedQuad(num):
"""

# import here to avoid it when ip_addr values are not used
import socket, struct
import socket
import struct

# no need to intercept here, 4294967295L is fine
if num > int(4294967295) or num < 0:
Expand Down Expand Up @@ -653,7 +654,7 @@ def _parse_check(self, check):
keymatch = self._key_arg.match(arg)
if keymatch:
val = keymatch.group(2)
if not val in ("'None'", '"None"'):
if val not in ("'None'", '"None"'):
# Special case a quoted None
val = self._unquote(val)
fun_kwargs[keymatch.group(1)] = val
Expand Down Expand Up @@ -740,7 +741,7 @@ def _is_num_param(names, values, to_float=False):
elif isinstance(val, (int, float, str)):
try:
out_params.append(fun(val))
except ValueError as e:
except ValueError:
raise VdtParamError(name, val)
else:
raise VdtParamError(name, val)
Expand Down Expand Up @@ -919,9 +920,9 @@ def is_boolean(value):
# we do an equality test rather than an identity test
# this ensures Python 2.2 compatibilty
# and allows 0 and 1 to represent True and False
if value == False:
if value is False:
return False
elif value == True:
elif value is True:
return True
else:
raise VdtTypeError(value)
Expand Down Expand Up @@ -1301,7 +1302,7 @@ def is_option(value, *options):
"""
if not isinstance(value, str):
raise VdtTypeError(value)
if not value in options:
if value not in options:
raise VdtValueError(value)
return value

Expand Down
2 changes: 1 addition & 1 deletion src/tests/test_configobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ def test_unicode_handling():
file_like.seek(0)
uc2 = ConfigObj(file_like)
assert uc2 == uc
assert uc2.filename == None
assert uc2.filename is None
assert uc2.newlines == '\r'


Expand Down
2 changes: 1 addition & 1 deletion src/tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from configobj import ConfigObj
import pytest
from configobj.validate import Validator, VdtValueTooSmallError
from configobj.validate import VdtValueTooSmallError


class TestImporting(object):
Expand Down
4 changes: 2 additions & 2 deletions src/tests/test_validate_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ def test_invalid_lines_with_percents(tmpdir, specpath):
ini = tmpdir.join('config.ini')
ini.write('extra: %H:%M\n')
with pytest.raises(ParseError):
conf = ConfigObj(str(ini), configspec=specpath, file_error=True)
ConfigObj(str(ini), configspec=specpath, file_error=True)


def test_no_parent(tmpdir, specpath):
ini = tmpdir.join('config.ini')
ini.write('[[haha]]')
with pytest.raises(NestingError):
conf = ConfigObj(str(ini), configspec=specpath, file_error=True)
ConfigObj(str(ini), configspec=specpath, file_error=True)


def test_re_dos(val):
Expand Down

0 comments on commit 5112c8a

Please sign in to comment.