Skip to content

Make Prior.__repr__ round-trippable for numpy scalar parameters#1108

Open
lorenzopompili00 wants to merge 1 commit into
bilby-dev:mainfrom
lorenzopompili00:fix-prior-repr-numpy-scalar
Open

Make Prior.__repr__ round-trippable for numpy scalar parameters#1108
lorenzopompili00 wants to merge 1 commit into
bilby-dev:mainfrom
lorenzopompili00:fix-prior-repr-numpy-scalar

Conversation

@lorenzopompili00

Copy link
Copy Markdown
Contributor

With numpy 2.0+, repr() of numpy scalars carry an np. prefix (e.g. repr(np.float64(0.0)) is "np.float64(0.0)"). This goes into Prior.__repr__, and bilby's parser Prior._parse_argument_string then fails to re-parse it: it treatsnp as a module and calls import_module("np") which fails.

Can be reproduced by running

import numpy as np
from bilby.core.prior import Uniform, PriorDict

p = Uniform(minimum=np.float64(0.0), maximum=np.float64(1.0), name="x")
print(repr(p))          # Uniform(minimum=np.float64(0.0), maximum=np.float64(1.0), ...)
PriorDict({"x": repr(p)})   # ModuleNotFoundError: No module named 'np'

This fixes it by converting numpy scalars to native Python types in Prior.__repr__

Copilot AI review requested due to automatic review settings June 30, 2026 09:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR restores Prior.__repr__ round-trippability when prior parameters are NumPy scalar types (notably under NumPy >= 2.0 where scalar repr() includes an np. prefix), preventing PriorDict parsing from mistakenly attempting import_module("np").

Changes:

  • Convert NumPy scalar values (np.generic) to native Python scalars via .item() when building Prior.__repr__.
  • Add a regression test ensuring repr() output for Uniform with NumPy scalar bounds contains no np. and can be parsed back via PriorDict.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
bilby/core/prior/base.py Ensures Prior.__repr__ emits native Python scalar literals for NumPy scalar parameters to keep from_repr / _parse_argument_string round-trippable.
test/core/prior/base_test.py Adds coverage to prevent regressions in repr() round-tripping for priors constructed with NumPy scalar parameters.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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