Index, Slice, and ExtSlice in Python 3.9

Issue #19 new
Alex Hall created an issue

Slices has been simplified in 3.9, here’s the BPO. Basically it seems Index has been replaced by just the expression and ExtSlice has been replaced by Tuple:

>>> print(ast.dump(ast.parse("a[1]"), indent=2))
Module(
  body=[
    Expr(
      value=Subscript(
        value=Name(id='a', ctx=Load()),
        slice=Constant(value=1),
        ctx=Load()))],
  type_ignores=[])
>>> print(ast.dump(ast.parse("a[1:2]"), indent=2))
Module(
  body=[
    Expr(
      value=Subscript(
        value=Name(id='a', ctx=Load()),
        slice=Slice(
          lower=Constant(value=1),
          upper=Constant(value=2)),
        ctx=Load()))],
  type_ignores=[])
>>> print(ast.dump(ast.parse("a[1:2, 3:4]"), indent=2))
Module(
  body=[
    Expr(
      value=Subscript(
        value=Name(id='a', ctx=Load()),
        slice=Tuple(
          elts=[
            Slice(
              lower=Constant(value=1),
              upper=Constant(value=2)),
            Slice(
              lower=Constant(value=3),
              upper=Constant(value=4))],
          ctx=Load()),
        ctx=Load()))],
  type_ignores=[])

Also ast.Slice is now a subclass of ast.expr, meaning isinstance(node, ast.expr)is not completely reliable for determining if something is an expression.

Comments (1)

  1. Thomas Kluyver repo owner

    Thanks. I normally don’t aim to document new Python versions until about the time they’re released, but if you want to get the additions ready in advance, that would be fine.

    At some point I must also work out a more systematic way to deal with the changes to ASTs. At the moment there’s a bit of a mishmash of details from different Python versions, e.g. there are still nodes from Python 2.7 and Python 3.2 described there. Maybe I should limit the ‘Meet the nodes’ page to the Python versions still in support (currently 3.5 and up) and start a separate page trying to describe changes between different versions.

  2. Log in to comment