Skip to content

Commit

Permalink
add pygrep-hooks and flake8-pytest-style Ruff linting (#214)
Browse files Browse the repository at this point in the history
* remove useless pragmas

* enable pytest style checks
  • Loading branch information
rpreen authored Jun 3, 2024
1 parent 8dddcaf commit 6f13c06
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
32 changes: 16 additions & 16 deletions acro/acro_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def crosstab( # pylint: disable=too-many-arguments,too-many-locals
agg_func = get_aggfuncs(aggfunc)

# requested table
table: DataFrame = pd.crosstab( # type: ignore
table: DataFrame = pd.crosstab(
index,
columns,
values,
Expand Down Expand Up @@ -321,33 +321,33 @@ def pivot_table( # pylint: disable=too-many-arguments,too-many-locals

# threshold check
agg = [agg_threshold] * n_agg if n_agg > 1 else agg_threshold
t_values = pd.pivot_table( # type: ignore
t_values = pd.pivot_table(
data, values, index, columns, aggfunc=agg, margins=margins
)
masks["threshold"] = t_values

if aggfunc is not None:
# check for negative values -- currently unsupported
agg = [agg_negative] * n_agg if n_agg > 1 else agg_negative
negative = pd.pivot_table( # type: ignore
negative = pd.pivot_table(
data, values, index, columns, aggfunc=agg, margins=margins
)
if negative.to_numpy().sum() > 0:
masks["negative"] = negative
# p-percent check
agg = [agg_p_percent] * n_agg if n_agg > 1 else agg_p_percent
masks["p-ratio"] = pd.pivot_table( # type: ignore
masks["p-ratio"] = pd.pivot_table(
data, values, index, columns, aggfunc=agg, margins=margins
)
# nk values check
agg = [agg_nk] * n_agg if n_agg > 1 else agg_nk
masks["nk-rule"] = pd.pivot_table( # type: ignore
masks["nk-rule"] = pd.pivot_table(
data, values, index, columns, aggfunc=agg, margins=margins
)
# check for missing values -- currently unsupported
if CHECK_MISSING_VALUES:
agg = [agg_missing] * n_agg if n_agg > 1 else agg_missing
masks["missing"] = pd.pivot_table( # type: ignore
masks["missing"] = pd.pivot_table(
data, values, index, columns, aggfunc=agg, margins=margins
)

Expand Down Expand Up @@ -782,7 +782,7 @@ def create_crosstab_masks( # pylint: disable=too-many-arguments,too-many-locals
logger.info(
"If there are multiple modes, one of them is randomly selected and displayed."
)
masks["all-values-are-same"] = pd.crosstab( # type: ignore
masks["all-values-are-same"] = pd.crosstab(
index,
columns,
values,
Expand All @@ -791,7 +791,7 @@ def create_crosstab_masks( # pylint: disable=too-many-arguments,too-many-locals
dropna=dropna,
)
else:
t_values = pd.crosstab( # type: ignore
t_values = pd.crosstab(
index,
columns,
values=values,
Expand All @@ -815,13 +815,13 @@ def create_crosstab_masks( # pylint: disable=too-many-arguments,too-many-locals
t_values = t_values < THRESHOLD
masks["threshold"] = t_values
# check for negative values -- currently unsupported
negative = pd.crosstab( # type: ignore
negative = pd.crosstab(
index, columns, values, aggfunc=neg_funcs, margins=margins
)
if negative.to_numpy().sum() > 0:
masks["negative"] = negative
# p-percent check
masks["p-ratio"] = pd.crosstab( # type: ignore
masks["p-ratio"] = pd.crosstab(
index,
columns,
values,
Expand All @@ -830,17 +830,17 @@ def create_crosstab_masks( # pylint: disable=too-many-arguments,too-many-locals
dropna=dropna,
)
# nk values check
masks["nk-rule"] = pd.crosstab( # type: ignore
masks["nk-rule"] = pd.crosstab(
index, columns, values, aggfunc=nk_funcs, margins=margins, dropna=dropna
)
# check for missing values -- currently unsupported
if CHECK_MISSING_VALUES:
masks["missing"] = pd.crosstab( # type: ignore
masks["missing"] = pd.crosstab(
index, columns, values, aggfunc=missing_funcs, margins=margins
)
else:
# threshold check- doesn't matter what we pass for value
t_values = pd.crosstab( # type: ignore
t_values = pd.crosstab(
index,
columns,
values=None,
Expand Down Expand Up @@ -1499,7 +1499,7 @@ def crosstab_with_totals( # pylint: disable=too-many-arguments,too-many-locals
if crosstab:
index_new, columns_new = get_index_columns(index, columns, data)
# apply the crosstab with the new index and columns
table = pd.crosstab( # type: ignore
table = pd.crosstab(
index_new,
columns_new,
values=values,
Expand Down Expand Up @@ -1534,7 +1534,7 @@ def crosstab_with_totals( # pylint: disable=too-many-arguments,too-many-locals
logger.setLevel(previous_level)

else:
table = pd.pivot_table( # type: ignore
table = pd.pivot_table(
data=data,
values=values,
index=index,
Expand Down Expand Up @@ -1618,7 +1618,7 @@ def manual_crossstab_with_totals( # pylint: disable=too-many-arguments
table = recalculate_margin(table, margins_name)

elif aggfunc == "mean":
count_table = pd.crosstab( # type: ignore
count_table = pd.crosstab(
index=index,
columns=columns,
values=values,
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ lint.select = [
"ICN", # flake8-import-conventions
"N", # pep8-naming
# "PD", # pandas-vet
# "PGH", # pygrep-hooks
"PGH", # pygrep-hooks
"PIE", # flake8-pie
# "PL", # Pylint
"PLC", # Pylint
"PLE", # Pylint
# "PLR", # Pylint
# "PLW", # Pylint
# "PT", # flake8-pytest-style
"PT", # flake8-pytest-style
"Q", # flake8-quotes
# "RET", # flake8-return
"RUF100", # Ruff-specific
Expand Down
22 changes: 11 additions & 11 deletions test/test_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
PATH: str = "RES_PYTEST"


@pytest.fixture
@pytest.fixture()
def data() -> pd.DataFrame:
"""Load test data."""
path = os.path.join("data", "test_data.dta")
data = pd.read_stata(path)
return data


@pytest.fixture
@pytest.fixture()
def acro() -> ACRO:
"""Initialise ACRO."""
return ACRO(suppress=True)
Expand Down Expand Up @@ -334,14 +334,14 @@ def test_output_removal(data, acro, monkeypatch):
assert output_1.summary == correct_summary
acro.print_outputs()
# remove something that is not there
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="unable to remove 123, key not found"):
acro.remove_output("123")
shutil.rmtree(PATH)


def test_load_output():
"""Empty array when loading output."""
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="error loading output"):
record.load_output(PATH, [])


Expand All @@ -350,7 +350,7 @@ def test_finalise_invalid(data, acro):
_ = acro.crosstab(data.year, data.grant_type)
output_0 = acro.results.get_index(0)
output_0.exception = "Let me have it"
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Invalid file extension.*"):
_ = acro.finalise(PATH, "123")


Expand Down Expand Up @@ -405,10 +405,10 @@ def test_rename_output(data, acro):
assert orig_name not in results.get_keys()
assert os.path.exists(f"{PATH}/{new_name}_0.csv")
# rename an output that doesn't exist
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="unable to rename 123, key not found"):
acro.rename_output("123", "name")
# rename an output to another that already exists
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="unable to rename, cross_table .* exists"):
acro.rename_output("output_1", "cross_table")
shutil.rmtree(PATH)

Expand All @@ -427,7 +427,7 @@ def test_add_comments(data, acro):
acro.add_comments(output_0.uid, comment_1)
assert output_0.comments == [comment, comment_1]
# add a comment to something that is not there
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="unable to find 123, key not found"):
acro.add_comments("123", "comment")
shutil.rmtree(PATH)

Expand Down Expand Up @@ -480,7 +480,7 @@ def test_suppression_error(caplog):

def test_adding_exception(acro):
"""Adding an exception to an output that doesn't exist test."""
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="unable to add exception: output_0 .*"):
acro.add_exception("output_0", "Let me have it")


Expand Down Expand Up @@ -597,14 +597,14 @@ def test_hierachical_aggregation(data, acro):

def test_single_values_column(data, acro):
"""Pandas does not allows multiple arrays for values."""
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=".*specify a single values column.*"):
_ = acro.crosstab(
data.year,
data.grant_type,
values=[data.inc_activity, data.inc_activity],
aggfunc="mean",
)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=".*specify a single values column.*"):
_ = acro.crosstab(data.year, data.grant_type, values=None, aggfunc="mean")


Expand Down
2 changes: 1 addition & 1 deletion test/test_stata17_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# return ACRO()


@pytest.fixture
@pytest.fixture()
def data() -> pd.DataFrame:
"""Load test data."""
path = os.path.join("data", "test_data.dta")
Expand Down
2 changes: 1 addition & 1 deletion test/test_stata_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# return ACRO()


@pytest.fixture
@pytest.fixture()
def data() -> pd.DataFrame:
"""Load test data."""
path = os.path.join("data", "test_data.dta")
Expand Down

0 comments on commit 6f13c06

Please sign in to comment.