Skip to content

Commit

Permalink
chore: more lazy importing
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Feb 11, 2025
1 parent e50f6f4 commit ef92ec4
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/githarbor/providers/gitearepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from typing import TYPE_CHECKING, Any, ClassVar, Literal
from urllib.parse import urlparse

import giteapy
from giteapy.rest import ApiException
import upath

from githarbor.core.base import BaseRepository
Expand All @@ -18,6 +16,8 @@
from collections.abc import Iterator
from datetime import datetime

import giteapy

from githarbor.core.models import (
Branch,
Commit,
Expand Down Expand Up @@ -46,6 +46,9 @@ def __init__(
token: str | None = None,
url: str = "https://gitea.com",
):
import giteapy
from giteapy.rest import ApiException

t = token or os.getenv("GITEA_TOKEN")
if not t:
msg = "Gitea token is required"
Expand Down
8 changes: 5 additions & 3 deletions src/githarbor/providers/githubrepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
from typing import TYPE_CHECKING, Any, ClassVar, Literal
from urllib.parse import urlparse

from github import Auth, Github, NamedUser
from github.GithubException import GithubException

from githarbor.core.base import BaseRepository
from githarbor.exceptions import AuthenticationError, ResourceNotFoundError
from githarbor.providers import githubtools
Expand Down Expand Up @@ -46,6 +43,9 @@ class GitHubRepository(BaseRepository):

def __init__(self, owner: str, name: str, token: str | None = None):
"""Initialize GitHub repository."""
from github import Auth, Github, NamedUser
from github.GithubException import GithubException

try:
t = token or TOKEN
if t is None:
Expand Down Expand Up @@ -310,6 +310,8 @@ def get_release(self, tag: str) -> Release:
@githubtools.handle_github_errors("Failed to get tag {name}")
def get_tag(self, name: str) -> Tag:
"""Get a specific tag by name."""
from github.GithubException import GithubException

try:
tag = self._repo.get_git_ref(f"tags/{name}")
tag_obj = self._repo.get_git_tag(tag.object.sha)
Expand Down
9 changes: 5 additions & 4 deletions src/githarbor/providers/gitlabrepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
from typing import TYPE_CHECKING, Any, ClassVar, Literal
from urllib.parse import urlparse

import gitlab
from gitlab.exceptions import GitlabAuthenticationError

from githarbor.core.base import BaseRepository
from githarbor.core.models import (
Branch,
Expand Down Expand Up @@ -43,6 +40,9 @@ def __init__(
token: str | None = None,
url: str = "https://gitlab.com",
):
import gitlab
from gitlab.exceptions import GitlabAuthenticationError

try:
t = token or os.getenv("GITLAB_TOKEN")
if not t:
Expand Down Expand Up @@ -258,7 +258,8 @@ def get_contributors(

@gitlabtools.handle_gitlab_errors("Failed to get languages")
def get_languages(self) -> dict[str, int]:
return self._repo.languages()
response = self._repo.languages()
return response if isinstance(response, dict) else response.json()

@gitlabtools.handle_gitlab_errors("Failed to compare branches {base} and {head}")
def compare_branches(
Expand Down
4 changes: 2 additions & 2 deletions src/githarbor/providers/localrepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import pathlib
from typing import TYPE_CHECKING, Any, ClassVar

import git

from githarbor.core.base import BaseRepository
from githarbor.exceptions import ResourceNotFoundError
from githarbor.providers import localtools
Expand All @@ -29,6 +27,8 @@ class LocalRepository(BaseRepository):
url_patterns: ClassVar[list[str]] = [] # Local repos don't have URL patterns

def __init__(self, path: str | os.PathLike[str]) -> None:
import git

try:
self.path = pathlib.Path(path)
self.repo = git.Repo(self.path)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

@pytest.fixture
def mock_github():
with patch("githarbor.providers.githubrepository.Github") as mock:
with patch("github.Github") as mock:
yield mock


Expand Down
2 changes: 1 addition & 1 deletion tests/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def mock_gitlab_client():

@pytest.fixture
def gitlab_repo(mock_gitlab_client) -> GitLabRepository:
with patch("githarbor.providers.gitlabrepository.gitlab.Gitlab") as mock_gitlab:
with patch("gitlab.Gitlab") as mock_gitlab:
mock_gitlab.return_value = mock_gitlab_client
provider = GitLabRepository("phil65", "test")
provider._gl = mock_gitlab_client
Expand Down

0 comments on commit ef92ec4

Please sign in to comment.