Skip to content

Commit 4f79dff

Browse files
Cope with col_offset improvements in Python 3.12
See python/cpython#81639
1 parent efb34f2 commit 4f79dff

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

astroid/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
PY39_PLUS = sys.version_info >= (3, 9)
1111
PY310_PLUS = sys.version_info >= (3, 10)
1212
PY311_PLUS = sys.version_info >= (3, 11)
13+
PY312_PLUS = sys.version_info >= (3, 12)
1314

1415
WIN32 = sys.platform == "win32"
1516

tests/test_nodes_lineno.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import astroid
1010
from astroid import builder, nodes
11-
from astroid.const import IS_PYPY, PY38, PY39_PLUS, PY310_PLUS
11+
from astroid.const import IS_PYPY, PY38, PY39_PLUS, PY310_PLUS, PY312_PLUS
1212

1313

1414
@pytest.mark.skipif(
@@ -977,13 +977,24 @@ def test_end_lineno_string() -> None:
977977
assert isinstance(s1.values[0], nodes.Const)
978978
assert (s1.lineno, s1.col_offset) == (1, 0)
979979
assert (s1.end_lineno, s1.end_col_offset) == (1, 29)
980-
assert (s1.values[0].lineno, s1.values[0].col_offset) == (1, 0)
981-
assert (s1.values[0].end_lineno, s1.values[0].end_col_offset) == (1, 29)
980+
if PY312_PLUS:
981+
assert (s1.values[0].lineno, s1.values[0].col_offset) == (1, 2)
982+
assert (s1.values[0].end_lineno, s1.values[0].end_col_offset) == (1, 15)
983+
else:
984+
# Bug in Python 3.11
985+
# https://github.com/python/cpython/issues/81639
986+
assert (s1.values[0].lineno, s1.values[0].col_offset) == (1, 0)
987+
assert (s1.values[0].end_lineno, s1.values[0].end_col_offset) == (1, 29)
982988

983989
s2 = s1.values[1]
984990
assert isinstance(s2, nodes.FormattedValue)
985-
assert (s2.lineno, s2.col_offset) == (1, 0)
986-
assert (s2.end_lineno, s2.end_col_offset) == (1, 29)
991+
if PY312_PLUS:
992+
assert (s2.lineno, s2.col_offset) == (1, 15)
993+
assert (s2.end_lineno, s2.end_col_offset) == (1, 28)
994+
else:
995+
assert (s2.lineno, s2.col_offset) == (1, 0)
996+
assert (s2.end_lineno, s2.end_col_offset) == (1, 29)
997+
987998
assert isinstance(s2.value, nodes.Const) # 42.1234
988999
if PY39_PLUS:
9891000
assert (s2.value.lineno, s2.value.col_offset) == (1, 16)
@@ -993,22 +1004,35 @@ def test_end_lineno_string() -> None:
9931004
# https://bugs.python.org/issue44885
9941005
assert (s2.value.lineno, s2.value.col_offset) == (1, 1)
9951006
assert (s2.value.end_lineno, s2.value.end_col_offset) == (1, 8)
996-
assert isinstance(s2.format_spec, nodes.JoinedStr) # '02d'
997-
assert (s2.format_spec.lineno, s2.format_spec.col_offset) == (1, 0)
998-
assert (s2.format_spec.end_lineno, s2.format_spec.end_col_offset) == (1, 29)
1007+
assert isinstance(s2.format_spec, nodes.JoinedStr) # ':02d'
1008+
if PY312_PLUS:
1009+
assert (s2.format_spec.lineno, s2.format_spec.col_offset) == (1, 23)
1010+
assert (s2.format_spec.end_lineno, s2.format_spec.end_col_offset) == (1, 27)
1011+
else:
1012+
assert (s2.format_spec.lineno, s2.format_spec.col_offset) == (1, 0)
1013+
assert (s2.format_spec.end_lineno, s2.format_spec.end_col_offset) == (1, 29)
9991014

10001015
s3 = ast_nodes[1]
10011016
assert isinstance(s3, nodes.JoinedStr)
10021017
assert isinstance(s3.values[0], nodes.Const)
10031018
assert (s3.lineno, s3.col_offset) == (2, 0)
10041019
assert (s3.end_lineno, s3.end_col_offset) == (2, 17)
1005-
assert (s3.values[0].lineno, s3.values[0].col_offset) == (2, 0)
1006-
assert (s3.values[0].end_lineno, s3.values[0].end_col_offset) == (2, 17)
1020+
if PY312_PLUS:
1021+
assert (s3.values[0].lineno, s3.values[0].col_offset) == (2, 2)
1022+
assert (s3.values[0].end_lineno, s3.values[0].end_col_offset) == (2, 15)
1023+
else:
1024+
assert (s3.values[0].lineno, s3.values[0].col_offset) == (2, 0)
1025+
assert (s3.values[0].end_lineno, s3.values[0].end_col_offset) == (2, 17)
10071026

10081027
s4 = s3.values[1]
10091028
assert isinstance(s4, nodes.FormattedValue)
1010-
assert (s4.lineno, s4.col_offset) == (2, 0)
1011-
assert (s4.end_lineno, s4.end_col_offset) == (2, 17)
1029+
if PY312_PLUS:
1030+
assert (s4.lineno, s4.col_offset) == (2, 9)
1031+
assert (s4.end_lineno, s4.end_col_offset) == (2, 16)
1032+
else:
1033+
assert (s4.lineno, s4.col_offset) == (2, 0)
1034+
assert (s4.end_lineno, s4.end_col_offset) == (2, 17)
1035+
10121036
assert isinstance(s4.value, nodes.Name) # 'name'
10131037
if PY39_PLUS:
10141038
assert (s4.value.lineno, s4.value.col_offset) == (2, 10)

0 commit comments

Comments
 (0)