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

Possibility of boilerplate reduction #1556

Closed
JacekPliszka opened this issue Jun 11, 2020 · 5 comments
Closed

Possibility of boilerplate reduction #1556

JacekPliszka opened this issue Jun 11, 2020 · 5 comments
Labels

Comments

@JacekPliszka
Copy link

I wonder if it was possible to reduce boilerplate... something like this:

class Artifact(
    github.GithubObject.CompletableGithubObject,
    metaclass=github.GithubObject.CompletableGithubObjectMeta
):
    """
    This class represents Artifacts.
    The reference can be found here https://developer.github.com/v3/actions/artifacts/
    """
    _ATTRIBUTES = {
        "id": "Int",
        "node_id": "String",
        "name": "String",
        "size_in_bytes": "Int",
        "url": "String",
        "archive_download_url": "String",
        "expired": "Bool",
        "created_at": "Datetime",
        "expires_at": "Datetime"
    }

Then

class CompletableGithubObjectMeta(type):
    def __new__(cls, name, bases, dct):
        instance = super().__new__(cls, name, bases, dct)
        for attr in dct.get('_ATTRIBUTES') or []:
            def fget(self, attr_=attr):
                _attr = getattr(self, '_' + attr_)
                self._completeIfNotSet(_attr)
                return _attr.value
            setattr(instance, attr, property(fget))
        return instance

and

class GithubObject(object):
    def _initAttributes(self):
        for attribute in self._ATTRIBUTES:
            setattr(self, '_' + attribute, NotSet)

    def _useAttributes(self, attributes):
        for attribute, type_ in self._ATTRIBUTES.items():
            if attribute in attributes: # pragma no branch
                make_method = getattr(self, f'_make{type_}Attribute')
                setattr(self, '_' + attribute, make_method(attributes[attribute]))

And some form of .pyi autogeneration.

What do you think?

@JacekPliszka
Copy link
Author

this is Artifacts implementation

JacekPliszka#1

@s-t-e-v-e-n-k
Copy link
Collaborator

The boilerplate reduction is an interesting idea, but I think it also loses us the docstrings mentioning type of the attributes, which is important.

@JacekPliszka
Copy link
Author

docstring is not a problem as you can modify it in meta.

    _ATTRIBUTES = {
        "id": ("Int", "doctring for id"),
        "node_id": ("String","docstring for node_id"),
        "name": ("String", "docstring for name"),
...
    }

The only difference is that you need to import the class for it to work (sphinx does import).
But there is pyi which can be generated.

So the question is if there is any use of the .py file that is not importing it?

@JacekPliszka
Copy link
Author

docstring is not a problem as you can modify it in meta.

    _ATTRIBUTES = {
        "id": ("Int", "doctring for id"),
        "node_id": ("String","docstring for node_id"),
        "name": ("String", "docstring for name"),
...
    }

And then

func_wrapper.__doc__ = f'some text ``{name}`` \n{docstring}'

The only difference is that you need to import the class for it to work (sphinx does import).
But there is pyi which can be generated.

So the question is if there is any use of the .py file that is not importing it?

@stale
Copy link

stale bot commented Aug 16, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants