Skip to content

Commit

Permalink
pylint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
whoburg committed Jan 5, 2024
1 parent 28982a7 commit a21deca
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions gpkit/tools/docstring.py
Expand Up @@ -7,15 +7,15 @@

def expected_unbounded(instance, doc):
"Gets expected-unbounded variables from a string"
# pylint: disable=too-many-locals,too-many-nested-blocks
# pylint: disable=too-many-locals,too-many-nested-blocks,too-many-branches
exp_unbounded = set()
for direction in ["upper", "lower"]:
flag = direction[0].upper()+direction[1:]+" Unbounded\n"
count = doc.count(flag)
if count == 0:
continue
if count > 1:
raise ValueError("multiple instances of %s" % flag)
raise ValueError(f"multiple instances of {flag}")

idx = doc.index(flag) + len(flag)
idx2 = doc[idx:].index("\n")
Expand Down Expand Up @@ -44,11 +44,11 @@ def expected_unbounded(instance, doc):
for subdot in var.split("."):
obj = getattr(obj, subdot)
variables = obj
except AttributeError:
raise AttributeError("`%s` is noted in %s as "
"unbounded, but is not "
"an attribute of that model."
% (var, instance.__class__.__name__))
except AttributeError as err:
raise AttributeError(
f"`{var}` is noted in {instance.__class__.__name__} as"
" unbounded, but is not an attribute of that model."
) from err
if not hasattr(variables, "shape"):
variables = np.array([variables])
it = np.nditer(variables, flags=['multi_index', 'refs_ok'])
Expand All @@ -59,7 +59,7 @@ def expected_unbounded(instance, doc):
return exp_unbounded


class parse_variables: # pylint:disable=invalid-name
class parse_variables: # pylint:disable=invalid-name,too-few-public-methods
"""decorator for adding local Variables from a string.
Generally called as `@parse_variables(__doc__, globals())`.
Expand Down Expand Up @@ -119,15 +119,16 @@ def parse_varstring(string):
# imports, to be updated if more things are parsed above
out[0] = "from gpkit import Variable, VectorVariable" + " " + out[0]
for lines, indexs in (consts, variables, vecvars):
# pylint: disable=no-member # for lines.split()
for line, index in zip(lines.split("\n"), indexs):
out[index] = line + " # from '%s'" % out[index][1:].strip()
out[index] = line + f" # from '{out[index][1:].strip()}'"
return "\n".join(out)


def vv_declare(string, flag, idx2, countstr):
"Turns Variable declarations into VectorVariable ones"
length = string[len(flag):idx2].strip()
return countstr.replace("Variable(", "VectorVariable(%s, " % length)
return countstr.replace("Variable(", f"VectorVariable({length}, ")


# pylint: disable=unused-argument
Expand Down Expand Up @@ -157,10 +158,10 @@ def check_and_parse_flag(string, flag, declaration_func=None):
break
try:
units = line[line.index("[")+1:line.index("]")]
except ValueError:
except ValueError as err:
raise ValueError("A unit declaration bracketed by [] was"
" not found on the line reading:\n"
" %s" % line)
f" {line}") from err
nameval = line[:line.index("[")].split()
labelstart = line.index("]") + 1
if labelstart >= len(line):
Expand Down Expand Up @@ -188,14 +189,13 @@ def check_and_parse_flag(string, flag, declaration_func=None):
def variable_declaration(nameval, units, label, line, errorcatch=True):
"Turns parsed output into a Variable declaration"
if len(nameval) > 2:
raise ValueError("while parsing the line '%s', additional fields"
raise ValueError(f"while parsing the line '{line}', additional fields"
" (separated by whitespace) were found between Value"
" '%s' and the Units `%s`. %s"
% (line, nameval[1], units, PARSETIP))
f" '{nameval[1]}' and the Units `{units}`. {PARSETIP}")
if len(nameval) == 2:
out = ("{0} = self.{0} = Variable('{0}', {1}, '{2}', '{3}')")
out = "{0} = self.{0} = Variable('{0}', {1}, '{2}', '{3}')"
out = out.format(nameval[0], nameval[1], units, label)
elif len(nameval) == 1:
out = ("{0} = self.{0} = Variable('{0}', '{1}', '{2}')")
out = "{0} = self.{0} = Variable('{0}', '{1}', '{2}')"
out = out.format(nameval[0], units, label)
return out + "\n"

0 comments on commit a21deca

Please sign in to comment.