Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python APIView] display decorators on models #4860

Closed
mikekistler opened this issue Dec 2, 2022 · 1 comment · Fixed by #5653
Closed

[Python APIView] display decorators on models #4860

mikekistler opened this issue Dec 2, 2022 · 1 comment · Fixed by #5653

Comments

@mikekistler
Copy link
Member

From @annatisch

it seems that APIview currently only displays decorators for methods, is it possible to add support for displaying decorators on models as well to prevent this being missed in future?

@azure-sdk azure-sdk moved this to 🆕 New in ApiView Dec 2, 2022
@tjprescott tjprescott self-assigned this Dec 2, 2022
@tjprescott tjprescott changed the title Python APIView should display decorators on models [Python APIView] display decorators on models Dec 2, 2022
@mikekistler mikekistler moved this from 🆕 New to 📋 Backlog in ApiView Jan 9, 2023
@tjprescott
Copy link
Member

@annatisch @johanste so the issue here is that Python APIView renders essentially the "compiled" Python, meaning that when we are inspecting the live Python objects, any class decorators have already been applied, and we are seeing the resulting impact of them.

Consider this:

def get_id(self):
    return self.__id

def add_id(cls):
    cls_init = cls.__init__

    def __init__(self, id, *args, **kwargs):
        self.__id = id
        self.get_id = get_id
        cls_init(self, *args, **kwargs)

    cls.__init__ = __init__
    return cls

@add_id
class ClassWithDecorators:
    pass

What do you want to see in APIView?

Today you would see the result of @add_id applied:

# Option 1: render the final result of the applied decorator
class ClassWithDecorators:
  def __init__(self, id, *args, **kwargs)

I can parse into the source to extract and render the decorator statement, but it will still have the "applied" change to __init__:

# Option 2: render the "applied" body but still annotate the decorators
@add_id
class ClassWithDecorators:
  def __init__(self, id, *args, **kwargs)

To render it as it is in source I would either need to somehow "subtract" the applied __init__ or, given how hacky that would likely be, just switch to parsing the entire class definitions from source. Parsing from source is, for presumably good reasons, only done by exception in Python APIView today.

# Option 3: render how it appears in source
@add_id
class ClassWithDecorators:
    pass

tjprescott added a commit that referenced this issue Mar 7, 2023
@maririos maririos moved this from 📋 Backlog to 🏗 In progress in ApiView Mar 8, 2023
tjprescott added a commit that referenced this issue Mar 8, 2023
* Closes #3759.

* Display class decorators. Fixes #4860

* Fix #5197.
@github-project-automation github-project-automation bot moved this from 🏗 In progress to ✅ Done in ApiView Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

2 participants