The sys.float_repr_style should be read-only
Opened this issue · 12 comments
Bug report
Bug description:
It's possible to "change" the repr style for floats:
Python 3.14.0a2+ (heads/long_export-decimal:4bb9ad04f5, Dec 17 2024, 05:39:57) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.float_repr_style
'short'
>>> sys.float_repr_style = 'legacy'
>>> sys.float_repr_style
'legacy'
Of course, this has no effect, as this variable just shows build-time settings. That certainly can misguide people, e.g. see this: #128005 (comment)
It could be nice to make this variable read-only (unfortunately, PEP 726 was rejected and it's might be not so easy). Or, at least, improve docs to mention that it's intended to be read-only (we emphasize this e.g. for int_info fields).
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
@skirpichev Are you working on it? Shall i take it?
So final resolution is to improve the docs that float_repr_style
is only a read only field, and that any change to other values such as legacy
has no effect on the formatting?
Are you working on it?
No. I'm not sure yet how it should be solved.
Shall i take it?
Sure.
So final resolution is to improve the docs that float_repr_style is only a read only field
The "problem" actually span not only this variable in the sys module. You can "change" everything:
>>> sys.maxsize
9223372036854775807
>>> sys.maxsize = 123
>>> sys.maxsize
123
>>> sys.int_info = "Spam"
>>> sys.int_info
'Spam'
Current docs looks ok for me. But it seems some people are misguided by such possibility. Maybe some generic note might help them.
https://peps.python.org/pep-0726/ was a good solution to this problem but it was rejected: https://discuss.python.org/t/pep-726-module-setattr-and-delattr/32640
It could be nice to make this variable read-only
I don't think that we can easily solve this issue and I suggest to close it.
@vstinner, does it make sense for you as a documentation issue?
Which kind of documentation addition are you thinking about? Add a note to each sys
attribute? Add a global note to the sys
module documentation?
Add a global note to the sys module documentation?
Yes, something like
diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst
index dd6293c722..1aedb277ef 100644
--- a/Doc/library/sys.rst
+++ b/Doc/library/sys.rst
@@ -8,7 +8,9 @@
This module provides access to some variables used or maintained by the
interpreter and to functions that interact strongly with the interpreter. It is
-always available.
+always available. Except when explicitly noted otherwise, all variables are
+only for read access.
+
.. data:: abiflags
sys.path
is a list and so is mutable. Does modifying sys.path
content count as treating sys.path
as read-only or not?
Well, sys.path
doc says:
A program is free to modify this list for its own purposes.
And that fails to "when explicitly noted otherwise" :-) I'm not insist on exact wording as proposed above. Does it make sense for you?
You can add this sentence if it matters to you:
Except when explicitly noted otherwise, all variables are only for read access.