Skip to content

Commit 422e4ce

Browse files
authored
Give RuntimeWarning if stored function calls use pandas object arguments (#310)
1 parent a040f1d commit 422e4ce

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- [#311](https://github.com/equinor/webviz-config/pull/311) - Automatically add a comment
1111
in generated application regarding which Python executable (`sys.executable`) was used
1212
when building a portable application.
13+
- [#310](https://github.com/equinor/webviz-config/pull/310) - Added `RuntimeWarning`
14+
which appears if `@webvizstore` decorated functions are given argument values of type
15+
`pandas.DataFrame` or `pandas.Series` (Which are known to not have `__repr__` functions
16+
useful for hashing).
1317

1418
### Fixed
1519
- [#313](https://github.com/equinor/webviz-config/pull/313) - Added `min-width` to menu CSS

webviz_config/webviz_store.py

+19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import hashlib
66
import inspect
77
import pathlib
8+
import warnings
89
from collections import defaultdict
910
from typing import Callable, List, Union, Any
1011

@@ -72,6 +73,24 @@ def register_function_arguments(self, functionarguments: List[tuple]) -> None:
7273
repr(argtuples)
7374
] = argtuples
7475

76+
for (argname, argvalue) in argtuples:
77+
# Check that values of decorated functions are not pandas objects.
78+
# This function could probably at some point be moved into a pytest
79+
# fixture (given that plugins use type hints) in order to slightly
80+
# reduce running time.
81+
if isinstance(argvalue, (pd.DataFrame, pd.Series)):
82+
warnings.warn(
83+
f"{func.__module__}.{func.__name__} is a @webvizstore decorated "
84+
f"function, and argument {argname} has been given a pandas "
85+
"object as value. Since pandas.DataFrames and pandas.Series are "
86+
"known to not have unique/deterministic __repr__ functions, "
87+
"they do not work well with @webvizstore (or flask-caching). "
88+
"Consider moving to another object with a deterministic "
89+
"__repr__ representation more suitable for hashing.",
90+
RuntimeWarning,
91+
stacklevel=0,
92+
)
93+
7594
def _unique_path(self, func: Callable, argtuples: tuple) -> str:
7695
"""Encodes the argumenttuples as bytes, and then does a sha256 on that.
7796
Mutable arguments are accepted in the argument tuples, however it is

0 commit comments

Comments
 (0)