Skip to content

ValueError if traced code contains match-case statements #108

@krosenmann

Description

@krosenmann

Hi, big fan.

As far as I dived into errors that I caught when tried to use birdseye, it has a hard time dealing with an AST of match...case expressions. I've got various errors depend on comparison type in the expression.

python 3.11.10
birdseye 0.10.0, also 0.9.5

Example with values comparison:

import birdseye.trace_module_deep

def func():
    banana = "BANANA"
    match banana:
        case 1:
            None
        case "BANANA":
            print("YAY")
        case _:
            print("OOPS")

func()

What happened:

$ python reproduce.py

Traceback (most recent call last):
  File ".../reproduce.py", line 1, in <module>
    import birdseye.trace_module_deep
  File ".../venv/lib/python3.11/site-packages/birdseye/trace_module_deep.py", line 3, in <module>
    eye.trace_this_module(1, deep=True)
  File ".../venv/lib/python3.11/site-packages/birdseye/bird.py", line 473, in trace_this_module
    self.exec_string(source, filename, frame.f_globals, frame.f_locals, deep)
  File ".../venv/lib/python3.11/site-packages/birdseye/bird.py", line 480, in exec_string
    traced_file = self.compile(source, filename)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../venv/lib/python3.11/site-packages/birdseye/bird.py", line 147, in compile
    traced_file = super(BirdsEye, self).compile(source, filename, flags)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../venv/lib/python3.11/site-packages/birdseye/tracer.py", line 168, in compile
    return TracedFile(self, source, filename, flags)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../venv/lib/python3.11/site-packages/birdseye/tracer.py", line 76, in __init__
    self.code = compile(new_root, filename, "exec", dont_inherit=True, flags=flags)  # type: CodeType
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: patterns may only match literals and attribute lookups

Example with the type matching:

import birdseye.trace_module_deep

def func():
    banana = "BANANA"
    match banana:
        case int():
            None
        case str():
            print("YAY")
        case _:
            print("OOPS")

func()

What happened:

$ python reproduce.py 
Traceback (most recent call last):
  File "/home/rzayrullin/fellow-craft/reproduce.py", line 1, in <module>
    import birdseye.trace_module_deep
  File "/home/rzayrullin/fellow-craft/venv/lib/python3.11/site-packages/birdseye/trace_module_deep.py", line 3, in <module>
    eye.trace_this_module(1, deep=True)
  File "/home/rzayrullin/fellow-craft/venv/lib/python3.11/site-packages/birdseye/bird.py", line 473, in trace_this_module
    self.exec_string(source, filename, frame.f_globals, frame.f_locals, deep)
  File "/home/rzayrullin/fellow-craft/venv/lib/python3.11/site-packages/birdseye/bird.py", line 480, in exec_string
    traced_file = self.compile(source, filename)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rzayrullin/fellow-craft/venv/lib/python3.11/site-packages/birdseye/bird.py", line 147, in compile
    traced_file = super(BirdsEye, self).compile(source, filename, flags)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rzayrullin/fellow-craft/venv/lib/python3.11/site-packages/birdseye/tracer.py", line 168, in compile
    return TracedFile(self, source, filename, flags)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rzayrullin/fellow-craft/venv/lib/python3.11/site-packages/birdseye/tracer.py", line 76, in __init__
    self.code = compile(new_root, filename, "exec", dont_inherit=True, flags=flags)  # type: CodeType
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: MatchClass cls field can only contain Name or Attribute nodes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions