Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor improvements to type checking and error checks in stats.py. #1493

Merged
merged 2 commits into from
Jan 10, 2021

Conversation

obi1kenobi
Copy link
Contributor

Description

  • Added a missing error check on the summary() function's kind parameter. Since the check was missing, that meant that illegal kind values could be passed, and cause unexpected, self-inconsistent behavior in the function.
  • Improved type hints to use explicit Literal types instead of str, making errors like the ones mentioned in the prior bullet point less likely to happen as mypy would catch them statically.
  • Resolved a type error caused by unintentional variable-sharing across unrelated portions of the same function.

As with my other PRs, there are no substantive changes to existing functionality and there is no new functionality, so existing tests should cover everything already.

Checklist

  • Follows official PR format
  • Code style correct (follows pylint and black guidelines)

Add a missing error check on summary()'s kind parameter, improve type hints to use explicit Literal types instead of str, and resolve a type error caused by unintentional variable-sharing across unrelated function blocks.
@@ -1322,12 +1329,12 @@ def summary(
dfs = []
for var_name, values in joined.data_vars.items():
if len(values.shape[1:]):
metric = list(values.metric.values)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This metric assignment clashed with the metric assignment on line 1307 in the original file: for metric, circ_stat in zip(...). This was a type error since the two assignments to metric had different types.

Renaming this metric to index_metric resolves the problem since now the differently-typed values are assigned to different variables.

@@ -1302,7 +1310,6 @@ def summary(
metric_names.extend(metrics_names_)

if circ_var_names:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was extraneous whitespace that did not seem to match the style of the surrounding code, so I removed it — hope you don't mind!

if extend:
metrics_names_ = (
metrics_: Tuple[xr.Dataset, ...]
metrics_names_: Tuple[str, ...] = (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The block below is why I noticed that the kind error check was missing, and added it: if kind is not one of "all", "stats", "diagnostics", then metrics_names_ will remain set to the tuple of strings, but metrics_ will never get assigned — even though it's about to get used at the end of the if-elif-elif block. This would have led to a user-unfriendly UnboundLocalError.

With the error check I added, supplying an illegal kind value is treated the same way as supplying an illegal order or fmt value: with a TypeError containing a descriptive and actionable error message.

@codecov
Copy link

codecov bot commented Jan 10, 2021

Codecov Report

Merging #1493 (87ba98f) into master (910e781) will decrease coverage by 0.00%.
The diff coverage is 90.90%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1493      +/-   ##
==========================================
- Coverage   91.97%   91.96%   -0.01%     
==========================================
  Files         105      105              
  Lines       11239    11244       +5     
==========================================
+ Hits        10337    10341       +4     
- Misses        902      903       +1     
Impacted Files Coverage Δ
arviz/stats/stats.py 96.27% <90.90%> (-0.14%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 910e781...87ba98f. Read the comment docs.

@OriolAbril
Copy link
Member

LGTM, I have added an extra test and will merge if it passes to try and release this week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants