Skip to content

Commit

Permalink
Fix issue #112 - unidiomatic-typecheck lint warn
Browse files Browse the repository at this point in the history
Change this:
    type(obj) is T
to this:
    isinstance(obj, T)
  • Loading branch information
cjdrake committed Mar 15, 2015
1 parent 8685f3d commit 4dfeca8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
40 changes: 20 additions & 20 deletions pyeda/boolalg/bfarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def __init__(self, objs, shape=None, ftype=None):
raise ValueError("could not determine ftype parameter")
self.ftype = autoftype
else:
if type(ftype) is not type:
if not isinstance(ftype, type):
raise TypeError("expected ftype to be a type")
if not (autoftype is None or ftype is autoftype):
raise ValueError("expected ftype to match items")
Expand Down Expand Up @@ -484,7 +484,7 @@ def __getitem__(self, key):
fsls = self._fill_slices(sls)
nsls = self._norm_slices(fsls)

if all(type(nsl) is int for nsl in nsls):
if all(isinstance(nsl, int) for nsl in nsls):
# Speed hack for coordinates
return self._items[self._coord2offset(nsls)]
else:
Expand All @@ -506,12 +506,12 @@ def __setitem__(self, key, item):
fsls = self._fill_slices(sls)
nsls = self._norm_slices(fsls)

if all(type(nsl) is int for nsl in nsls):
if all(isinstance(nsl, int) for nsl in nsls):
if not isinstance(item, boolfunc.Function):
raise TypeError("expected item to be a Function")
self._items[self._coord2offset(nsls)] = item
else:
if type(item) is not farray:
if not isinstance(item, farray):
raise TypeError("expected item to be an farray")
coords = list(_iter_coords(nsls))
if item.size != len(coords):
Expand Down Expand Up @@ -552,9 +552,9 @@ def __lshift__(self, obj):
.. seealso:: :meth:`lsh`
"""
if type(obj) is tuple and len(obj) == 2:
if isinstance(obj, tuple) and len(obj) == 2:
return self.lsh(obj[0], obj[1])[0]
elif type(obj) is int:
elif isinstance(obj, int):
return self.lsh(obj)[0]
else:
raise TypeError("expected int or (int, farray)")
Expand All @@ -567,9 +567,9 @@ def __rshift__(self, obj):
.. seealso:: :meth:`rsh`
"""
if type(obj) is tuple and len(obj) == 2:
if isinstance(obj, tuple) and len(obj) == 2:
return self.rsh(obj[0], obj[1])[0]
elif type(obj) is int:
elif isinstance(obj, int):
return self.rsh(obj)[0]
else:
raise TypeError("expected int or (int, farray)")
Expand Down Expand Up @@ -606,7 +606,7 @@ def __radd__(self, other):

def __mul__(self, num):
"""Repetition operator"""
if type(num) is not int:
if not isinstance(num, int):
raise TypeError("expected multiplier to be an int")
if num < 0:
raise ValueError("expected multiplier to be non-negative")
Expand Down Expand Up @@ -850,7 +850,7 @@ def decode(self):
def _keys2sls(self, keys, key2sl):
"""Convert an input key to a list of slices."""
sls = list()
if type(keys) is tuple:
if isinstance(keys, tuple):
for key in keys:
sls.append(key2sl(key))
else:
Expand Down Expand Up @@ -929,9 +929,9 @@ def _dims2shape(*dims):
raise ValueError("expected at least one dimension spec")
shape = list()
for dim in dims:
if type(dim) is int:
if isinstance(dim, int):
dim = (0, dim)
if type(dim) is tuple and len(dim) == 2:
if isinstance(dim, tuple) and len(dim) == 2:
if dim[0] < 0:
raise ValueError("expected low dimension to be >= 0")
if dim[1] < 0:
Expand Down Expand Up @@ -1071,10 +1071,10 @@ def _itemize(objs):

def _check_shape(shape):
"""Verify that a shape has the right format."""
if type(shape) is tuple:
if isinstance(shape, tuple):
for dim in shape:
if (type(dim) is tuple and len(dim) == 2 and
type(dim[0]) is int and type(dim[1]) is int):
if (isinstance(dim, tuple) and len(dim) == 2 and
isinstance(dim[0], int) and isinstance(dim[1], int)):
if dim[0] < 0:
raise ValueError("expected low dimension to be >= 0")
if dim[1] < 0:
Expand All @@ -1089,9 +1089,9 @@ def _check_shape(shape):

def _get_key2sl(key):
"""Convert a key part to a slice part."""
if type(key) in {int, farray} or key is Ellipsis:
if isinstance(key, (int, farray)) or key is Ellipsis:
return key
elif type(key) is slice:
elif isinstance(key, slice):
# Forbid slice steps
if key.step is not None:
raise ValueError("farray slice step is not supported")
Expand All @@ -1104,9 +1104,9 @@ def _get_key2sl(key):

def _set_key2sl(key):
"""Convert a key part to a slice part."""
if type(key) is int or key is Ellipsis:
if isinstance(key, int) or key is Ellipsis:
return key
elif type(key) is slice:
elif isinstance(key, slice):
# Forbid slice steps
if key.step is not None:
raise ValueError("farray slice step is not supported")
Expand Down Expand Up @@ -1210,7 +1210,7 @@ def _iter_coords(nsls):
# First convert all slices to ranges
ranges = list()
for nsl in nsls:
if type(nsl) is int:
if isinstance(nsl, int):
ranges.append(range(nsl, nsl+1))
else:
ranges.append(range(nsl.start, nsl.stop))
Expand Down
6 changes: 3 additions & 3 deletions pyeda/boolalg/boolfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def num2point(num, vs):
The ``a b c`` column is the binary representation of *num*
written in little-endian order.
"""
if type(num) is not int:
if not isinstance(num, int):
fstr = "expected num to be an int, got {0.__name__}"
raise TypeError(fstr.format(type(num)))
n = len(vs)
Expand Down Expand Up @@ -215,7 +215,7 @@ def num2term(num, fs, conj=False):
The ``f g h`` column is the binary representation of *num*
written in little-endian order.
"""
if type(num) is not int:
if not isinstance(num, int):
fstr = "expected num to be an int, got {0.__name__}"
raise TypeError(fstr.format(type(num)))
n = len(fs)
Expand Down Expand Up @@ -524,7 +524,7 @@ def __radd__(self, other):
def __mul__(self, num):
"""Repetition operator"""
from pyeda.boolalg.bfarray import farray
if type(num) is not int:
if not isinstance(num, int):
raise TypeError("expected multiplier to be an int")
if num < 0:
raise ValueError("expected multiplier to be non-negative")
Expand Down
4 changes: 2 additions & 2 deletions pyeda/boolalg/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def expr(obj, simplify=True):
# False, True, 0, 1
elif isinstance(obj, int) and obj in {0, 1}:
return _CONSTS[obj]
elif type(obj) is str:
elif isinstance(obj, str):
ast = pyeda.parsing.boolexpr.parse(obj)
ex = ast2expr(ast)
if simplify:
Expand Down Expand Up @@ -768,7 +768,7 @@ def box(obj):
# False, True, 0, 1
elif isinstance(obj, int) and obj in {0, 1}:
return _CONSTS[obj]
elif type(obj) is str:
elif isinstance(obj, str):
ast = pyeda.parsing.boolexpr.parse(obj)
return ast2expr(ast)
else:
Expand Down
24 changes: 12 additions & 12 deletions pyeda/parsing/boolexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def parse(s):
def _expect_token(lexer, types):
"""Return the next token, or raise an exception."""
tok = next(lexer)
if any(type(tok) is t for t in types):
if any(isinstance(tok, t) for t in types):
return tok
else:
raise Error("unexpected token: " + str(tok))
Expand All @@ -413,7 +413,7 @@ def _ite(lexer):

tok = next(lexer)
# IMPL '?' ITE ':' ITE
if type(tok) is OP_question:
if isinstance(tok, OP_question):
d1 = _ite(lexer)
_expect_token(lexer, {OP_colon})
d0 = _ite(lexer)
Expand All @@ -430,11 +430,11 @@ def _impl(lexer):

tok = next(lexer)
# SUMTERM '=>' IMPL
if type(tok) is OP_rarrow:
if isinstance(tok, OP_rarrow):
q = _impl(lexer)
return ('implies', p, q)
# SUMTERM '<=>' IMPL
elif type(tok) is OP_lrarrow:
elif isinstance(tok, OP_lrarrow):
q = _impl(lexer)
return ('equal', p, q)
# SUMTERM
Expand All @@ -457,7 +457,7 @@ def _sumterm_prime(lexer):
"""Return a sum term' expression, eliminates left recursion."""
tok = next(lexer)
# '|' XORTERM SUMTERM'
if type(tok) is OP_or:
if isinstance(tok, OP_or):
xorterm = _xorterm(lexer)
sumterm_prime = _sumterm_prime(lexer)
if sumterm_prime is None:
Expand All @@ -484,7 +484,7 @@ def _xorterm_prime(lexer):
"""Return an xor term' expression, eliminates left recursion."""
tok = next(lexer)
# '^' PRODTERM XORTERM'
if type(tok) is OP_xor:
if isinstance(tok, OP_xor):
prodterm = _prodterm(lexer)
xorterm_prime = _xorterm_prime(lexer)
if xorterm_prime is None:
Expand All @@ -511,7 +511,7 @@ def _prodterm_prime(lexer):
"""Return a product term' expression, eliminates left recursion."""
tok = next(lexer)
# '&' FACTOR PRODTERM'
if type(tok) is OP_and:
if isinstance(tok, OP_and):
factor = _factor(lexer)
prodterm_prime = _prodterm_prime(lexer)
if prodterm_prime is None:
Expand Down Expand Up @@ -542,7 +542,7 @@ def _factor(lexer):
_expect_token(lexer, {LPAREN})
tok = next(lexer)
# OPN '(' ')'
if type(tok) is RPAREN:
if isinstance(tok, RPAREN):
xs = tuple()
# OPN '(' XS ')'
else:
Expand Down Expand Up @@ -594,7 +594,7 @@ def _zom_arg(lexer):
"""Return zero or more arguments."""
tok = next(lexer)
# ',' EXPR ZOM_X
if type(tok) is COMMA:
if isinstance(tok, COMMA):
return (_expr(lexer), ) + _zom_arg(lexer)
# null
else:
Expand All @@ -608,7 +608,7 @@ def _variable(lexer):

tok = next(lexer)
# NAMES '[' ... ']'
if type(tok) is LBRACK:
if isinstance(tok, LBRACK):
indices = _indices(lexer)
_expect_token(lexer, {RBRACK})
# NAMES
Expand All @@ -631,7 +631,7 @@ def _zom_name(lexer):
"""Return zero or more names."""
tok = next(lexer)
# '.' NAME ZOM_NAME
if type(tok) is DOT:
if isinstance(tok, DOT):
first = _expect_token(lexer, {NameToken}).value
rest = _zom_name(lexer)
return (first, ) + rest
Expand All @@ -652,7 +652,7 @@ def _zom_index(lexer):
"""Return zero or more indices."""
tok = next(lexer)
# ',' INT
if type(tok) is COMMA:
if isinstance(tok, COMMA):
first = _expect_token(lexer, {IntegerToken}).value
rest = _zom_index(lexer)
return (first, ) + rest
Expand Down
16 changes: 8 additions & 8 deletions pyeda/parsing/dimacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def integer(self, text):
def _expect_token(lexer, types):
"""Return the next token, or raise an exception."""
tok = next(lexer)
if any(type(tok) is t for t in types):
if any(isinstance(tok, t) for t in types):
return tok
else:
raise Error("unexpected token: " + str(tok))
Expand Down Expand Up @@ -227,10 +227,10 @@ def _clause(lexer, varname, nvars):
def _lits(lexer, varname, nvars):
"""Return a tuple of DIMACS CNF clause literals."""
tok = _expect_token(lexer, {OP_not, IntegerToken})
if type(tok) is IntegerToken and tok.value == 0:
if isinstance(tok, IntegerToken) and tok.value == 0:
return tuple()
else:
if type(tok) is OP_not:
if isinstance(tok, OP_not):
neg = True
tok = _expect_token(lexer, {IntegerToken})
else:
Expand Down Expand Up @@ -387,17 +387,17 @@ def _sat_formula(lexer, varname, fmt, nvars):
types = {IntegerToken, LPAREN} | _SAT_TOKS[fmt]
tok = _expect_token(lexer, types)
# INT
if type(tok) is IntegerToken:
if isinstance(tok, IntegerToken):
index = tok.value
if not 0 < index <= nvars:
fstr = "formula literal {} outside valid range: (0, {}]"
raise Error(fstr.format(index, nvars))
return ('var', (varname, ), (index, ))
# '-'
elif type(tok) is OP_not:
elif isinstance(tok, OP_not):
tok = _expect_token(lexer, {IntegerToken, LPAREN})
# '-' INT
if type(tok) is IntegerToken:
if isinstance(tok, IntegerToken):
index = tok.value
if not 0 < index <= nvars:
fstr = "formula literal {} outside valid range: (0, {}]"
Expand All @@ -409,7 +409,7 @@ def _sat_formula(lexer, varname, fmt, nvars):
_expect_token(lexer, {RPAREN})
return ('not', formula)
# '(' FORMULA ')'
elif type(tok) is LPAREN:
elif isinstance(tok, LPAREN):
formula = _sat_formula(lexer, varname, fmt, nvars)
_expect_token(lexer, {RPAREN})
return formula
Expand All @@ -425,7 +425,7 @@ def _formulas(lexer, varname, fmt, nvars):
"""Return a tuple of DIMACS SAT formulas."""
types = {IntegerToken, LPAREN} | _SAT_TOKS[fmt]
tok = lexer.peek_token()
if any(type(tok) is t for t in types):
if any(isinstance(tok, t) for t in types):
first = _sat_formula(lexer, varname, fmt, nvars)
rest = _formulas(lexer, varname, fmt, nvars)
return (first, ) + rest
Expand Down

0 comments on commit 4dfeca8

Please sign in to comment.