Skip to content

Commit f22ae56

Browse files
committed
style: fix docstring and complexity violations in auth.py
Extract _build_redis_client() helper to reduce build_token_store() cyclomatic complexity below the limit of 8. Fix D212 and D205 docstring violations in build_token_store() and get_oauth_verifier().
1 parent 2edcb3a commit f22ae56

1 file changed

Lines changed: 21 additions & 16 deletions

File tree

src/mcp_github/auth.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,23 @@ async def get_client(self, client_id: str) -> OAuthClientInformationFull | None:
9797
return None
9898

9999

100+
def _build_redis_client(host_port: str) -> AsyncRedis:
101+
"""Build an AsyncRedis client from a host:port string or Redis URI."""
102+
uri = host_port if "://" in host_port else f"redis://{host_port}"
103+
parsed = urlparse(uri)
104+
db_path = parsed.path.lstrip("/")
105+
return AsyncRedis(
106+
host=parsed.hostname or "localhost",
107+
port=parsed.port or 6379,
108+
db=int(db_path) if db_path.isdigit() else 0,
109+
password=parsed.password or REDIS_PASSWORD or None,
110+
ssl=parsed.scheme == "rediss",
111+
decode_responses=True,
112+
)
113+
114+
100115
def build_token_store() -> AsyncKeyValue:
101-
"""
102-
Return a token store for OAuth state.
116+
"""Return a token store for OAuth state.
103117
104118
When REDIS_HOST_PORT is set, returns a RedisStore whose collection names are
105119
prefixed with a 12-char SHA-256 hash of GITHUB_OAUTH_BASE_URL. Two server
@@ -115,20 +129,10 @@ def build_token_store() -> AsyncKeyValue:
115129
rediss://[:<password>@]<host>:<port>[/<db>] — TLS
116130
REDIS_PASSWORD is used as a fallback when not embedded in the URI.
117131
The database defaults to 0 when not specified in the URI.
132+
118133
"""
119134
if REDIS_HOST_PORT:
120-
uri = REDIS_HOST_PORT if "://" in REDIS_HOST_PORT else f"redis://{REDIS_HOST_PORT}"
121-
parsed = urlparse(uri)
122-
ssl = parsed.scheme == "rediss"
123-
host = parsed.hostname or "localhost"
124-
port = parsed.port or 6379
125-
_db_path = parsed.path.lstrip("/")
126-
db = int(_db_path) if _db_path.isdigit() else 0
127-
password = parsed.password or REDIS_PASSWORD or None
128-
129-
client = AsyncRedis(host=host, port=port, db=db, password=password, ssl=ssl, decode_responses=True)
130-
store: AsyncKeyValue = RedisStore(client=client)
131-
135+
store: AsyncKeyValue = RedisStore(client=_build_redis_client(REDIS_HOST_PORT))
132136
if GITHUB_OAUTH_BASE_URL:
133137
prefix = hashlib.sha256(GITHUB_OAUTH_BASE_URL.encode()).hexdigest()[:12]
134138
return PrefixCollectionsWrapper(store, prefix=prefix)
@@ -137,10 +141,11 @@ def build_token_store() -> AsyncKeyValue:
137141

138142

139143
def get_oauth_verifier() -> _PermissiveGitHubProvider:
140-
"""
141-
Return a PermissiveGitHubProvider instance for OAuth2 authentication.
144+
"""Return a PermissiveGitHubProvider instance for OAuth2 authentication.
145+
142146
Requires GITHUB_OAUTH_CLIENT_ID, GITHUB_OAUTH_CLIENT_SECRET, and
143147
GITHUB_OAUTH_BASE_URL to be set.
148+
144149
"""
145150
if not all((GITHUB_OAUTH_CLIENT_ID, GITHUB_OAUTH_CLIENT_SECRET, GITHUB_OAUTH_BASE_URL)):
146151
raise ValueError(

0 commit comments

Comments
 (0)