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