[issue27622] int.to_bytes(): docstring is not precise
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
New submission from Марк Коренберг: Docstring says: The signed keyword-only argument determines whether two's complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised. But actually, (130).to_bytes(1, 'big', signed=True) will also trigger OverflowError ---------- assignee: docs@python components: Documentation, Library (Lib) messages: 271327 nosy: docs@python, mmarkk priority: normal severity: normal status: open title: int.to_bytes(): docstring is not precise type: enhancement versions: Python 3.6 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27622> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Serhiy Storchaka added the comment: What is wrong? ---------- nosy: +serhiy.storchaka _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27622> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Марк Коренберг added the comment: The docstring does not say, that positive values also may raise OverflowError if it can not fit into `(byte_count * 8) - 1` bits. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27622> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Марк Коренберг added the comment: I mean when signed=True ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27622> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
INADA Naoki added the comment: docstring says: The integer is represented using length bytes. An OverflowError is raised if the integer is not representable with the given number of bytes. (Python 3.5.2) ---------- nosy: +methane _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27622> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Марк Коренберг added the comment: Well. Phrase `If signed is False and a negative integer is given, an OverflowError is raised.` describe onlyhalf of behaviour. It does not explain behaviour when `signed=True` and value is big enough to trigger exception, but not enough to trigger it when signed=False. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27622> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Марк Коренберг added the comment: So, in order to make documentation consistent, either new text should be added, or last phraze removed. That's what I think. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27622> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
INADA Naoki added the comment: @mmarkk Do you read full docstring? It's documented very clearly. --- int.to_bytes(length, byteorder, *, signed=False) -> bytes Return an array of bytes representing an integer. The integer is represented using length bytes. An OverflowError is raised if the integer is not representable with the given number of bytes. The byteorder argument determines the byte order used to represent the integer. If byteorder is 'big', the most significant byte is at the beginning of the byte array. If byteorder is 'little', the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder' as the byte order value. The signed keyword-only argument determines whether two's complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised. --- ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27622> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Serhiy Storchaka added the comment: The docstring looks correct to me. The last phrase corresponds to the last paragraph that describes the signed keyword-only argument. Is there anyone else for whom the docstring looks wrong? ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27622> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
R. David Murray added the comment: Looks correct to me as well. If you can think of a wording that would be clearer, Марк, that would be great, but it shouldn't make the docstring much wordier (we strive for conciseness in docstrings). That the last phrase can't be just removed without omitting an important piece of information (an *additional* time OverflowError can be raised). ---------- nosy: +r.david.murray _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27622> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
R. David Murray added the comment: Looks correct to me as well. If you can think of a wording that would be clearer, Марк, that would be great, but it shouldn't make the docstring much wordier (we strive for conciseness in docstrings). That last phrase can't be just removed without omitting an important piece of information (an *additional* time OverflowError can be raised). ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27622> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Changes by R. David Murray <rdmurray@bitdance.com>: ---------- Removed message: http://bugs.python.org/msg271381 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27622> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Марк Коренберг added the comment: Ok. This is minor fix, I will not fight for my point of view :) ---------- resolution: -> rejected status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue27622> _______________________________________
participants (4)
-
INADA Naoki
-
R. David Murray
-
Serhiy Storchaka
-
Марк Коренберг