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

BUG: stats._distribution_infrastructure._Domain.symbols class attribute is shared by instances #22137

Closed
jorenham opened this issue Dec 20, 2024 · 2 comments · Fixed by #22139
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.stats
Milestone

Comments

@jorenham
Copy link
Contributor

jorenham commented Dec 20, 2024

Describe your issue.

The following class attribute is defined in scipy.stats._distribution_infrastructure._Domain:

symbols = {np.inf: r"\infty", -np.inf: r"-\infty", np.pi: r"\pi", -np.pi: r"-\pi"}

Its subclass, _SimpleDomain, mutates it in an instance method:

self.symbols.update(new_symbols)

... even though _Domain.__init___ doesn't shadow it (i.e. self.symbols = ...):

def __init__(self, endpoints=(-inf, inf), inclusive=(False, False)):
a, b = endpoints
self.endpoints = np.asarray(a)[()], np.asarray(b)[()]
self.inclusive = inclusive
# self.all_inclusive = (endpoints == (-inf, inf)
# and inclusive == (True, True))

So as far as I can tell, it's directly updated on the class state, and therefore shared between all instances. I don't see why this would be intended behaviour, so I thought I'd let you know.

This is relevant to the current main branch, and I spotted this in the 1.15.0rc1 release.

Reproducing Code Example

...

Error message

...

SciPy/NumPy/Python version and system information

irrelevant
@jorenham jorenham added the defect A clear bug or issue that prevents SciPy from being installed or used as expected label Dec 20, 2024
@mdhaber mdhaber changed the title BUG: stats._distribution_infrastructure._Domain.symbols class attribute dict leaks instance state BUG: stats._distribution_infrastructure._Domain.symbols class attribute is shared by instances Dec 20, 2024
@mdhaber
Copy link
Contributor

mdhaber commented Dec 20, 2024

Thanks @jorenham. Yeah, I'll want to revisit that system at some point to see if I can simplify things. In the meantime, I think it would be sufficient to just copy it in the _SimpleDomain initializer (gh-22139).

@j-bowhay j-bowhay added this to the 1.15.0 milestone Dec 20, 2024
@jorenham
Copy link
Contributor Author

I'll update jorenham/scipy-stubs#360 accordingly (remove the ClassVar) 👌🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.stats
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants