8000 The sys.float_repr_style should be read-only · Issue #128017 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

The sys.float_repr_style should be read-only #128017

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

Closed
skirpichev opened this issue Dec 17, 2024 · 13 comments
Closed

The sys.float_repr_style should be read-only #128017

skirpichev opened this issue Dec 17, 2024 · 13 comments
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@skirpichev
Copy link
Contributor
skirpichev commented Dec 17, 2024

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

Linked PRs

@skirpichev skirpichev added the type-bug An unexpected behavior, bug, or error label Dec 17, 2024
@srinivasreddy
Copy link
Contributor
srinivasreddy commented Dec 17, 2024

@skirpichev Are you working on it? Shall i take it?

8000

@srinivasreddy
Copy link
Contributor

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?

@picnixz picnixz added interpreter-core (Objects, Python, Grammar, and Parser dirs) extension-modules C modules in the Modules dir and removed extension-modules C modules in the Modules dir labels Dec 17, 2024
@skirpichev
Copy link
Contributor Author

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.

@vstinner
Copy link
Member

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

@vstinner
Copy link
Member

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.

@skirpichev
Copy link
Contributor Author

@vstinner, does it make sense for you as a documentation issue?

@vstinner
Copy link
Member

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?

@skirpichev
Copy link
Contributor Author

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

@skirpichev skirpichev added docs Documentation in the Doc dir and removed interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Dec 18, 2024
@vstinner
Copy link
Member

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.

@skirpichev
Copy link
Contributor Author

And that fails to "when explicitly noted otherwise" :-) I'm not insist on exact wording as proposed above. Does it make sense for you?

@vstinner
Copy link
Member

You can add this sentence if it matters to you:

Except when explicitly noted otherwise, all variables are only for read access.

@skirpichev
Copy link
Contributor Author

@srinivasreddy?

srinivasreddy added a commit to srinivasreddy/cpython that referenced this issue Jan 15, 2025
…ule: sys are read-only, Unless explicitly stated otherwise
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 16, 2025
…GH-128887)

(cherry picked from commit 313b96e)

Co-authored-by: Srinivas Reddy Thatiparthy (తాటిపర్తి శ్రీనివాస్  రెడ్డి) <thatiparthysreenivas@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 16, 2025
…GH-128887)

(cherry picked from commit 313b96e)

Co-authored-by: Srinivas Reddy Thatiparthy (తాటిపర్తి శ్రీనివాస్  రెడ్డి) <thatiparthysreenivas@gmail.com>
@vstinner
Copy link
Member

Issue fixed the in documentation by change 313b96e.

vstinner pushed a commit that referenced this issue Jan 16, 2025
…8887) (#128909)

gh-128017: Make a note that sys variables are read-only (GH-128887)
(cherry picked from commit 313b96e)

Co-authored-by: Srinivas Reddy Thatiparthy (తాటిపర్తి శ్రీనివాస్  రెడ్డి) <thatiparthysreenivas@gmail.com>
hugovk pushed a commit that referenced this issue Jan 17, 2025
…8887) (#128908)

Co-authored-by: Srinivas Reddy Thatiparthy (తాటిపర్తి శ్రీనివాస్  రెడ్డి) <thatiparthysreenivas@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
Projects
Status: Todo
Development

No branches or pull requests

4 participants
0