Skip to content

Commit

Permalink
ENH: compute hash without PYTHONHASHSED (#146)
Browse files Browse the repository at this point in the history
* ENH: reuse `get_readable_hash()` from AmpForm v0.15.5
* MAINT: remove `PYTHONHASHSEED` explanations
  • Loading branch information
redeboer authored Dec 24, 2024
1 parent a1f1a5f commit b57ee47
Show file tree
Hide file tree
Showing 8 changed files with 5 additions and 154 deletions.
1 change: 0 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@
"pyplot",
"pyright",
"pytest",
"PYTHONHASHSEED",
"QRules",
"recoupling",
"recouplings",
Expand Down
2 changes: 0 additions & 2 deletions .envrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
uv sync --all-extras --quiet
source .venv/bin/activate

export PYTHONHASHSEED=0
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ concurrency:
cancel-in-progress: |-
${{ github.ref != format('refs/heads/{0}', github.event.repository.default_branch) }}
env:
PYTHONHASHSEED: "0"

on:
push:
branches:
Expand Down
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ repos:
- id: check-dev-files
args:
- --doc-apt-packages=graphviz
- --environment-variables=PYTHONHASHSEED=0
- --github-pages
- --repo-name=ampform-dpd
- --repo-title=AmpForm-DPD
Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,6 @@ commands =
pytest {posargs}
description = Run all unit tests
passenv = *
setenv =
PYTHONHASHSEED = 0
[testenv:cov]
allowlist_externals =
Expand Down
91 changes: 0 additions & 91 deletions src/ampform_dpd/_cache.py

This file was deleted.

9 changes: 4 additions & 5 deletions src/ampform_dpd/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
import cloudpickle
import sympy as sp
from ampform.io import aslatex
from ampform.sympy._cache import (
get_readable_hash, # noqa: PLC2701
get_system_cache_directory, # noqa: PLC2701
)
from tensorwaves.function.sympy import create_function, create_parametrized_function

from ampform_dpd import DefinedExpression
from ampform_dpd._cache import get_readable_hash, get_system_cache_directory
from ampform_dpd.decay import (
IsobarNode,
Particle,
Expand Down Expand Up @@ -269,10 +272,6 @@ def perform_cached_lambdify( # type:ignore[misc] # pyright:ignore[reportIncons
directory will be put under the home directory, or to the path specified by
the environment variable :code:`SYMPY_CACHE_DIR`.
.. tip:: For a faster cache, set `PYTHONHASHSEED
<https://docs.python.org/3/using/cmdline.html#envvar-PYTHONHASHSEED>`_ to a
fixed value.
.. seealso:: :func:`ampform.sympy.perform_cached_doit`
"""
if cache_directory is None:
Expand Down
50 changes: 1 addition & 49 deletions tests/test_io.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
# pyright: reportPrivateUsage=false
from __future__ import annotations

import logging
import os
import sys
from os.path import abspath, dirname
from typing import TYPE_CHECKING

import pytest
import sympy as sp

from ampform_dpd._cache import _warn_about_unsafe_hash
from ampform_dpd.decay import IsobarNode, Particle
from ampform_dpd.io import aslatex, get_readable_hash

if TYPE_CHECKING:
from _pytest.logging import LogCaptureFixture

THIS_DIR = dirname(abspath(__file__))
from ampform_dpd.io import aslatex

# https://compwa-org--129.org.readthedocs.build/report/018.html#resonances-and-ls-scheme
dummy_args = {"mass": 0, "width": 0}
Expand Down Expand Up @@ -50,36 +35,3 @@ def test_aslatex_isobar_node():
node = IsobarNode(Λ1520, p, K, interaction=(2, 1))
latex = aslatex(node)
assert latex == R"\left(\Lambda(1520) \xrightarrow[S=1]{L=2} p K^-\right)"


@pytest.mark.parametrize(
("assumptions", "expected_hash"),
[
(dict(), (+7459658071388516764, +8778804591879682108)),
(dict(real=True), (+3665410414623666716, -7967572625470457155)),
(dict(rational=True), (-7926839224244779605, -8321323707982755013)),
],
)
def test_get_readable_hash(
assumptions, expected_hash: tuple[int, int], caplog: LogCaptureFixture
):
caplog.set_level(logging.WARNING)
x, y = sp.symbols("x y", **assumptions)
expr = x**2 + y
h = get_readable_hash(expr)
python_hash_seed = os.environ.get("PYTHONHASHSEED")
if python_hash_seed is None or not python_hash_seed.isdigit():
assert h[:7] == "bbc9833"
if _warn_about_unsafe_hash.cache_info().hits == 0:
assert "PYTHONHASHSEED has not been set." in caplog.text
caplog.clear()
elif python_hash_seed == "0":
if sys.version_info >= (3, 11):
expected_hash = expected_hash[1] # type:ignore[assignment]
else:
expected_hash = expected_hash[0] # type:ignore[assignment]
expected = f"pythonhashseed-0{expected_hash:+d}"
assert h == expected
else:
pytest.skip("PYTHONHASHSEED has been set, but is not 0")
assert not caplog.text

0 comments on commit b57ee47

Please sign in to comment.