[New-bugs-announce] [issue40662] ast.get_source_segment behaviour with missing location info doesn't match doctoring

Irit Katriel report at bugs.python.org
Sun May 17 13:35:18 EDT 2020


New submission from Irit Katriel <iritkatriel at yahoo.com>:

The doctoring says "If some location information (lineno, end_lineno, col_offset, or end_col_offset) is missing, return None."

However:

>>> s = "12"
>>> node = ast.parse(s).body[0]
>>> ast.get_source_segment(s, node)
'12'
>>> del node.lineno
>>> ast.get_source_segment(s, node)
>>> node = ast.parse(s).body[0]
>>> del node.end_lineno
>>> ast.get_source_segment(s, node)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/iritkatriel/src/cpython/Lib/ast.py", line 336, in get_source_segment
    end_lineno = node.end_lineno - 1
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

In other parts of ast.py there are hasattr checks for lineno/col_offset alongside checks for None for end_lineno/end_col_offset. So this difference in behaviour of the end_ fields seems intended.

----------
components: Library (Lib)
messages: 369139
nosy: benjamin.peterson, brett.cannon, iritkatriel, pablogsal, yselivanov
priority: normal
pull_requests: 19458
severity: normal
status: open
title: ast.get_source_segment behaviour with missing location info doesn't match doctoring
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40662>
_______________________________________


More information about the New-bugs-announce mailing list