Skip to content

Commit

Permalink
ENH: set pickle protocol to highest
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Dec 20, 2024
1 parent a6673d9 commit 3fe59f2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions docs/_extend_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# pyright: reportMissingImports=false
from __future__ import annotations

import hashlib
import inspect
import logging
import pickle
Expand All @@ -24,6 +23,7 @@
from ampform.io import aslatex
from ampform.kinematics.lorentz import ArraySize, FourMomentumSymbol
from ampform.sympy._array_expressions import ArrayMultiplication
from ampform.sympy._cache import get_readable_hash

if TYPE_CHECKING:
from qrules.transition import ReactionInfo, SpinFormalism
Expand Down Expand Up @@ -727,7 +727,7 @@ def __generate_transitions_cached(
) -> ReactionInfo:
version = get_package_version("qrules")
obj = (initial_state, final_state, formalism)
h = hashlib.sha256(pickle.dumps(obj)).hexdigest()
h = get_readable_hash(obj)
docs_dir = Path(__file__).parent
file_name = docs_dir / ".cache" / f"reaction-qrules-v{version}-{h}.pickle"
file_name.parent.mkdir(exist_ok=True)
Expand Down
2 changes: 1 addition & 1 deletion src/ampform/sympy/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ def get_readable_hash(obj) -> str:
def to_bytes(obj) -> bytes:
if isinstance(obj, bytes | bytearray):
return obj
return pickle.dumps(obj)
return pickle.dumps(obj, protocol=pickle.HIGHEST_PROTOCOL)
16 changes: 8 additions & 8 deletions tests/sympy/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
@pytest.mark.parametrize(
("expected_hash", "assumptions"),
[
("564ea466060f7565ec3ee24de64e0f92", dict()),
("91495f4a4193c7ac08bd53e7fb5a1521", dict(real=True)),
("dba358d78f1aec9641114d7a26d59a09", dict(rational=True)),
("a7559ca6e9037446da186a62ff1ed673", dict()),
("f4b1fadd65890a86cbac47da2435694d", dict(real=True)),
("d5bdc74485ba4d3f16f387541315f64d", dict(rational=True)),
],
ids=["symbol", "symbol-real", "symbol-rational"],
)
Expand Down Expand Up @@ -50,7 +50,7 @@ def test_get_readable_hash_energy_dependent_width():
meson_radius=d,
)
h = get_readable_hash(expr)
assert h == "2ebebe58be64f0b77540fd138597c28e"
assert h == "ccafec30a6b6974920652b9df1b13c57"


class TestLargeHash:
Expand All @@ -62,8 +62,8 @@ class TestLargeHash:
@pytest.mark.parametrize(
("expected_hash", "formalism"),
[
("65106a44301f9340e633d09f66ad7d17", "canonical-helicity"),
("9646d3ee5c5e8534deb8019435161f2e", "helicity"),
("762cc006a8c4c0a0a88fce934a32577d", "canonical-helicity"),
("17fefe55a7da0810371e90bd762a176a", "helicity"),
],
ids=["canonical-helicity", "helicity"],
)
Expand All @@ -80,8 +80,8 @@ def test_reaction(self, expected_hash: str, formalism: SpinFormalism):
@pytest.mark.parametrize(
("expected_hash", "formalism"),
[
("bb6cba308b7b7691c22ffa6d462f55c8", "canonical-helicity"),
("1d31f62fd37c5053d498ee9b35ae4244", "helicity"),
("01bb11213896fcb173cb9b4ea4bdc3bf", "canonical-helicity"),
("0638a0ec2efa602e4c1f79141f7ca88a", "helicity"),
],
ids=["canonical-helicity", "helicity"],
)
Expand Down

0 comments on commit 3fe59f2

Please sign in to comment.