Skip to content
Snippets Groups Projects
Verified Commit 9d37899e authored by Daniel Göbel's avatar Daniel Göbel
Browse files

Fix wrong typing of several methods

#42
parent 23f82987
No related branches found
No related tags found
No related merge requests found
......@@ -21,11 +21,11 @@ repos:
files: app
args: [--check]
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.0.283'
rev: 'v0.0.285'
hooks:
- id: ruff
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.4.1
rev: v1.5.1
hooks:
- id: mypy
files: app
......
......@@ -6,7 +6,7 @@ from tempfile import SpooledTemporaryFile
from typing import TYPE_CHECKING, Dict, List, Optional
from fastapi import HTTPException, status
from httpx import AsyncClient, Auth
from httpx import USE_CLIENT_DEFAULT, AsyncClient, Auth
from pydantic import AnyHttpUrl
if TYPE_CHECKING:
......@@ -94,7 +94,7 @@ class GitRepository(ABC):
@cached_property
@abstractmethod
def request_auth(self) -> Auth:
def request_auth(self) -> Optional[Auth]:
...
@cached_property
......@@ -127,7 +127,7 @@ class GitRepository(ABC):
"""
response = await client.head(
str(self.check_file_url(filepath)),
auth=self.request_auth,
auth=USE_CLIENT_DEFAULT if self.request_auth is None else self.request_auth,
follow_redirects=True,
headers=self.request_headers,
)
......@@ -193,7 +193,10 @@ class GitRepository(ABC):
Write the file into this stream in binary mode.
"""
async with client.stream(
"GET", str(await self.download_file_url(filepath, client)), auth=self.request_auth, follow_redirects=True
"GET",
str(await self.download_file_url(filepath, client)),
auth=USE_CLIENT_DEFAULT if self.request_auth is None else self.request_auth,
follow_redirects=True,
) as response:
async for chunk in response.aiter_bytes():
file_handle.write(chunk)
......@@ -31,11 +31,10 @@ class GitHubRepository(GitRepository):
return self.commit
@cached_property
def request_auth(self) -> BasicAuth:
if self.token is None:
return USE_CLIENT_DEFAULT
else:
def request_auth(self) -> Optional[BasicAuth]:
if self._token is not None:
return BasicAuth(username=self.user, password=self._token)
return None
@cached_property
def request_headers(self) -> Dict[str, str]:
......@@ -61,7 +60,9 @@ class GitHubRepository(GitRepository):
async def download_file_url(self, filepath: str, client: AsyncClient) -> AnyHttpUrl:
response = await client.get(
str(self.check_file_url(filepath)), auth=self.request_auth, headers=self.request_headers
str(self.check_file_url(filepath)),
auth=USE_CLIENT_DEFAULT if self.request_auth is None else self.request_auth,
headers=self.request_headers,
)
assert response.status_code == status.HTTP_200_OK
return AnyHttpUrl(response.json()["download_url"])
......
from functools import cached_property
from typing import Dict, Iterator, Optional
from typing import Dict, Generator, Optional
from urllib.parse import quote, urlparse
from httpx import USE_CLIENT_DEFAULT, AsyncClient, Auth, Request
from httpx import AsyncClient, Auth, Request, Response
from pydantic import AnyHttpUrl
from .abstract_repository import GitRepository
......@@ -12,7 +12,7 @@ class BearerAuth(Auth):
def __init__(self, token: str):
self.token = token
def auth_flow(self, request: Request) -> Iterator[Request]:
def auth_flow(self, request: Request) -> Generator[Request, Response, None]:
request.headers["Authorization"] = f"Bearer {self.token}"
yield request
......@@ -39,11 +39,10 @@ class GitlabRepository(GitRepository):
return self.commit
@cached_property
def request_auth(self) -> BearerAuth:
if self._token is None:
return USE_CLIENT_DEFAULT
else:
def request_auth(self) -> Optional[BearerAuth]:
if self._token is not None:
return BearerAuth(token=self._token)
return None
@cached_property
def request_headers(self) -> Dict[str, str]:
......
......@@ -2,12 +2,12 @@
pytest>=7.4.0,<7.5.0
pytest-asyncio>=0.21.0,<0.22.0
pytest-cov>=4.1.0,<4.2.0
coverage[toml]>=7.2.0,<7.3.0
coverage[toml]>=7.3.0,<7.4.0
# Linters
ruff
black>=23.07.0,<23.08.0
isort>=5.12.0,<5.13.0
mypy>=1.4.0,<1.5.0
mypy>=1.5.0,<1.6.0
# stubs for mypy
boto3-stubs-lite[s3]>=1.28.0,<1.29.0
types-requests
......
......@@ -4,7 +4,7 @@ clowmdb>=2.0.0,<2.1.0
# Webserver packages
anyio>=3.7.0,<3.8.0
fastapi>=0.101.0,<0.102.0
pydantic>=2.1.0,<2.2.0
pydantic>=2.2.0,<2.3.0
pydantic-settings
uvicorn>=0.23.0,<0.24.0
python-multipart
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment