diff --git a/github/Gist.py b/github/Gist.py index 4644c4eb..f99660bf 100644 --- a/github/Gist.py +++ b/github/Gist.py @@ -30,8 +30,10 @@ # along with PyGithub. If not, see . # # # ################################################################################ +from __future__ import annotations -from typing import Any, Dict +from datetime import datetime +from typing import TYPE_CHECKING, Any import github.GistComment import github.GistFile @@ -39,173 +41,142 @@ import github.GistHistoryState import github.GithubObject import github.NamedUser import github.PaginatedList +from github.GithubObject import Attribute, CompletableGithubObject, NotSet, Opt, _NotSetType, is_defined, is_optional +from github.PaginatedList import PaginatedList + +if TYPE_CHECKING: + from github.GistComment import GistComment + from github.GistHistoryState import GistHistoryState + from github.InputFileContent import InputFileContent -class Gist(github.GithubObject.CompletableGithubObject): +class Gist(CompletableGithubObject): """ This class represents Gists. The reference can be found here https://docs.github.com/en/rest/reference/gists """ + def _initAttributes(self) -> None: + self._comments: Attribute[int] = NotSet + self._comments_url: Attribute[str] = NotSet + self._commits_url: Attribute[str] = NotSet + self._created_at: Attribute[datetime] = NotSet + self._description: Attribute[str] = NotSet + self._files: Attribute[dict[str, github.GistFile.GistFile]] = NotSet + self._fork_of: Attribute[Gist] = NotSet + self._forks: Attribute[list[Gist]] = NotSet + self._forks_url: Attribute[str] = NotSet + self._git_pull_url: Attribute[str] = NotSet + self._git_push_url: Attribute[str] = NotSet + self._history: Attribute[list[GistHistoryState]] = NotSet + self._html_url: Attribute[str] = NotSet + self._id: Attribute[str] = NotSet + self._owner: Attribute[github.NamedUser.NamedUser] = NotSet + self._public: Attribute[bool] = NotSet + self._updated_at: Attribute[datetime] = NotSet + self._url: Attribute[str] = NotSet + self._user: Attribute[github.NamedUser.NamedUser] = NotSet + def __repr__(self) -> str: return self.get__repr__({"id": self._id.value}) @property - def comments(self): - """ - :type: integer - """ + def comments(self) -> int: self._completeIfNotSet(self._comments) return self._comments.value @property - def comments_url(self): - """ - :type: string - """ + def comments_url(self) -> str: self._completeIfNotSet(self._comments_url) return self._comments_url.value @property - def commits_url(self): - """ - :type: string - """ + def commits_url(self) -> str: self._completeIfNotSet(self._commits_url) return self._commits_url.value @property - def created_at(self): - """ - :type: datetime.datetime - """ + def created_at(self) -> datetime: self._completeIfNotSet(self._created_at) return self._created_at.value @property - def description(self): - """ - :type: string - """ + def description(self) -> str: self._completeIfNotSet(self._description) return self._description.value @property - def files(self): - """ - :type: dict of string to :class:`github.GistFile.GistFile` - """ + def files(self) -> dict[str, github.GistFile.GistFile]: self._completeIfNeeded() return self._files.value @property - def fork_of(self): - """ - :type: :class:`github.Gist.Gist` - """ + def fork_of(self) -> github.Gist.Gist: self._completeIfNotSet(self._fork_of) return self._fork_of.value @property - def forks(self): - """ - :type: list of :class:`github.Gist.Gist` - """ + def forks(self) -> list[Gist]: self._completeIfNotSet(self._forks) return self._forks.value @property - def forks_url(self): - """ - :type: string - """ + def forks_url(self) -> str: self._completeIfNotSet(self._forks_url) return self._forks_url.value @property - def git_pull_url(self): - """ - :type: string - """ + def git_pull_url(self) -> str: self._completeIfNotSet(self._git_pull_url) return self._git_pull_url.value @property - def git_push_url(self): - """ - :type: string - """ + def git_push_url(self) -> str: self._completeIfNotSet(self._git_push_url) return self._git_push_url.value @property - def history(self): - """ - :type: list of :class:`github.GistHistoryState.GistHistoryState` - """ + def history(self) -> list[GistHistoryState]: self._completeIfNotSet(self._history) return self._history.value @property - def html_url(self): - """ - :type: string - """ + def html_url(self) -> str: self._completeIfNotSet(self._html_url) return self._html_url.value @property - def id(self): - """ - :type: string - """ + def id(self) -> str: self._completeIfNotSet(self._id) return self._id.value @property - def owner(self): - """ - :type: :class:`github.NamedUser.NamedUser` - """ + def owner(self) -> github.NamedUser.NamedUser: self._completeIfNotSet(self._owner) return self._owner.value @property - def public(self): - """ - :type: bool - """ + def public(self) -> bool: self._completeIfNotSet(self._public) return self._public.value @property - def updated_at(self): - """ - :type: datetime.datetime - """ + def updated_at(self) -> datetime: self._completeIfNotSet(self._updated_at) return self._updated_at.value @property - def url(self): - """ - :type: string - """ + def url(self) -> str: self._completeIfNotSet(self._url) return self._url.value @property - def user(self): - """ - :type: :class:`github.NamedUser.NamedUser` - """ + def user(self) -> github.NamedUser.NamedUser: self._completeIfNotSet(self._user) return self._user.value - def create_comment(self, body): + def create_comment(self, body: str) -> GistComment: """ :calls: `POST /gists/{gist_id}/comments `_ - :param body: string - :rtype: :class:`github.GistComment.GistComment` """ assert isinstance(body, str), body post_parameters = { @@ -214,106 +185,75 @@ class Gist(github.GithubObject.CompletableGithubObject): headers, data = self._requester.requestJsonAndCheck("POST", f"{self.url}/comments", input=post_parameters) return github.GistComment.GistComment(self._requester, headers, data, completed=True) - def create_fork(self): + def create_fork(self) -> Gist: """ :calls: `POST /gists/{id}/forks `_ - :rtype: :class:`github.Gist.Gist` """ headers, data = self._requester.requestJsonAndCheck("POST", f"{self.url}/forks") return Gist(self._requester, headers, data, completed=True) - def delete(self): + def delete(self) -> None: """ :calls: `DELETE /gists/{id} `_ - :rtype: None """ headers, data = self._requester.requestJsonAndCheck("DELETE", self.url) - def edit(self, description=github.GithubObject.NotSet, files=github.GithubObject.NotSet): + def edit(self, description: Opt[str] = NotSet, files: Opt[dict[str, InputFileContent | None]] = NotSet) -> None: """ :calls: `PATCH /gists/{id} `_ - :param description: string - :param files: dict of string to :class:`github.InputFileContent.InputFileContent` - :rtype: None """ - assert description is github.GithubObject.NotSet or isinstance(description, str), description - assert files is github.GithubObject.NotSet or all( + assert is_optional(description, str), description + # limitation of `TypeGuard` + assert isinstance(files, _NotSetType) or all( element is None or isinstance(element, github.InputFileContent) for element in files.values() ), files - post_parameters = dict() - if description is not github.GithubObject.NotSet: + post_parameters: dict[str, Any] = {} + if is_defined(description): post_parameters["description"] = description - if files is not github.GithubObject.NotSet: + if is_defined(files): post_parameters["files"] = {key: None if value is None else value._identity for key, value in files.items()} headers, data = self._requester.requestJsonAndCheck("PATCH", self.url, input=post_parameters) self._useAttributes(data) - def get_comment(self, id): + def get_comment(self, id: int) -> GistComment: """ :calls: `GET /gists/{gist_id}/comments/{id} `_ - :param id: integer - :rtype: :class:`github.GistComment.GistComment` """ assert isinstance(id, int), id headers, data = self._requester.requestJsonAndCheck("GET", f"{self.url}/comments/{id}") return github.GistComment.GistComment(self._requester, headers, data, completed=True) - def get_comments(self): + def get_comments(self) -> PaginatedList[GistComment]: """ :calls: `GET /gists/{gist_id}/comments `_ - :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.GistComment.GistComment` """ - return github.PaginatedList.PaginatedList( + return PaginatedList( github.GistComment.GistComment, self._requester, f"{self.url}/comments", None, ) - def is_starred(self): + def is_starred(self) -> bool: """ :calls: `GET /gists/{id}/star `_ - :rtype: bool """ status, headers, data = self._requester.requestJson("GET", f"{self.url}/star") return status == 204 - def reset_starred(self): + def reset_starred(self) -> None: """ :calls: `DELETE /gists/{id}/star `_ - :rtype: None """ headers, data = self._requester.requestJsonAndCheck("DELETE", f"{self.url}/star") - def set_starred(self): + def set_starred(self) -> None: """ :calls: `PUT /gists/{id}/star `_ - :rtype: None """ headers, data = self._requester.requestJsonAndCheck("PUT", f"{self.url}/star") - def _initAttributes(self) -> None: - self._comments = github.GithubObject.NotSet - self._comments_url = github.GithubObject.NotSet - self._commits_url = github.GithubObject.NotSet - self._created_at = github.GithubObject.NotSet - self._description = github.GithubObject.NotSet - self._files = github.GithubObject.NotSet - self._fork_of = github.GithubObject.NotSet - self._forks = github.GithubObject.NotSet - self._forks_url = github.GithubObject.NotSet - self._git_pull_url = github.GithubObject.NotSet - self._git_push_url = github.GithubObject.NotSet - self._history = github.GithubObject.NotSet - self._html_url = github.GithubObject.NotSet - self._id = github.GithubObject.NotSet - self._owner = github.GithubObject.NotSet - self._public = github.GithubObject.NotSet - self._updated_at = github.GithubObject.NotSet - self._url = github.GithubObject.NotSet - self._user = github.GithubObject.NotSet - - def _useAttributes(self, attributes: Dict[str, Any]) -> None: + def _useAttributes(self, attributes: dict[str, Any]) -> None: if "comments" in attributes: # pragma no branch self._comments = self._makeIntAttribute(attributes["comments"]) if "comments_url" in attributes: # pragma no branch diff --git a/github/Gist.pyi b/github/Gist.pyi deleted file mode 100644 index 833e0bac..00000000 --- a/github/Gist.pyi +++ /dev/null @@ -1,66 +0,0 @@ -from datetime import datetime -from typing import Any, Dict, List, Optional, Union - -from github.GistComment import GistComment -from github.GistFile import GistFile -from github.GistHistoryState import GistHistoryState -from github.GithubObject import CompletableGithubObject, _NotSetType -from github.InputFileContent import InputFileContent -from github.NamedUser import NamedUser -from github.PaginatedList import PaginatedList - -class Gist(CompletableGithubObject): - def __repr__(self) -> str: ... - def _initAttributes(self) -> None: ... - def _useAttributes(self, attributes: Dict[str, Any]) -> None: ... - @property - def comments(self) -> int: ... - @property - def comments_url(self) -> str: ... - @property - def commits_url(self) -> str: ... - def create_comment(self, body: str) -> GistComment: ... - def create_fork(self) -> Gist: ... - @property - def created_at(self) -> datetime: ... - def delete(self) -> None: ... - @property - def description(self) -> Optional[str]: ... - def edit( - self, - description: Union[_NotSetType, str] = ..., - files: Union[_NotSetType, Dict[str, Optional[InputFileContent]]] = ..., - ) -> None: ... - @property - def files(self) -> Dict[str, GistFile]: ... - @property - def fork_of(self) -> Optional[Gist]: ... - @property - def forks(self) -> List[Gist]: ... - @property - def forks_url(self) -> str: ... - def get_comment(self, id: int) -> GistComment: ... - def get_comments(self) -> PaginatedList[GistComment]: ... - @property - def git_pull_url(self) -> str: ... - @property - def git_push_url(self) -> str: ... - @property - def history(self) -> List[GistHistoryState]: ... - @property - def html_url(self) -> str: ... - @property - def id(self) -> str: ... - def is_starred(self) -> bool: ... - @property - def owner(self) -> NamedUser: ... - @property - def public(self) -> bool: ... - def reset_starred(self) -> None: ... - def set_starred(self) -> None: ... - @property - def updated_at(self) -> datetime: ... - @property - def url(self) -> str: ... - @property - def user(self) -> Optional[NamedUser]: ... diff --git a/github/GithubObject.py b/github/GithubObject.py index 9049235e..969cc503 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -273,12 +273,12 @@ class GithubObject: def _makeDictOfStringsToClassesAttribute( self, - klass: Type["NonCompletableGithubObject"], + klass: Type[T_gh], value: Dict[ str, Union[int, Dict[str, Union[str, int, None]], Dict[str, Union[str, int]]], ], - ) -> Union[_ValuedAttribute, _BadAttribute]: + ) -> Attribute[Dict[str, T_gh]]: if isinstance(value, dict) and all( isinstance(key, str) and isinstance(element, dict) for key, element in value.items() ):