one of many python attributes not carrying over to children in GitHub class, with and without Django support

47 views Asked by At

When i subclass my GitHubIssues class (see below), all the children have the expected attributes EXCEPT self.repo_object... when i redeclare it in child classes, it functions as it should... but i shouldn't need to do this if my knowledge of inheritance is correct.

class GitHubIssues:
    class UnknownRepositoryError(UnknownObjectException):
        ...

    def __init__(self, repo_name: str, github_key_path: str, data: dict = None):
        # TODO: test this as standalone more.
        self.repo_name = repo_name
        self.repo_obj = None
        self._IsDjangoForm = None
        self._GitHubKeyPath = github_key_path
        self._GitHubKey = None
        self._IsInIssues = None

        if not self.IsDjangoForm:
            self.data: dict = data
            self.cleaned_data: dict = self.data
            self.errors: list = []

        self.repo_obj = self.GetRepoObject()
# other attributes omitted for brevity 

    def GetRepoObject(self):
        try:
            g = Github(self.__GitHubKey)
            for repo in g.get_user().get_repos():
                if repo.name == self.repo_name:
                    self.repo_obj = repo
                    return self.repo_obj

        except BadCredentialsException as e:
            self.add_error(None, e)
        except GithubException as e:
            self.add_error(None, e)
        except Exception as e:
            self.add_error(None, e)

        if isinstance(self.repo_obj, Repository.Repository):
            pass
        else:
            try:
                raise self.UnknownRepositoryError(404, data=self.repo_obj, message=f"Could not find {self.repo_name}")
            except self.UnknownRepositoryError as e:
                self.add_error(None, e)

class BugReportForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._issue_added = None

        if kwargs:
            if 'repo_name' in kwargs:
                self.repo_name = kwargs['repo_name']
            else:
                self.repo_name = 'BackflowPreventionValveCatalog'
            if 'GitHubKeyPath' in kwargs:
                self._GitHubKeyPath = kwargs['GitHubKeyPath']
            else:
                self._GitHubKeyPath = #redacted path
        else:
            self.repo_name = 'BackflowPreventionValveCatalog'
            self._GitHubKeyPath = #redacted path

    @property
    def issue_added(self):
        return self._issue_added

    @issue_added.setter
    def issue_added(self, value):
        self._issue_added = value


class BugReportModelForm(BugReportForm, forms.ModelForm):
    def save(self, commit=True):
        self.instance = super().save()
        if self.issue_added:
            pass
        else:
            self.issue_added = False
            self.instance.save()
        return self.instance

    class Meta:
        model = BugReport
        fields = ['Page', 'BugTitle', 'Bug_Description', 'BugType', 'YourName']


class ValveBugReportForm(BugReportForm, GitHubIssues):
    ...


class ValveBugReportModelForm(ValveBugReportForm, BugReportModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # FIXME: why does this need to be redeclared? is it an MRO issue? if this declaration is commented out, the class throws AttributeError: 'ValveBugReportModelForm' object has no attribute 'repo_obj'
        #self.repo_obj = self.GetRepoObject()

    def save(self, commit=True):
        self.instance = super().save()
        # FIXME: there might be some duplication here... self._issue_added
        self.issue_added = self.CreateGitHubIssue()
        if self.issue_added or self.IsInIssues:
            pass
        else:
            self.instance.InIssues = False
            self.instance.save()
        return self.instance

Adding self.repo_obj = self.GetRepoObject() to the subclass fixes the issue, but I shouldn't need to do that if my knowledge of python is correct?

0

There are 0 answers