deshaw / pyflyby

A set of productivity tools for Python
https://deshaw.github.io/pyflyby/
Other
347 stars 52 forks source link

Pyflyby crashes in structural pattern matching of dict #338

Closed Atry closed 4 months ago

Atry commented 4 months ago

Given the following pyflyby_bug.py file:

match { "foo": 1, "bar": 2 }:
    case {
        "foo": foo,
        "bar": bar,
        **rest,
    }:
        pass
    case _:
        pass

When running:

tidy-imports pyflyby_bug.py

I got the error:

Traceback (most recent call last):
  File "/home/bo/peftai/.venv/lib/python3.11/site-packages/pyflyby/_cmdline.py", line 391, in process_actions
    action(m)
  File "/home/bo/peftai/.venv/lib/python3.11/site-packages/pyflyby/_cmdline.py", line 433, in action_ifchanged
    if m.output_content.joined == m.input_content.joined:
       ^^^^^^^^^^^^^^^^
  File "/nix/store/7wz6hm9i8wljz0hgwz1wqmn2zlbgavrq-python3-3.11.8/lib/python3.11/functools.py", line 1001, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/home/bo/peftai/.venv/lib/python3.11/site-packages/pyflyby/_cmdline.py", line 346, in output_content
    return FileText(self.modifier(self.input_content), filename=self.filename)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bo/peftai/.venv/bin/tidy-imports", line 151, in modify
    x = fix_unused_and_missing_imports(
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bo/peftai/.venv/lib/python3.11/site-packages/pyflyby/_imports2s.py", line 345, in fix_unused_and_missing_imports
    codeblock = reformat_import_statements(codeblock, params=params)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bo/peftai/.venv/lib/python3.11/site-packages/pyflyby/_imports2s.py", line 276, in reformat_import_statements
    transformer = SourceToSourceFileImportsTransformation(codeblock)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bo/peftai/.venv/lib/python3.11/site-packages/pyflyby/_imports2s.py", line 26, in __new__
    return cls._from_source_code(arg)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bo/peftai/.venv/lib/python3.11/site-packages/pyflyby/_imports2s.py", line 34, in _from_source_code
    self.preprocess()
  File "/home/bo/peftai/.venv/lib/python3.11/site-packages/pyflyby/_imports2s.py", line 94, in preprocess
    for is_imports, subblock in self.input.groupby(lambda ps: ps.is_import):
  File "/home/bo/peftai/.venv/lib/python3.11/site-packages/pyflyby/_parse.py", line 1268, in groupby
    for pred, stmts in groupby(self.statements, predicate):
                               ^^^^^^^^^^^^^^^
  File "/nix/store/7wz6hm9i8wljz0hgwz1wqmn2zlbgavrq-python3-3.11.8/lib/python3.11/functools.py", line 1001, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/home/bo/peftai/.venv/lib/python3.11/site-packages/pyflyby/_parse.py", line 1208, in statements
    node = self.annotated_ast_node
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/7wz6hm9i8wljz0hgwz1wqmn2zlbgavrq-python3-3.11.8/lib/python3.11/functools.py", line 1001, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/home/bo/peftai/.venv/lib/python3.11/site-packages/pyflyby/_parse.py", line 1131, in annotated_ast_node
    _annotate_ast_nodes(result)
  File "/home/bo/peftai/.venv/lib/python3.11/site-packages/pyflyby/_parse.py", line 292, in _annotate_ast_nodes
    _annotate_ast_startpos(ast_node, None, startpos, text, flags)
  File "/home/bo/peftai/.venv/lib/python3.11/site-packages/pyflyby/_parse.py", line 357, in _annotate_ast_startpos
    leftstr = _annotate_ast_startpos(child_node, ast_node,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bo/peftai/.venv/lib/python3.11/site-packages/pyflyby/_parse.py", line 357, in _annotate_ast_startpos
    leftstr = _annotate_ast_startpos(child_node, ast_node,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bo/peftai/.venv/lib/python3.11/site-packages/pyflyby/_parse.py", line 357, in _annotate_ast_startpos
    leftstr = _annotate_ast_startpos(child_node, ast_node,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bo/peftai/.venv/lib/python3.11/site-packages/pyflyby/_parse.py", line 363, in _annotate_ast_startpos
    raise AssertionError(
AssertionError: While processing /home/bo/peftai/pyflyby_bug.py: Got out-of-order AST node(s):
  parent minpos=(1,7)
    node: MatchMapping(keys=[Constant(value='foo'), Constant(value='bar')], patterns=[MatchAs(name='foo'), MatchAs(name='bar')], rest='rest')
      fields: keys patterns rest
      children:
                (3,9): Constant(value='foo')
                (4,9): Constant(value='bar')
        ==>    (3,16): MatchAs(name='foo')
                     : MatchAs(name='bar')

This indicates a bug in pyflyby._

pyflyby developer: Check if there's a bug or missing ast node handler in pyflyby._parse._iter_child_nodes_in_order() - probably the handler for ast.MatchMapping.

/home/bo/peftai/.venv/bin/tidy-imports: encountered the following problems:
    /home/bo/peftai/pyflyby_bug.py: AssertionError: Got out-of-order AST node(s):              parent minpos=(1,7)
                node: MatchMapping(keys=[Constant(value='foo'), Constant(value='bar')], patterns=[MatchAs(name='foo'), MatchAs(name='bar')], rest='rest')
                  fields: keys patterns rest
                  children:
                            (3,9): Constant(value='foo')
                            (4,9): Constant(value='bar')
                    ==>    (3,16): MatchAs(name='foo')
                                 : MatchAs(name='bar')

            This indicates a bug in pyflyby._

            pyflyby developer: Check if there's a bug or missing ast node handler in pyflyby._parse._iter_child_nodes_in_order() - probably the handler for ast.MatchMapping.
Atry commented 4 months ago

Thank you!

Atry commented 2 months ago

@Carreau Any plan to release the next version?

Carreau commented 2 months ago

While I can make releases, usually @dshivashankar1994 does it and I don't want to overstep.

Carreau commented 2 months ago

While I can make releases, usually @dshivashankar1994 does it and I don't want to overstep.

Released as 1.9.4