News for hackers. Stuff that matters.
Re:Maccarone: AI-managed code blocks in Python by verdverm on Thursday August 24, 2023 @05:52PM (#37255033)
Would python decorators be better for something like this?

I always get squeamish when I see magic comments

Re:Maccarone: AI-managed code blocks in Python by skybrian on Thursday August 24, 2023 @06:26PM (#37255350)
This is leaving a comment for another programmer, not the compiler or interpreter. It's what comments are for, actually, like writing a TODO.

You should be squeamish about running the code without reading it first, given that you're pair-programming with a bot.

Re:Maccarone: AI-managed code blocks in Python by silverthorn on Thursday August 24, 2023 @07:08PM (#37255709)
> You should be squeamish about running the code without reading it first, given that you're pair-programming with a bot.

It's funny, the first version of this project[0] let you do exactly that, e.g.,

  def main(path: str):
      #<<filenames = a list of filenames under path>>
  
      for fn in filenames:
          #<<size = size of fn in bytes>>
  
          print(fn, size)
  
  #<<use argparse and call main>>
and then run that program like any other Python script (using import magic):

  $ python -m examples.file_sizes /etc
  …
  /etc/wgetrc 4942
  /etc/nsswitch.conf 542
  /etc/adduser.conf 3028
  /etc/ethertypes 1816
But yeah, it never felt exactly practical. :)

[0] https://github.com/bsilverthorn/maccarone/tree/v0.1.3

Re:Maccarone: AI-managed code blocks in Python by rbdixon on Thursday August 24, 2023 @06:26PM (#37255354)
Here is a framework which uses decorators to delegate runtime behavior to an LLM. Not quite what you meant but the closest I’ve seen.

https://askmarvin.ai/components/ai_function/

Re:Maccarone: AI-managed code blocks in Python by malux85 on Thursday August 24, 2023 @05:55PM (#37255060)
Decorators are a runtime construct, this is a code-writing-time feature, wouldn't it be confusing to have Maccarone find the decorators at write-time that then did nothing at runtime?
Re:Maccarone: AI-managed code blocks in Python by uranusjr on Thursday August 24, 2023 @06:00PM (#37255116)
There are examples of decorators having no runtime effects, such as typing.overload. Bit comments are probably more flexible here since it allows arbitrary blocks, not just function/class scopes.
Re:Maccarone: AI-managed code blocks in Python by orf on Thursday August 24, 2023 @06:14PM (#37255259)
How would not having the source code be present/pre-generated, and thus needing to generate it at runtime, be an example of a decorator having no runtime effects?
Re:Maccarone: AI-managed code blocks in Python by nkrumm on Thursday August 24, 2023 @06:25PM (#37255343)
I had the urge to try this out a while back, here's what I came up with: https://gist.github.com/nkrumm/2b154ea2041511233079222373c83...

The decorator invokes AI completion only the first time the function is run.

edit: I lost interest before I was able to get arguments to work ¯\_(ツ)_/¯

Re:Maccarone: AI-managed code blocks in Python by orf on Thursday August 24, 2023 @06:33PM (#37255430)
Yea, that’s cool and very possible, but it’s a runtime effect
Re:Maccarone: AI-managed code blocks in Python by verdverm on Thursday August 24, 2023 @07:04PM (#37255681)
OP has to be doing some parsing somewhere, so you just switch to seeking decorators rather than magic comments. It's still before the code reaches the interpreter.

The potential issue I see here is that comments are valid anywhere while decorators may not be, but the parser is hopefully resilient to that. You could see a multi-phase LLM that uses the interpreter to ensure the code runs / works as expected

Re:Maccarone: AI-managed code blocks in Python by orf on Thursday August 24, 2023 @07:22PM (#37255830)
It literally just adds the code to the file between the comments. There’s nothing more magic than copy+paste and no runtime component.

It’s completely different from a decorator that generates code at runtime? As in, when the code runs?

Re:Maccarone: AI-managed code blocks in Python by verdverm on Thursday August 24, 2023 @09:04PM (#37256594)
which comments should be generated?

what if the output has comments?

in my question, there is no need for the decorator to be handled at runtime, the tool that does the LLM stuff has to parse the Python code to know what to generate. It can just key off of decorators rather than comments, or at least this is my question & hypothesis.

Re:Maccarone: AI-managed code blocks in Python by uranusjr on Friday August 25, 2023 @12:14AM (#37257977)
I’m not objecting to what you say. The latter part of my comment is simply considering cases where you only want to replace a part of the function (the author have some examples on the project page), where a decorator wouldn’t be flexible enough to support. Of course you can also argue those can be easily rewritten to be function scoped, but that is a design tradeoff you make that author did not (or didn’t want to).
Re:Maccarone: AI-managed code blocks in Python by alexeldeib on Thursday August 24, 2023 @05:55PM (#37255063)
I guess comments provide a simple paradigm for many languages
Re:Maccarone: AI-managed code blocks in Python by verdverm on Thursday August 24, 2023 @06:06PM (#37255189)
comments are in most languages, so I can see that angle, but you still have to be able to parse all supported languages, no small feat

you can alternatively split generated code from human written code with files, keep the mapping in something more structured like a config file

I just normally see a better way to do the same thing a magic comment does, generally speaking. There is typically a better language construct if you limit yourself to that language (most common), and config files offer much more structure with existing tooling (mostly decode in your preferred language)