Skip to content

Commit

Permalink
aio.api.github: Typing cleanups (#2364)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Northey <ryan@synca.io>
  • Loading branch information
phlax authored Oct 17, 2024
1 parent 2024d6f commit 408a6d2
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 151 deletions.
2 changes: 1 addition & 1 deletion aio.api.github/aio/api/github/abstract/actions/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def repo(self) -> interface.IGithubRepo:
return self._repo

@cached_property
def workflows(self) -> "interface.IGithubWorkflows":
def workflows(self) -> interface.IGithubWorkflows:
return self.github.workflows_class(actions=self)


Expand Down
44 changes: 22 additions & 22 deletions aio.api.github/aio/api/github/abstract/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import abc
from functools import cached_property
from typing import Any, Type
from typing import Any

import aiohttp

Expand Down Expand Up @@ -36,9 +36,9 @@ def __getitem__(self, k) -> interface.IGithubRepo:
# and validate `k`
return self.repo_class(self, k)

@property # type:ignore
@property
@abstracts.interfacemethod
def actions_class(self) -> Type[interface.IGithubActions]:
def actions_class(self) -> type[interface.IGithubActions]:
raise NotImplementedError

@cached_property
Expand All @@ -51,62 +51,62 @@ def api(self) -> gidgethub.aiohttp.GitHubAPI:

@property
@abc.abstractmethod
def api_class(self) -> Type[gidgethub.aiohttp.GitHubAPI]:
def api_class(self) -> type[gidgethub.aiohttp.GitHubAPI]:
"""API class."""
return gidgethub.aiohttp.GitHubAPI

@property # type:ignore
@property
@abstracts.interfacemethod
def commit_class(self) -> Type[interface.IGithubCommit]:
def commit_class(self) -> type[interface.IGithubCommit]:
raise NotImplementedError

@property # type:ignore
@property
@abstracts.interfacemethod
def issue_class(self) -> Type[interface.IGithubIssue]:
def issue_class(self) -> type[interface.IGithubIssue]:
raise NotImplementedError

@property # type:ignore
@property
@abstracts.interfacemethod
def issues_class(self) -> Type[interface.IGithubIssues]:
def issues_class(self) -> type[interface.IGithubIssues]:
raise NotImplementedError

@property # type:ignore
@property
@abstracts.interfacemethod
def iterator_class(self) -> Type[interface.IGithubIterator]:
def iterator_class(self) -> type[interface.IGithubIterator]:
"""Github iterator class.
Provides both an async iterator and a `total_count` async prop.
"""
raise NotImplementedError

@property # type:ignore
@property
@abstracts.interfacemethod
def label_class(self) -> Type[interface.IGithubLabel]:
def label_class(self) -> type[interface.IGithubLabel]:
raise NotImplementedError

@property # type:ignore
@property
@abstracts.interfacemethod
def release_class(self) -> Type[interface.IGithubRelease]:
def release_class(self) -> type[interface.IGithubRelease]:
raise NotImplementedError

@property # type:ignore
@property
@abstracts.interfacemethod
def repo_class(self) -> Type[interface.IGithubRepo]:
def repo_class(self) -> type[interface.IGithubRepo]:
"""Github repo class."""
raise NotImplementedError

@property
def session(self) -> aiohttp.ClientSession:
return self._session

@property # type:ignore
@property
@abstracts.interfacemethod
def tag_class(self) -> Type[interface.IGithubTag]:
def tag_class(self) -> type[interface.IGithubTag]:
raise NotImplementedError

@property # type:ignore
@property
@abstracts.interfacemethod
def workflows_class(self) -> Type[interface.IGithubWorkflows]:
def workflows_class(self) -> type[interface.IGithubWorkflows]:
raise NotImplementedError

async def getitem(self, *args, **kwargs) -> Any:
Expand Down
26 changes: 12 additions & 14 deletions aio.api.github/aio/api/github/abstract/issues/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
import abc
import re
from functools import cached_property
from typing import (
Any, AsyncGenerator,
Pattern, Type)
from typing import Any, AsyncGenerator, Pattern

import abstracts

Expand Down Expand Up @@ -103,7 +101,7 @@ async def __aiter__(
if (issue := self.issue_class(self, issue)).key:
yield issue

@property # type:ignore
@property
@abstracts.interfacemethod
def closing_tpl(self) -> str:
raise NotImplementedError
Expand All @@ -127,9 +125,9 @@ def github(self) -> interface.IGithubAPI:
def issue_author(self) -> str:
return self._issue_author or ISSUE_AUTHOR

@property # type:ignore
@property
@abstracts.interfacemethod
def issue_class(self) -> Type[interface.IGithubTrackedIssue]:
def issue_class(self) -> type[interface.IGithubTrackedIssue]:
raise NotImplementedError

@async_property(cache=True)
Expand All @@ -140,12 +138,12 @@ async def issues(self) -> dict[str, interface.IGithubTrackedIssue]:
issues[issue.key] = issue
return issues

@property # type:ignore
@property
@abc.abstractmethod
def issues_search_tpl(self):
return ISSUES_SEARCH_TPL

@property # type:ignore
@property
@abstracts.interfacemethod
def labels(self) -> tuple[str, ...]:
raise NotImplementedError
Expand Down Expand Up @@ -175,7 +173,7 @@ async def open_issues(self) -> tuple[interface.IGithubTrackedIssue, ...]:
def repo(self) -> interface.IGithubRepo:
return self.github[self.repo_name]

@property # type:ignore
@property
@abstracts.interfacemethod
def repo_name(self) -> str:
raise NotImplementedError
Expand All @@ -186,17 +184,17 @@ def title_re(self) -> Pattern[str]:
self.title_re_tpl.format(
title_prefix=self.title_prefix))

@property # type:ignore
@property
@abstracts.interfacemethod
def title_prefix(self) -> str:
raise NotImplementedError

@property # type:ignore
@property
@abstracts.interfacemethod
def title_re_tpl(self):
raise NotImplementedError

@property # type:ignore
@property
@abstracts.interfacemethod
def title_tpl(self):
raise NotImplementedError
Expand Down Expand Up @@ -224,7 +222,7 @@ async def issue_body(self, **kwargs) -> str:
async def issue_title(self, **kwargs) -> str:
raise NotImplementedError

def iter_issues(self) -> "interface.IGithubIterator":
def iter_issues(self) -> interface.IGithubIterator:
return self.repo.issues.search(
self.issues_search_tpl.format(self=self))

Expand All @@ -245,7 +243,7 @@ def __init__(self, github) -> None:
def __getitem__(self, k) -> interface.IGithubTrackedIssues:
return self.tracked_issues[k]

@property # type:ignore
@property
@abstracts.interfacemethod
def tracked_issues(self) -> dict[str, interface.IGithubTrackedIssues]:
raise NotImplementedError
10 changes: 5 additions & 5 deletions aio.api.github/aio/api/github/abstract/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pathlib
from datetime import datetime
from functools import cached_property
from typing import AsyncIterator, Callable, Type
from typing import AsyncIterator, Callable

from packaging import version

Expand Down Expand Up @@ -50,9 +50,9 @@ def __str__(self):
def assets(self) -> interface.IGithubReleaseAssets:
return self.assets_class(self)

@property # type: ignore
@property
@abstracts.interfacemethod
def assets_class(self) -> Type[interface.IGithubReleaseAssets]:
def assets_class(self) -> type[interface.IGithubReleaseAssets]:
raise NotImplementedError

@property
Expand Down Expand Up @@ -90,11 +90,11 @@ class AGithubReleaseAssets(metaclass=abstracts.Abstraction):

def __init__(
self,
release: "interface.IGithubRelease") -> None:
release: interface.IGithubRelease) -> None:
self._release = release

@property
def release(self) -> "interface.IGithubRelease":
def release(self) -> interface.IGithubRelease:
return self._release

@property
Expand Down
28 changes: 14 additions & 14 deletions aio.api.github/aio/api/github/abstract/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pathlib
from datetime import datetime
from functools import cached_property, partial
from typing import Any, Type
from typing import Any

import gidgethub

Expand All @@ -20,15 +20,15 @@
class AGithubRepo(metaclass=abstracts.Abstraction):
"""A Github repo."""

def __init__(self, github: "interface.IGithubAPI", name: str) -> None:
def __init__(self, github: interface.IGithubAPI, name: str) -> None:
self._github = github
self._name = name

def __str__(self):
return f"<{self.__class__.__name__} {self.name}>"

@cached_property
def actions(self) -> "interface.IGithubActions":
def actions(self) -> interface.IGithubActions:
return self.github.actions_class(repo=self)

@cached_property
Expand All @@ -41,28 +41,28 @@ def github(self) -> interface.IGithubAPI:
return self._github

@cached_property
def issues(self) -> "interface.IGithubIssues":
def issues(self) -> interface.IGithubIssues:
return self.github.issues_class(
self.github,
repo=self)

@property
def labels(self) -> "interface.IGithubIterator":
def labels(self) -> interface.IGithubIterator:
return self.iter_entities(
self.github.label_class, "labels")

@property
def name(self) -> str:
return self._name

async def commit(self, name: str) -> "interface.IGithubCommit":
async def commit(self, name: str) -> interface.IGithubCommit:
return self.github.commit_class(
self,
await self.getitem(f"commits/{name}"))

def commits(
self,
since: datetime | None = None) -> "interface.IGithubIterator":
since: datetime | None = None) -> interface.IGithubIterator:
query = "commits"
if since is not None:
query = f"{query}?since={utils.dt_to_js_isoformat(since)}"
Expand Down Expand Up @@ -100,7 +100,7 @@ async def getitem(self, query: str) -> Any:
"""Call the `gidgethub.getitem` api for this repo."""
return await self.github.getitem(self.github_endpoint(query))

def getiter(self, query: str, **kwargs) -> "interface.IGithubIterator":
def getiter(self, query: str, **kwargs) -> interface.IGithubIterator:
"""Return a `GithubIterator` wrapping `gidgethub.getiter` for this
repo."""
return self.github.getiter(self.github_endpoint(query), **kwargs)
Expand Down Expand Up @@ -132,9 +132,9 @@ async def highest_release(

def iter_entities(
self,
entity: Type[base.GithubEntity],
entity: type[base.GithubEntity],
path: str,
**kwargs) -> "interface.IGithubIterator":
**kwargs) -> interface.IGithubIterator:
"""Iterate and inflate entities for provided type."""
return self.getiter(
path,
Expand All @@ -157,21 +157,21 @@ async def release(
self,
name: str,
data: dict | None = None,
dry_run: bool = False) -> "interface.IGithubRelease":
dry_run: bool = False) -> interface.IGithubRelease:
if data:
return await self.github.release_class.create(self, data, dry_run)
return self.github.release_class(
self,
await self.getitem(f"releases/tags/{name}"))

def releases(self) -> "interface.IGithubIterator":
def releases(self) -> interface.IGithubIterator:
"""Iterate releases for this repo."""
# TODO: make per_page configurable
return self.iter_entities(
self.github.release_class,
"releases?per_page=100")

async def tag(self, name: str) -> "interface.IGithubTag":
async def tag(self, name: str) -> interface.IGithubTag:
ref_tag = await self.getitem(f"git/ref/tags/{name}")
if ref_tag["object"]["type"] != "tag":
raise exceptions.TagNotFound(name)
Expand All @@ -186,7 +186,7 @@ async def tag_exists(self, tag_name: str) -> bool:
pass
return False

def tags(self) -> "interface.IGithubIterator":
def tags(self) -> interface.IGithubIterator:
"""Iterate tags for this repo."""
return self.iter_entities(
self.github.tag_class,
Expand Down
9 changes: 9 additions & 0 deletions aio.api.github/aio/api/github/abstract/stream/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""aio.core.stream."""

from .base import AsyncStream
from ._reader import reader, Reader
from ._writer import writer, Writer


__all__ = (
"AsyncStream", "Reader", "reader", "Writer", "writer")
Loading

0 comments on commit 408a6d2

Please sign in to comment.