Coverage for bioimageio/spec/_internal/field_validation.py: 74%
42 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-18 12:47 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-18 12:47 +0000
1from __future__ import annotations
3from datetime import date, datetime
4from typing import (
5 Any,
6 Hashable,
7 Mapping,
8 Sequence,
9 Union,
10)
12import httpx
14from ._settings import settings
15from .constants import KNOWN_GITHUB_USERS, KNOWN_INVALID_GITHUB_USERS
16from .field_warning import issue_warning
17from .type_guards import is_mapping, is_sequence, is_tuple
18from .validation_context import get_validation_context
21def is_valid_yaml_leaf_value(value: Any) -> bool:
22 return value is None or isinstance(value, (bool, date, datetime, int, float, str))
25def is_valid_yaml_key(value: Union[Any, Sequence[Any]]) -> bool:
26 return (
27 is_valid_yaml_leaf_value(value)
28 or is_tuple(value)
29 and all(is_valid_yaml_leaf_value(v) for v in value)
30 )
33def is_valid_yaml_mapping(value: Union[Any, Mapping[Any, Any]]) -> bool:
34 return is_mapping(value) and all(
35 is_valid_yaml_key(k) and is_valid_yaml_value(v) for k, v in value.items()
36 )
39def is_valid_yaml_sequence(value: Union[Any, Sequence[Any]]) -> bool:
40 return is_sequence(value) and all(is_valid_yaml_value(v) for v in value)
43def is_valid_yaml_value(value: Any) -> bool:
44 return any(
45 is_valid(value)
46 for is_valid in (
47 is_valid_yaml_key,
48 is_valid_yaml_mapping,
49 is_valid_yaml_sequence,
50 )
51 )
54def validate_unique_entries(seq: Sequence[Hashable]):
55 if len(seq) != len(set(seq)):
56 raise ValueError("Entries are not unique.")
57 return seq
60def validate_github_user(
61 username: str, hotfix_known_errorenous_names: bool = True
62) -> str:
63 if hotfix_known_errorenous_names:
64 if 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=3,
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