From 5fc5e54f475c46784a06db653f0d38450a09a5dd Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 23 May 2020 20:33:23 +0200 Subject: [PATCH] sources/oauth: fix typing errors # Conflicts: # passbook/sources/oauth/clients.py --- .github/workflows/ci.yml | 2 ++ passbook/sources/oauth/clients.py | 15 ++++++++++----- passbook/sources/oauth/views/core.py | 4 ++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3b76df232..dbb0e4d62 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -62,6 +62,8 @@ jobs: python-version: '3.8' - name: Install pyright run: npm install -g pyright + - name: Show pyright version + run: pyright --version - name: Install dependencies run: sudo pip install -U wheel pipenv && pipenv install --dev - name: Lint with pyright diff --git a/passbook/sources/oauth/clients.py b/passbook/sources/oauth/clients.py index 06a9310fc..35c58d7ba 100644 --- a/passbook/sources/oauth/clients.py +++ b/passbook/sources/oauth/clients.py @@ -1,6 +1,6 @@ """OAuth Clients""" import json -from typing import Dict, Optional +from typing import TYPE_CHECKING, Any, Dict, Optional from urllib.parse import parse_qs, urlencode from django.http import HttpRequest @@ -14,24 +14,29 @@ from structlog import get_logger from passbook import __version__ LOGGER = get_logger() +if TYPE_CHECKING: + from passbook.sources.oauth.models import OAuthSource class BaseOAuthClient: """Base OAuth Client""" session: Session + source: "OAuthSource" - def __init__(self, source, token=""): # nosec + def __init__(self, source: "OAuthSource", token=""): # nosec self.source = source self.token = token self.session = Session() self.session.headers.update({"User-Agent": "passbook %s" % __version__}) - def get_access_token(self, request, callback=None): + def get_access_token( + self, request: HttpRequest, callback=None + ) -> Optional[Dict[str, Any]]: "Fetch access token from callback request." raise NotImplementedError("Defined in a sub-class") # pragma: no cover - def get_profile_info(self, token: Dict[str, str]): + def get_profile_info(self, token: Dict[str, str]) -> Optional[Dict[str, Any]]: "Fetch user profile information." try: headers = { @@ -45,7 +50,7 @@ class BaseOAuthClient: LOGGER.warning("Unable to fetch user profile", exc=exc) return None else: - return response.json() or response.text + return response.json() def get_redirect_args(self, request, callback) -> Dict[str, str]: "Get request parameters for redirect url." diff --git a/passbook/sources/oauth/views/core.py b/passbook/sources/oauth/views/core.py index 7e3249bc7..9166ad3b6 100644 --- a/passbook/sources/oauth/views/core.py +++ b/passbook/sources/oauth/views/core.py @@ -21,7 +21,7 @@ from passbook.flows.planner import ( ) from passbook.flows.views import SESSION_KEY_PLAN from passbook.lib.utils.urls import redirect_with_qs -from passbook.sources.oauth.clients import get_client +from passbook.sources.oauth.clients import BaseOAuthClient, get_client from passbook.sources.oauth.models import OAuthSource, UserOAuthSourceConnection from passbook.stages.password.stage import PLAN_CONTEXT_AUTHENTICATION_BACKEND @@ -34,7 +34,7 @@ class OAuthClientMixin: client_class: Optional[Callable] = None - def get_client(self, source): + def get_client(self, source: OAuthSource) -> BaseOAuthClient: "Get instance of the OAuth client for this source." if self.client_class is not None: # pylint: disable=not-callable