-2

First of all, I'm super new to stackoverflow and don't really know a lot about how to ask questions and all, so please excuse any major mistakes I made.

Anyways, I'm currently trying to create a way for functions to reference themselves. For that I have created a wrapper which looks like this:

def custom_self(func):
    def wrapped(*args, **kwargs):
        return func(func, *args, **kwargs)
    return wrapped

Pretty standard stuff, honestly. I can now use this wrapper as a decorator on any new function which is supposed to reference itself. However, I'm using Visual Studio Code as editor and whenever I hover over my new function it shows something like this:

Screenshot (Sorry, apparently I'm not allowed to add pictures yet)

Now, as you can see, the code lens shows all parameters the function requires. However this includes the parameter 'own' which is supposed to be a reference to the function itself. Therefore it isn't a parameter, that you can actually pass to the function, similar to how class methods work. My question now is: How can I make it so that the code lens only shows the parameters 'arg_1' and 'arg_2'?

(This is what is displayed. As you can see the editor thinks I pass the parameter 'own' when actually I'm passing the parameter 'arg_1'. The code works just as I want it to but the displaying kinda fails.)

I already tried using 'typing.overload' as a decorator like so:

@typing.overload
def new_func(arg_1, arg_2): ...

@custom_self
def new_func(own, arg_1, arg_2):
    own.arg_1 = arg_1
    own.arg_2 = arg_2
    ...

This works and the code lens now only shows 'arg_1' and 'arg_2' as parameters, but let's just be honest, this is not a very pretty solution, because it means I have to create a dummy for every self-referencing function and I also have to declare every function twice, which makes it less 'pythonic', I guess.

I have also tried to decorate the 'custom_self' function, but this also didn't get me the desired result.

Again, the code works just fine, this is just a display error. But it's a rather annoying one at that.

Mr_HxID
  • 5
  • 1
  • 1
  • 4
  • Functions don't reference themselves because as soon as you leave the function all data about it ceases to exist. I'm really struggling to see what you are trying to do with this that wouldn't be better as a class itself. If you're shooting for a 'pythonic' solution, it's definitely a class. – Rashid 'Lee' Ibrahim Sep 25 '21 at 17:27
  • What is your intention with function referencing itself? What you're trying to achieve by that? – dannyxn Sep 25 '21 at 17:29
  • it is incredibly vague what the intention here is. I coudl not understand it. – rv.kvetch Sep 25 '21 at 17:36
  • The idea here is for the function to return its own reference so that I can access its local variables like instance variables of an object. As for the 'why', I know creating a class is a lot easier, I just wanted to see if it's possible. – Mr_HxID Sep 25 '21 at 17:41
  • accessing a functions local variables by a different function is BAD, return a tuple of the stuff you want – rioV8 Sep 25 '21 at 22:16

1 Answers1

0

The lens indicates own, arg_1, arg_2 because that is the actual signature of the function prior to decoration.
And I guess VS Code will not execute the decorator code symbolically to determine that the actual signature will be only arg_1, arg_2 because own willl be dynamically provided.
You are asking too much at once.

Without specifying the final signature for each function (which is not what you want), apart from an extension to the IDE (which would be overkill) I can't see how to achieve that.

Lenormju
  • 4,078
  • 2
  • 8
  • 22
  • 1
    Yeah, I suppose that makes sense. As I said, the code doesn't cause any errors. I was merely curious whether something like this would be possible in a relatively easy way. – Mr_HxID Oct 02 '21 at 05:28
  • You are essentially [currying](https://en.wikipedia.org/wiki/Currying) using a decorator. Maybe you could use type annotations to inform that the `own` parameter was supplied. Or maybe use [`functools.update_wrapper`](https://docs.python.org/3/library/functools.html#functools.update_wrapper) (what `functools.wraps` uses under the hood). I have no VSCode to check, but I may try to search for a solution with PyCharm. – Lenormju Oct 06 '21 at 00:20
  • @Mr_HxID I tried other approaches but could not find a proper solution. Just don't do what you wanted, sometimes clever is not best. – Lenormju Oct 15 '21 at 10:11
  • You're right, I suppose. Besides, this wrapper is for my personal use anyway, so as long as I know what's going on I technically don't need anything more specific than that. – Mr_HxID Oct 15 '21 at 14:28