From 3fe7aeb97137d28d874f4dd1f18114c2c79767ed Mon Sep 17 00:00:00 2001 From: Martin Holmer Date: Fri, 7 Sep 2018 13:06:22 -0400 Subject: [PATCH] Remove use of six Py2-Py3-compatability package --- taxcalc/calculate.py | 12 ++++++------ taxcalc/decorators.py | 6 +++--- taxcalc/growfactors.py | 3 +-- taxcalc/parameters.py | 3 +-- taxcalc/policy.py | 3 +-- taxcalc/records.py | 7 +++---- taxcalc/simpletaxio.py | 5 ++--- taxcalc/taxcalcio.py | 13 ++++++------- taxcalc/tests/test_calculate.py | 3 +-- taxcalc/tests/test_compatible_data.py | 9 ++++----- taxcalc/tests/test_decorators.py | 4 ++-- taxcalc/tests/test_functions.py | 6 +----- taxcalc/tests/test_parameters.py | 11 +++++------ taxcalc/tests/test_policy.py | 3 +-- taxcalc/tests/test_taxcalcio.py | 2 +- taxcalc/utils.py | 5 ++--- 16 files changed, 40 insertions(+), 55 deletions(-) diff --git a/taxcalc/calculate.py b/taxcalc/calculate.py index accf8dc80..f1551a190 100644 --- a/taxcalc/calculate.py +++ b/taxcalc/calculate.py @@ -12,7 +12,7 @@ import json import re import copy -import six + import numpy as np import pandas as pd from taxcalc.functions import (TaxInc, SchXYZTax, GainsTax, AGIsurtax, @@ -1133,7 +1133,7 @@ def decile_graph(self, calc, def read_json_param_objects(reform, assump): """ Read JSON reform and assump objects and - return a single dictionary containing six key:dict pairs: + return a single dictionary containing 6 key:dict pairs: 'policy':dict, 'consumption':dict, 'behavior':dict, 'growdiff_baseline':dict, 'growdiff_response':dict, and 'growmodel':dict. @@ -1168,7 +1168,7 @@ def read_json_param_objects(reform, assump): gdiff_base_dict = dict() gdiff_resp_dict = dict() growmodel_dict = dict() - elif isinstance(assump, six.string_types): + elif isinstance(assump, str): if os.path.isfile(assump): txt = open(assump, 'r').read() else: @@ -1183,7 +1183,7 @@ def read_json_param_objects(reform, assump): # next process first reform parameter if reform is None: rpol_dict = dict() - elif isinstance(reform, six.string_types): + elif isinstance(reform, str): if os.path.isfile(reform): txt = open(reform, 'r').read() else: @@ -1653,7 +1653,7 @@ def _convert_parameter_dict(param_key_dict): # optionally convert lists into np.arrays year_param = dict() for pkey, sdict in param_key_dict.items(): - if not isinstance(pkey, six.string_types): + if not isinstance(pkey, str): msg = 'pkey {} in reform is not a string' raise ValueError(msg.format(pkey)) rdict = dict() @@ -1661,7 +1661,7 @@ def _convert_parameter_dict(param_key_dict): msg = 'pkey {} in reform is not paired with a dict' raise ValueError(msg.format(pkey)) for skey, val in sdict.items(): - if not isinstance(skey, six.string_types): + if not isinstance(skey, str): msg = 'skey {} in reform is not a string' raise ValueError(msg.format(skey)) else: diff --git a/taxcalc/decorators.py b/taxcalc/decorators.py index e27b78f1a..36599e638 100644 --- a/taxcalc/decorators.py +++ b/taxcalc/decorators.py @@ -7,10 +7,10 @@ # pylint --disable=locally-disabled decorators.py import sys +import io import ast import inspect import toolz -from six import StringIO from taxcalc.policy import Policy @@ -84,7 +84,7 @@ def ap_fuc(x_0, x_1, x_2, ...): ------- a String representing the function """ - fstr = StringIO() + fstr = io.StringIO() total_len = len(sigout) + len(sigin) 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)] @@ -122,7 +122,7 @@ def hl_func(x_0, x_1, x_2, ...): ------- a String representing the function """ - fstr = StringIO() + fstr = io.StringIO() fstr.write("def hl_func(pm, pf") fstr.write("):\n") fstr.write(" from pandas import DataFrame\n") diff --git a/taxcalc/growfactors.py b/taxcalc/growfactors.py index b4204c4bc..d63307f8b 100644 --- a/taxcalc/growfactors.py +++ b/taxcalc/growfactors.py @@ -6,7 +6,6 @@ # pylint --disable=locally-disabled growfactors.py import os -import six import numpy as np import pandas as pd from taxcalc.utils import read_egg_csv @@ -53,7 +52,7 @@ class instance: GrowFactors def __init__(self, growfactors_filename=FILE_PATH): # read grow factors from specified growfactors_filename gfdf = pd.DataFrame() - if isinstance(growfactors_filename, six.string_types): + if isinstance(growfactors_filename, str): if os.path.isfile(growfactors_filename): gfdf = pd.read_csv(growfactors_filename, index_col='YEAR') diff --git a/taxcalc/parameters.py b/taxcalc/parameters.py index af07c30ef..64a7d0b6e 100644 --- a/taxcalc/parameters.py +++ b/taxcalc/parameters.py @@ -6,7 +6,6 @@ import os import json -import six import abc import collections as collect import numpy as np @@ -173,7 +172,7 @@ def set_year(self, year): year_zero_indexed = year - self._start_year if hasattr(self, '_vals'): for name in self._vals: - if isinstance(name, six.string_types): + if isinstance(name, str): arr = getattr(self, name) setattr(self, name[1:], arr[year_zero_indexed]) diff --git a/taxcalc/policy.py b/taxcalc/policy.py index fb00186e1..7d8ba3e7b 100644 --- a/taxcalc/policy.py +++ b/taxcalc/policy.py @@ -5,7 +5,6 @@ # pycodestyle policy.py # pylint --disable=locally-disabled policy.py -import six import numpy as np from taxcalc.parameters import ParametersBase from taxcalc.growfactors import GrowFactors @@ -511,7 +510,7 @@ def _validate_parameter_values(self, parameters_set): continue # *_cpi parameter values validated elsewhere pvalue = getattr(self, pname) for vop, vval in self._vals[pname]['range'].items(): - if isinstance(vval, six.string_types): + if isinstance(vval, str): vvalue = getattr(self, vval) else: vvalue = np.full(pvalue.shape, vval) diff --git a/taxcalc/records.py b/taxcalc/records.py index bc5d8364a..089d6816b 100644 --- a/taxcalc/records.py +++ b/taxcalc/records.py @@ -7,7 +7,6 @@ import os import json -import six import numpy as np import pandas as pd from taxcalc.growfactors import GrowFactors @@ -423,7 +422,7 @@ def _read_data(self, data, exact_calcs): # read specified data if isinstance(data, pd.DataFrame): taxdf = data - elif isinstance(data, six.string_types): + elif isinstance(data, str): if os.path.isfile(data): taxdf = pd.read_csv(data) else: @@ -503,7 +502,7 @@ def _read_weights(self, weights): return if isinstance(weights, pd.DataFrame): WT = weights - elif isinstance(weights, six.string_types): + elif isinstance(weights, str): weights_path = os.path.join(Records.CUR_PATH, weights) if os.path.isfile(weights_path): WT = pd.read_csv(weights_path) @@ -526,7 +525,7 @@ def _read_ratios(self, ratios): if ratios is None: setattr(self, 'ADJ', pd.DataFrame({'nothing': []})) return - if isinstance(ratios, six.string_types): + if isinstance(ratios, str): ratios_path = os.path.join(Records.CUR_PATH, ratios) if os.path.isfile(ratios_path): ADJ = pd.read_csv(ratios_path, diff --git a/taxcalc/simpletaxio.py b/taxcalc/simpletaxio.py index 0e901a2e0..1d80831cf 100644 --- a/taxcalc/simpletaxio.py +++ b/taxcalc/simpletaxio.py @@ -8,7 +8,6 @@ import os import sys import re -import six import pandas as pd from taxcalc.policy import Policy from taxcalc.records import Records @@ -67,7 +66,7 @@ def __init__(self, """ # pylint: disable=too-many-arguments # check that input_filename is a string - if not isinstance(input_filename, six.string_types): + if not isinstance(input_filename, str): msg = 'SimpleTaxIO.ctor input_filename is not a string' raise ValueError(msg) # construct output_filename and delete old output file if it exists @@ -76,7 +75,7 @@ def __init__(self, ref = '' self._using_reform_file = True else: # if reform is not None - if isinstance(reform, six.string_types): + if isinstance(reform, str): if reform.endswith('.json'): ref = '-{}'.format(reform[:-5]) else: diff --git a/taxcalc/taxcalcio.py b/taxcalc/taxcalcio.py index 3ac9850e9..1ffa4c272 100644 --- a/taxcalc/taxcalcio.py +++ b/taxcalc/taxcalcio.py @@ -9,7 +9,6 @@ import gc import copy import sqlite3 -import six import numpy as np import pandas as pd from taxcalc.policy import Policy @@ -75,7 +74,7 @@ def __init__(self, input_data, tax_year, baseline, reform, assump, inp = 'x' self.puf_input_data = False self.cps_input_data = False - if isinstance(input_data, six.string_types): + if isinstance(input_data, str): # remove any leading directory path from INPUT filename fname = os.path.basename(input_data) # check if fname ends with ".csv" @@ -99,7 +98,7 @@ def __init__(self, input_data, tax_year, baseline, reform, assump, bas = '-x' if baseline is None: bas = '-#' - elif isinstance(baseline, six.string_types): + elif isinstance(baseline, str): # remove any leading directory path from BASELINE filename fname = os.path.basename(baseline) # check if fname ends with ".json" @@ -120,7 +119,7 @@ def __init__(self, input_data, tax_year, baseline, reform, assump, if reform is None: self.specified_reform = False ref = '-#' - elif isinstance(reform, six.string_types): + elif isinstance(reform, str): self.specified_reform = True # split any compound reform into list of simple reforms refnames = list() @@ -153,7 +152,7 @@ def __init__(self, input_data, tax_year, baseline, reform, assump, asm = '-x' if assump is None: asm = '-#' - elif isinstance(assump, six.string_types): + elif isinstance(assump, str): # remove any leading directory path from ASSUMP filename fname = os.path.basename(assump) # check if fname ends with ".json" @@ -172,7 +171,7 @@ def __init__(self, input_data, tax_year, baseline, reform, assump, # check name and existence of OUTDIR if outdir is None: valid_outdir = True - elif isinstance(outdir, six.string_types): + elif isinstance(outdir, str): # check existence of OUTDIR if os.path.isdir(outdir): valid_outdir = True @@ -381,7 +380,7 @@ def custom_dump_variables(self, tcdumpvars_str): contains the contents of the tcdumpvars file in the current directory. Also, builds self.errmsg if any custom variables are not valid. """ - assert isinstance(tcdumpvars_str, six.string_types) + assert isinstance(tcdumpvars_str, str) self.errmsg = '' # change some common delimiter characters into spaces dump_vars_str = tcdumpvars_str.replace(',', ' ') diff --git a/taxcalc/tests/test_calculate.py b/taxcalc/tests/test_calculate.py index e64e3c60d..1fd2a6f62 100644 --- a/taxcalc/tests/test_calculate.py +++ b/taxcalc/tests/test_calculate.py @@ -6,7 +6,6 @@ from io import StringIO import tempfile import copy -import six import pytest import numpy as np import pandas as pd @@ -886,7 +885,7 @@ def test_reform_documentation(): params = Calculator.read_json_param_objects(reform_json, assump_json) assert isinstance(params, dict) doc = Calculator.reform_documentation(params) - assert isinstance(doc, six.string_types) + assert isinstance(doc, str) dump = False # set to True to print documentation and force test failure if dump: print(doc) diff --git a/taxcalc/tests/test_compatible_data.py b/taxcalc/tests/test_compatible_data.py index 8983882aa..1ef11eec2 100644 --- a/taxcalc/tests/test_compatible_data.py +++ b/taxcalc/tests/test_compatible_data.py @@ -12,7 +12,6 @@ import copy import pytest import numpy as np -import six from taxcalc import Policy, Records, Calculator # pylint: disable=import-error @@ -247,23 +246,23 @@ def test_compatible_data(cps_subsample, puf_subsample, param = allparams_batch[pname] max_listed = param['range']['max'] # handle links to other params or self - if isinstance(max_listed, six.string_types): + if isinstance(max_listed, str): if max_listed == 'default': max_val = param['value'][-1] else: max_val = allparams[max_listed]['value'][0] - if not isinstance(max_listed, six.string_types): + if not isinstance(max_listed, str): if isinstance(param['value'][0], list): max_val = [max_listed] * len(param['value'][0]) else: max_val = max_listed min_listed = param['range']['min'] - if isinstance(min_listed, six.string_types): + if isinstance(min_listed, str): if min_listed == 'default': min_val = param['value'][-1] else: min_val = allparams[min_listed]['value'][0] - if not isinstance(min_listed, six.string_types): + if not isinstance(min_listed, str): if isinstance(param['value'][0], list): min_val = [min_listed] * len(param['value'][0]) else: diff --git a/taxcalc/tests/test_decorators.py b/taxcalc/tests/test_decorators.py index d1378c93e..f5e2ca76d 100644 --- a/taxcalc/tests/test_decorators.py +++ b/taxcalc/tests/test_decorators.py @@ -3,7 +3,7 @@ import sys import pytest -from six.moves import reload_module +import importlib import numpy as np from pandas import DataFrame from taxcalc.decorators import * @@ -317,7 +317,7 @@ def test_force_no_numba(): nmba = sys.modules.get('numba', None) sys.modules.update([('numba', mck)]) # Reload the decorators with faked out numba - reload_module(taxcalc.decorators) + importlib.reload(taxcalc.decorators) # Get access to iterate_jit and force to jit ij = taxcalc.decorators.iterate_jit taxcalc.decorators.DO_JIT = True diff --git a/taxcalc/tests/test_functions.py b/taxcalc/tests/test_functions.py index e07048318..7631a7c54 100644 --- a/taxcalc/tests/test_functions.py +++ b/taxcalc/tests/test_functions.py @@ -8,7 +8,6 @@ import os import re import ast -import six from taxcalc import Records # pylint: disable=import-error @@ -35,10 +34,7 @@ def visit_FunctionDef(self, node): # pylint: disable=invalid-name self.fnames.append(self.fname) self.fargs[self.fname] = list() for anode in ast.iter_child_nodes(node.args): - if six.PY3: - self.fargs[self.fname].append(anode.arg) - else: # in Python 2 anode is a Name node - self.fargs[self.fname].append(anode.id) + self.fargs[self.fname].append(anode.arg) self.cvars[self.fname] = list() for bodynode in node.body: if isinstance(bodynode, ast.Return): diff --git a/taxcalc/tests/test_parameters.py b/taxcalc/tests/test_parameters.py index e900d2f6f..48aea7914 100644 --- a/taxcalc/tests/test_parameters.py +++ b/taxcalc/tests/test_parameters.py @@ -8,7 +8,6 @@ import os import json import math -import six import numpy as np import pytest # pylint: disable=import-error @@ -87,17 +86,17 @@ def test_json_file_contents(tests_path, fname): failures = '' for pname in allparams: # all parameter names should be strings - assert isinstance(pname, six.string_types) + assert isinstance(pname, str) # check that param contains required keys param = allparams[pname] assert isinstance(param, dict) for key in reqkeys: assert key in param # check for non-empty long_name and description strings - assert isinstance(param['long_name'], six.string_types) + assert isinstance(param['long_name'], str) if not param['long_name']: assert '{} long_name'.format(pname) == 'empty string' - assert isinstance(param['description'], six.string_types) + assert isinstance(param['description'], str) if not param['description']: assert '{} description'.format(pname) == 'empty string' # check that row_var is FLPDYR @@ -123,10 +122,10 @@ def test_json_file_contents(tests_path, fname): assert len(value) == len(rowlabel) # check that col_var and col_label are consistent cvar = param['col_var'] - assert isinstance(cvar, six.string_types) + assert isinstance(cvar, str) clab = param['col_label'] if cvar == '': - assert isinstance(clab, six.string_types) and clab == '' + assert isinstance(clab, str) and clab == '' else: assert isinstance(clab, list) # check different possible col_var values diff --git a/taxcalc/tests/test_policy.py b/taxcalc/tests/test_policy.py index 076422d70..4b7567457 100644 --- a/taxcalc/tests/test_policy.py +++ b/taxcalc/tests/test_policy.py @@ -3,7 +3,6 @@ import os import sys -import six import json import tempfile import numpy as np @@ -847,7 +846,7 @@ def test_range_infomation(tests_path): assert len(range_items) == 2 for vop, vval in range_items: assert vop in min_max_list - if isinstance(vval, six.string_types): + if isinstance(vval, str): if vval == 'default': if vop != 'min' or oor_action != 'warn': msg = 'USES DEFAULT FOR min OR FOR error' diff --git a/taxcalc/tests/test_taxcalcio.py b/taxcalc/tests/test_taxcalcio.py index ed81bfb9d..e3ece7de2 100644 --- a/taxcalc/tests/test_taxcalcio.py +++ b/taxcalc/tests/test_taxcalcio.py @@ -397,7 +397,7 @@ def test_ctor_init_with_cps_files(): MARS;iitax payrolltax|combined, c00100 surtax - """, True, 8), # these six plus added RECID and FLPDYR + """, True, 8), # these 6 parameters plus added RECID and FLPDYR (""" MARS;iitax payrolltax|kombined,c00100 diff --git a/taxcalc/utils.py b/taxcalc/utils.py index aaa213ec0..a82d52ef3 100644 --- a/taxcalc/utils.py +++ b/taxcalc/utils.py @@ -12,7 +12,6 @@ import json import collections import pkg_resources -import six import numpy as np import pandas as pd import bokeh.io as bio @@ -735,7 +734,7 @@ def mtr_graph_data(vdf, year, '"wages", "agi", nor "expanded_income"') raise ValueError(msg.format(income_measure)) # . . check mars value - if isinstance(mars, six.string_types): + if isinstance(mars, str): if mars != 'ALL': msg = 'string value of mars="{}" is not "ALL"' raise ValueError(msg.format(mars)) @@ -843,7 +842,7 @@ def atr_graph_data(vdf, year, # pylint: disable=too-many-locals,too-many-statements # check validity of function arguments # . . check mars value - if isinstance(mars, six.string_types): + if isinstance(mars, str): if mars != 'ALL': msg = 'string value of mars="{}" is not "ALL"' raise ValueError(msg.format(mars))