Skip to content

Commit

Permalink
Cope with col_offset improvements in Python 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Jun 27, 2023
1 parent efb34f2 commit 4f79dff
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
1 change: 1 addition & 0 deletions astroid/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
PY39_PLUS = sys.version_info >= (3, 9)
PY310_PLUS = sys.version_info >= (3, 10)
PY311_PLUS = sys.version_info >= (3, 11)
PY312_PLUS = sys.version_info >= (3, 12)

WIN32 = sys.platform == "win32"

Expand Down
48 changes: 36 additions & 12 deletions tests/test_nodes_lineno.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import astroid
from astroid import builder, nodes
from astroid.const import IS_PYPY, PY38, PY39_PLUS, PY310_PLUS
from astroid.const import IS_PYPY, PY38, PY39_PLUS, PY310_PLUS, PY312_PLUS


@pytest.mark.skipif(
Expand Down Expand Up @@ -977,13 +977,24 @@ def test_end_lineno_string() -> None:
assert isinstance(s1.values[0], nodes.Const)
assert (s1.lineno, s1.col_offset) == (1, 0)
assert (s1.end_lineno, s1.end_col_offset) == (1, 29)
assert (s1.values[0].lineno, s1.values[0].col_offset) == (1, 0)
assert (s1.values[0].end_lineno, s1.values[0].end_col_offset) == (1, 29)
if PY312_PLUS:
assert (s1.values[0].lineno, s1.values[0].col_offset) == (1, 2)
assert (s1.values[0].end_lineno, s1.values[0].end_col_offset) == (1, 15)
else:
# Bug in Python 3.11
# https://github.com/python/cpython/issues/81639
assert (s1.values[0].lineno, s1.values[0].col_offset) == (1, 0)
assert (s1.values[0].end_lineno, s1.values[0].end_col_offset) == (1, 29)

s2 = s1.values[1]
assert isinstance(s2, nodes.FormattedValue)
assert (s2.lineno, s2.col_offset) == (1, 0)
assert (s2.end_lineno, s2.end_col_offset) == (1, 29)
if PY312_PLUS:
assert (s2.lineno, s2.col_offset) == (1, 15)
assert (s2.end_lineno, s2.end_col_offset) == (1, 28)
else:
assert (s2.lineno, s2.col_offset) == (1, 0)
assert (s2.end_lineno, s2.end_col_offset) == (1, 29)

assert isinstance(s2.value, nodes.Const) # 42.1234
if PY39_PLUS:
assert (s2.value.lineno, s2.value.col_offset) == (1, 16)
Expand All @@ -993,22 +1004,35 @@ def test_end_lineno_string() -> None:
# https://bugs.python.org/issue44885
assert (s2.value.lineno, s2.value.col_offset) == (1, 1)
assert (s2.value.end_lineno, s2.value.end_col_offset) == (1, 8)
assert isinstance(s2.format_spec, nodes.JoinedStr) # '02d'
assert (s2.format_spec.lineno, s2.format_spec.col_offset) == (1, 0)
assert (s2.format_spec.end_lineno, s2.format_spec.end_col_offset) == (1, 29)
assert isinstance(s2.format_spec, nodes.JoinedStr) # ':02d'
if PY312_PLUS:
assert (s2.format_spec.lineno, s2.format_spec.col_offset) == (1, 23)
assert (s2.format_spec.end_lineno, s2.format_spec.end_col_offset) == (1, 27)
else:
assert (s2.format_spec.lineno, s2.format_spec.col_offset) == (1, 0)
assert (s2.format_spec.end_lineno, s2.format_spec.end_col_offset) == (1, 29)

s3 = ast_nodes[1]
assert isinstance(s3, nodes.JoinedStr)
assert isinstance(s3.values[0], nodes.Const)
assert (s3.lineno, s3.col_offset) == (2, 0)
assert (s3.end_lineno, s3.end_col_offset) == (2, 17)
assert (s3.values[0].lineno, s3.values[0].col_offset) == (2, 0)
assert (s3.values[0].end_lineno, s3.values[0].end_col_offset) == (2, 17)
if PY312_PLUS:
assert (s3.values[0].lineno, s3.values[0].col_offset) == (2, 2)
assert (s3.values[0].end_lineno, s3.values[0].end_col_offset) == (2, 15)
else:
assert (s3.values[0].lineno, s3.values[0].col_offset) == (2, 0)
assert (s3.values[0].end_lineno, s3.values[0].end_col_offset) == (2, 17)

s4 = s3.values[1]
assert isinstance(s4, nodes.FormattedValue)
assert (s4.lineno, s4.col_offset) == (2, 0)
assert (s4.end_lineno, s4.end_col_offset) == (2, 17)
if PY312_PLUS:
assert (s4.lineno, s4.col_offset) == (2, 9)
assert (s4.end_lineno, s4.end_col_offset) == (2, 16)
else:
assert (s4.lineno, s4.col_offset) == (2, 0)
assert (s4.end_lineno, s4.end_col_offset) == (2, 17)

assert isinstance(s4.value, nodes.Name) # 'name'
if PY39_PLUS:
assert (s4.value.lineno, s4.value.col_offset) == (2, 10)
Expand Down

0 comments on commit 4f79dff

Please sign in to comment.