Coverage for src/bioimageio/spec/_internal/field_validation.py: 74%
43 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 16:20 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 16:20 +0000
1from __future__ import annotations
3from collections.abc import Hashable, Mapping, Sequence
4from datetime import date, datetime
5from typing import (
6 Any,
7 TypeGuard,
8)
10import httpx
12from ._settings import settings
13from .constants import KNOWN_GITHUB_USERS, KNOWN_INVALID_GITHUB_USERS
14from .field_warning import issue_warning
15from .io import YamlKey, YamlLeafValue, YamlValue
16from .type_guards import is_mapping, is_sequence, is_tuple
17from .validation_context import get_validation_context
20def is_valid_yaml_leaf_value(
21 value: Any,
22) -> TypeGuard[YamlLeafValue]:
23 return value is None or isinstance(value, (bool, date, datetime, int, float, str))
26def is_valid_yaml_key(value: Any | Sequence[Any]) -> TypeGuard[YamlKey]:
27 return (
28 is_valid_yaml_leaf_value(value)
29 or is_tuple(value)
30 and all(is_valid_yaml_leaf_value(v) for v in value)
31 )
34def is_valid_yaml_mapping(value: Any) -> TypeGuard[Mapping[YamlKey, YamlValue]]:
35 return is_mapping(value) and all(
36 is_valid_yaml_key(k) and is_valid_yaml_value(v) for k, v in value.items()
37 )
40def is_valid_yaml_sequence(value: Any) -> TypeGuard[Sequence[YamlValue]]:
41 return is_sequence(value) and all(is_valid_yaml_value(v) for v in value)
44def is_valid_yaml_value(value: Any) -> TypeGuard[YamlValue]:
45 return any(
46 is_valid(value)
47 for is_valid in (
48 is_valid_yaml_key,
49 is_valid_yaml_mapping,
50 is_valid_yaml_sequence,
51 )
52 )
55def validate_unique_entries(seq: Sequence[Hashable]):
56 if len(seq) != len(set(seq)):
57 raise ValueError("Entries are not unique.")
58 return seq
61def validate_github_user(
62 username: str, hotfix_known_errorenous_names: bool = True
63) -> str:
64 if hotfix_known_errorenous_names and username == "Constantin Pape":
65 return "constantinpape"
67 if (
68 username.lower() in KNOWN_GITHUB_USERS
69 or not get_validation_context().perform_io_checks
70 ):
71 return username
73 if username.lower() in KNOWN_INVALID_GITHUB_USERS:
74 raise ValueError(f"Known invalid GitHub user '{username}'")
76 try:
77 r = httpx.get(
78 f"https://api.github.com/users/{username}",
79 auth=settings.github_auth,
80 timeout=settings.http_timeout,
81 )
82 except httpx.TimeoutException:
83 issue_warning(
84 "Could not verify GitHub user '{value}' due to connection timeout",
85 value=username,
86 )
87 else:
88 if r.status_code == 403 and r.reason_phrase == "rate limit exceeded":
89 issue_warning(
90 "Could not verify GitHub user '{value}' due to GitHub API rate limit",
91 value=username,
92 )
93 elif r.status_code != 200:
94 KNOWN_INVALID_GITHUB_USERS.add(username.lower())
95 raise ValueError(f"Could not find GitHub user '{username}'")
97 KNOWN_GITHUB_USERS.add(username.lower())
99 return username