Skip to content

Commit

Permalink
Fix pylint C0209 warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
martinholmer committed Jan 5, 2025
1 parent 9c77b54 commit 578eb6d
Show file tree
Hide file tree
Showing 20 changed files with 239 additions and 213 deletions.
8 changes: 4 additions & 4 deletions taxcalc/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ def lines(text, num_indent_spaces, max_line_length=77):
if params_with_diff:
mdata_base = baseline.specification(meta_data=True)
# write year
doc += '{}:\n'.format(year)
doc += f'{year}:\n'
for pname in sorted(params_with_diff):
# write updated value line
pval = getattr(updated, pname).tolist()[0]
Expand All @@ -1213,7 +1213,7 @@ def lines(text, num_indent_spaces, max_line_length=77):
pval = [bool(item) for item in pval]
else:
pval = bool(pval)
doc += ' {} : {}\n'.format(pname, pval)
doc += f' {pname} : {pval}\n'
# ... write optional param-vector-index line
if isinstance(pval, list):
labels = paramtools.consistent_labels(
Expand All @@ -1231,7 +1231,7 @@ def lines(text, num_indent_spaces, max_line_length=77):
]
doc += ' ' * (
4 + len(pname)
) + '{}\n'.format(lv)
) + f'{lv}\n'
# ... write param-name line
name = mdata_base[pname]['title']
for line in lines('name: ' + name, 6):
Expand All @@ -1249,7 +1249,7 @@ def lines(text, num_indent_spaces, max_line_length=77):
pval = [bool(item) for item in pval]
elif ptype == 'bool':
pval = bool(pval)
doc += ' baseline_value: {}\n'.format(pval)
doc += f' baseline_value: {pval}\n'
else: # if baseline is GrowDiff object
# each GrowDiff parameter has zero as default value
doc += ' baseline_value: 0.0\n'
Expand Down
8 changes: 4 additions & 4 deletions taxcalc/consumption.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ def has_response(self):
parameters are one
"""
for var in Consumption.RESPONSE_VARS:
if getattr(self, 'MPC_{}'.format(var)) > 0.0:
if getattr(self, f'MPC_{var}') > 0.0:
return True
for var in Consumption.BENEFIT_VARS:
if getattr(self, 'BEN_{}_value'.format(var)) < 1.0:
if getattr(self, f'BEN_{var}_value') < 1.0:
return True
return False

Expand All @@ -84,12 +84,12 @@ def response(self, records, income_change):
raise ValueError('records is not a Records object')
for var in Consumption.RESPONSE_VARS:
records_var = getattr(records, var)
mpc_var = getattr(self, 'MPC_{}'.format(var))
mpc_var = getattr(self, f'MPC_{var}')
records_var[:] += mpc_var * income_change

def benval_params(self):
"""
Returns list of BEN_*_value parameter values
"""
return [getattr(self, 'BEN_{}_value'.format(var))
return [getattr(self, f'BEN_{var}_value')
for var in Consumption.BENEFIT_VARS]
2 changes: 1 addition & 1 deletion taxcalc/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def ap_fuc(x_0, x_1, x_2, ...):
out_args = ["x_" + str(i) for i in range(0, len(sigout))]
in_args = ["x_" + str(i) for i in range(len(sigout), total_len)]

fstr.write("def ap_func({0}):\n".format(",".join(out_args + in_args)))
fstr.write(f"def ap_func({','.join(out_args + in_args)}):\n")
fstr.write(" for i in range(len(x_0)):\n")
out_index = [x + "[i]" for x in out_args]
in_index = []
Expand Down
71 changes: 37 additions & 34 deletions taxcalc/taxcalcio.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@ def __init__(self, input_data, tax_year, baseline, reform, assump,
fname = os.path.basename(input_data)
# check if fname ends with ".csv"
if fname.endswith('.csv'):
inp = '{}-{}'.format(fname[:-4], str(tax_year)[2:])
inp = f'{fname[:-4]}-{str(tax_year)[2:]}'
else:
msg = 'INPUT file name does not end in .csv'
self.errmsg += 'ERROR: {}\n'.format(msg)
self.errmsg += f'ERROR: {msg}\n'
# check existence of INPUT file
self.puf_input_data = input_data.endswith('puf.csv')
self.cps_input_data = input_data.endswith('cps.csv')
self.tmd_input_data = input_data.endswith('tmd.csv')
if not self.cps_input_data and not os.path.isfile(input_data):
msg = 'INPUT file could not be found'
self.errmsg += 'ERROR: {}\n'.format(msg)
self.errmsg += f'ERROR: {msg}\n'
# if tmd_input_data is True, construct weights and gfactor paths
if self.tmd_input_data: # pragma: no cover
tmd_dir = os.path.dirname(input_data)
Expand All @@ -106,15 +106,15 @@ def __init__(self, input_data, tax_year, baseline, reform, assump,
self.tmd_gfactor = os.path.join(tmd_dir, 'tmd_growfactors.csv')
if not os.path.isfile(self.tmd_weights):
msg = f'weights file {self.tmd_weights} could not be found'
self.errmsg += 'ERROR: {}\n'.format(msg)
self.errmsg += f'ERROR: {msg}\n'
if not os.path.isfile(self.tmd_gfactor):
msg = f'gfactor file {self.tmd_gfactor} could not be found'
self.errmsg += 'ERROR: {}\n'.format(msg)
self.errmsg += f'ERROR: {msg}\n'
elif isinstance(input_data, pd.DataFrame):
inp = 'df-{}'.format(str(tax_year)[2:])
inp = f'df-{str(tax_year)[2:]}'
else:
msg = 'INPUT is neither string nor Pandas DataFrame'
self.errmsg += 'ERROR: {}\n'.format(msg)
self.errmsg += f'ERROR: {msg}\n'
# check name and existence of BASELINE file
bas = '-x'
if baseline is None:
Expand All @@ -124,17 +124,17 @@ def __init__(self, input_data, tax_year, baseline, reform, assump,
fname = os.path.basename(baseline)
# check if fname ends with ".json"
if fname.endswith('.json'):
bas = '-{}'.format(fname[:-5])
bas = f'-{fname[:-5]}'
else:
msg = 'BASELINE file name does not end in .json'
self.errmsg += 'ERROR: {}\n'.format(msg)
self.errmsg += f'ERROR: {msg}\n'
# check existence of BASELINE file
if not os.path.isfile(baseline):
msg = 'BASELINE file could not be found'
self.errmsg += 'ERROR: {}\n'.format(msg)
self.errmsg += f'ERROR: {msg}\n'
else:
msg = 'TaxCalcIO.ctor: baseline is neither None nor str'
self.errmsg += 'ERROR: {}\n'.format(msg)
self.errmsg += f'ERROR: {msg}\n'
# check name(s) and existence of REFORM file(s)
ref = '-x'
if reform is None:
Expand All @@ -150,12 +150,12 @@ def __init__(self, input_data, tax_year, baseline, reform, assump,
fname = os.path.basename(rfm)
# check if fname ends with ".json"
if not fname.endswith('.json'):
msg = '{} does not end in .json'.format(fname)
self.errmsg += 'ERROR: REFORM file name {}\n'.format(msg)
msg = f'{fname} does not end in .json'
self.errmsg += f'ERROR: REFORM file name {msg}\n'
# check existence of REFORM file
if not os.path.isfile(rfm):
msg = '{} could not be found'.format(rfm)
self.errmsg += 'ERROR: REFORM file {}\n'.format(msg)
msg = f'{rfm} could not be found'
self.errmsg += f'ERROR: REFORM file {msg}\n'
# add fname to list of refnames used in output file names
refnames.append(fname)
# create (possibly compound) reform name for output file names
Expand All @@ -165,10 +165,10 @@ def __init__(self, input_data, tax_year, baseline, reform, assump,
num_refnames += 1
if num_refnames > 1:
ref += '+'
ref += '{}'.format(refname[:-5])
ref += f'{refname[:-5]}'
else:
msg = 'TaxCalcIO.ctor: reform is neither None nor str'
self.errmsg += 'ERROR: {}\n'.format(msg)
self.errmsg += f'ERROR: {msg}\n'
# check name and existence of ASSUMP file
asm = '-x'
if assump is None:
Expand All @@ -178,17 +178,17 @@ def __init__(self, input_data, tax_year, baseline, reform, assump,
fname = os.path.basename(assump)
# check if fname ends with ".json"
if fname.endswith('.json'):
asm = '-{}'.format(fname[:-5])
asm = f'-{fname[:-5]}'
else:
msg = 'ASSUMP file name does not end in .json'
self.errmsg += 'ERROR: {}\n'.format(msg)
self.errmsg += f'ERROR: {msg}\n'
# check existence of ASSUMP file
if not os.path.isfile(assump):
msg = 'ASSUMP file could not be found'
self.errmsg += 'ERROR: {}\n'.format(msg)
self.errmsg += f'ERROR: {msg}\n'
else:
msg = 'TaxCalcIO.ctor: assump is neither None nor str'
self.errmsg += 'ERROR: {}\n'.format(msg)
self.errmsg += f'ERROR: {msg}\n'
# check name and existence of OUTDIR
if outdir is None:
valid_outdir = True
Expand All @@ -199,13 +199,13 @@ def __init__(self, input_data, tax_year, baseline, reform, assump,
else:
valid_outdir = False
msg = 'OUTDIR could not be found'
self.errmsg += 'ERROR: {}\n'.format(msg)
self.errmsg += f'ERROR: {msg}\n'
else:
valid_outdir = False
msg = 'TaxCalcIO.ctor: outdir is neither None nor str'
self.errmsg += 'ERROR: {}\n'.format(msg)
self.errmsg += f'ERROR: {msg}\n'
# create OUTPUT file name and delete any existing output files
output_filename = '{}{}{}{}.csv'.format(inp, bas, ref, asm)
output_filename = f'{inp}{bas}{ref}{asm}.csv'
if outdir is None:
self._output_filename = output_filename
delete_old_files = True
Expand Down Expand Up @@ -431,9 +431,8 @@ def custom_dump_variables(self, tcdumpvars_str):
valid_set = recs_vinfo.USABLE_READ_VARS | recs_vinfo.CALCULATED_VARS
for var in dump_vars_list:
if var not in valid_set:
msg = 'invalid variable name in tcdumpvars file: {}'
msg = msg.format(var)
self.errmsg += 'ERROR: {}\n'.format(msg)
msg = f'invalid variable name in tcdumpvars file: {var}'
self.errmsg += f'ERROR: {msg}\n'
# add essential variables even if not on custom list
if 'RECID' not in dump_vars_list:
dump_vars_list.append('RECID')
Expand Down Expand Up @@ -687,8 +686,9 @@ def write_decile_table(dfx, tfile, tkind='Totals'):
weighted_sum, 'combined', include_groups=False
).values[:, 1]
# write decile table to text file
row = 'Weighted Tax {} by Baseline Expanded-Income Decile\n'
tfile.write(row.format(tkind))
row = f'Weighted Tax {tkind} by Baseline Expanded-Income Decile\n'
tfile.write(row)
# pylint: disable=consider-using-f-string
rowfmt = '{}{}{}{}{}{}\n'
row = rowfmt.format(' Returns',
' ExpInc',
Expand All @@ -706,7 +706,7 @@ def write_decile_table(dfx, tfile, tkind='Totals'):
tfile.write(row)
rowfmt = '{:9.2f}{:10.1f}{:10.1f}{:10.1f}{:10.1f}{:10.1f}\n'
for decile in range(0, 10):
row = '{:2d}'.format(decile)
row = f'{decile:2d}'
row += rowfmt.format(rtns_series[decile] * 1e-6,
xinc_series[decile] * 1e-9,
itax_series[decile] * 1e-9,
Expand All @@ -722,6 +722,7 @@ def write_decile_table(dfx, tfile, tkind='Totals'):
htax_series.sum() * 1e-9,
ctax_series.sum() * 1e-9)
tfile.write(row)
# pylint: enable=consider-using-f-string
del gdfx
del rtns_series
del xinc_series
Expand Down Expand Up @@ -778,10 +779,12 @@ def write_empty_graph_file(fname, title, reason):
"""
Write HTML graph file with title but no graph for specified reason.
"""
txt = ('<html>\n'
'<head><title>{}</title></head>\n'
'<body><center<h1>{}</h1></center></body>\n'
'</html>\n').format(title, reason)
txt = (
'<html>\n'
f'<head><title>{title}</title></head>\n'
f'<body><center<h1>{reason}</h1></center></body>\n'
'</html>\n'
)
with open(fname, 'w', encoding='utf-8') as gfile:
gfile.write(txt)

Expand Down
12 changes: 6 additions & 6 deletions taxcalc/tests/cmpi_cps_expect.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TABLE for EITC
AGI category T-C SOI %diff
AGIcategory T-C SOI %diff
[-9e+99, 1) 0.014 0.210 -93.2
[1, 5000) 1.555 1.184 +31.3
[5000, 10000) 6.132 7.156 -14.3
Expand All @@ -21,7 +21,7 @@ AGI category T-C SOI %diff
[10000000, 9e+99) 0.000 0.000 +nan
ALL 54.894 68.525 -19.9
TABLE for FCTC
AGI category T-C SOI %diff
AGIcategory T-C SOI %diff
[-9e+99, 1) 0.005 0.130 -96.0
[1, 5000) 0.083 0.079 +4.4
[5000, 10000) 1.118 1.474 -24.2
Expand All @@ -43,7 +43,7 @@ AGI category T-C SOI %diff
[10000000, 9e+99) 0.000 0.000 +nan
ALL 56.073 53.690 +4.4
TABLE for NIIT
AGI category T-C SOI %diff
AGIcategory T-C SOI %diff
[-9e+99, 1) 0.000 0.000 -100.0
[1, 5000) 0.000 0.000 +nan
[5000, 10000) 0.000 0.000 +nan
Expand All @@ -65,7 +65,7 @@ AGI category T-C SOI %diff
[10000000, 9e+99) 4.187 8.273 -49.4
ALL 6.179 22.043 -72.0
TABLE for ITAX
AGI category T-C SOI %diff
AGIcategory T-C SOI %diff
[-9e+99, 1) -0.006 0.242 -102.3
[1, 5000) -1.529 0.041 -3838.6
[5000, 10000) -6.862 0.368 -1964.8
Expand All @@ -87,7 +87,7 @@ AGI category T-C SOI %diff
[10000000, 9e+99) 49.547 139.611 -64.5
ALL 969.859 1457.891 -33.5
TABLE for SETAX
AGI category T-C SOI %diff
AGIcategory T-C SOI %diff
[-9e+99, 1) 0.014 0.656 -97.9
[1, 5000) 0.101 0.555 -81.8
[5000, 10000) 0.319 1.896 -83.2
Expand All @@ -109,7 +109,7 @@ AGI category T-C SOI %diff
[10000000, 9e+99) 0.073 0.638 -88.6
ALL 48.503 60.174 -19.4
TABLE for AMTAX
AGI category T-C SOI %diff
AGIcategory T-C SOI %diff
[-9e+99, 1) 0.000 0.022 -100.0
[1, 5000) 0.000 0.000 -100.0
[5000, 10000) 0.000 0.000 +nan
Expand Down
12 changes: 6 additions & 6 deletions taxcalc/tests/cmpi_puf_expect.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TABLE for EITC
AGI category T-C SOI %diff
AGIcategory T-C SOI %diff
[-9e+99, 1) 0.174 0.210 -17.5
[1, 5000) 1.495 1.184 +26.2
[5000, 10000) 5.880 7.156 -17.8
Expand All @@ -21,7 +21,7 @@ AGI category T-C SOI %diff
[10000000, 9e+99) 0.000 0.000 +nan
ALL 68.057 68.525 -0.7
TABLE for FCTC
AGI category T-C SOI %diff
AGIcategory T-C SOI %diff
[-9e+99, 1) 0.145 0.130 +11.2
[1, 5000) 0.115 0.079 +44.7
[5000, 10000) 1.084 1.474 -26.5
Expand All @@ -43,7 +43,7 @@ AGI category T-C SOI %diff
[10000000, 9e+99) 0.000 0.000 +nan
ALL 53.276 53.690 -0.8
TABLE for NIIT
AGI category T-C SOI %diff
AGIcategory T-C SOI %diff
[-9e+99, 1) 0.000 0.000 -100.0
[1, 5000) 0.000 0.000 +nan
[5000, 10000) 0.000 0.000 +nan
Expand All @@ -65,7 +65,7 @@ AGI category T-C SOI %diff
[10000000, 9e+99) 8.112 8.273 -1.9
ALL 27.149 22.043 +23.2
TABLE for ITAX
AGI category T-C SOI %diff
AGIcategory T-C SOI %diff
[-9e+99, 1) 4.502 0.242 +1756.7
[1, 5000) -1.776 0.041 -4442.6
[5000, 10000) -5.779 0.368 -1670.5
Expand All @@ -87,7 +87,7 @@ AGI category T-C SOI %diff
[10000000, 9e+99) 97.259 139.611 -30.3
ALL 1462.385 1457.891 +0.3
TABLE for SETAX
AGI category T-C SOI %diff
AGIcategory T-C SOI %diff
[-9e+99, 1) 0.761 0.656 +16.1
[1, 5000) 0.626 0.555 +12.7
[5000, 10000) 1.633 1.896 -13.8
Expand All @@ -109,7 +109,7 @@ AGI category T-C SOI %diff
[10000000, 9e+99) 0.033 0.638 -94.8
ALL 49.779 60.174 -17.3
TABLE for AMTAX
AGI category T-C SOI %diff
AGIcategory T-C SOI %diff
[-9e+99, 1) 0.012 0.022 -45.5
[1, 5000) 0.000 0.000 -68.2
[5000, 10000) 0.000 0.000 +nan
Expand Down
2 changes: 1 addition & 1 deletion taxcalc/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def fixture_test_reforms(tests_path):
expect_lines = efile.readlines()
# ... compare actual and expected results for each test
diffs = False
with open(actfile_path, 'w') as actfile:
with open(actfile_path, 'w', encoding='utf-8') as actfile:
actfile.write('rid,res1,res2,res3,res4\n')
idx = 1 # expect_lines list index
for rnum in range(1, num_reforms + 1):
Expand Down
2 changes: 1 addition & 1 deletion taxcalc/tests/test_benefits.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_benefits(tests_path, cps_fullsample):
# (head counts include all members of filing unit receiving a benefit,
# which means benavg is f.unit benefit amount divided by f.unit size)
for bname in benefit_names:
ben = calc.array('{}_ben'.format(bname))
ben = calc.array(f'{bname}_ben')
benamt = round((ben * wght).sum() * 1e-9, 3)
bencnt = round((size[ben > 0] * wght[ben > 0]).sum() * 1e-6, 3)
benavg = round(benamt / bencnt, 1)
Expand Down
Loading

0 comments on commit 578eb6d

Please sign in to comment.