Merge Branch type stubs back to source (#2571)

Introduces new helper methods `is_defined`, `is_undefined`, `is_optional`, `is_optional_list` that is going to simplify input assertions in all our methods.

Co-authored-by: Enrico Minack <github@enrico.minack.dev>
This commit is contained in:
Trim21
2023-07-04 09:42:53 +02:00
committed by GitHub
co-authored by Enrico Minack
parent 7324110249
commit c5682489d9
6 changed files with 190 additions and 323 deletions
+21
View File
@@ -54,6 +54,7 @@ from typing import (
)
from dateutil import parser
from typing_extensions import TypeGuard
from . import Consts
from .GithubException import BadAttributeException, IncompletableObject
@@ -92,6 +93,26 @@ NotSet = _NotSetType()
Opt = Union[T, _NotSetType]
def is_defined(v: Union[T, _NotSetType]) -> TypeGuard[T]:
return not isinstance(v, _NotSetType)
def is_undefined(v: Any) -> TypeGuard[_NotSetType]:
return isinstance(v, _NotSetType)
def is_optional(v, type: Type[T]) -> TypeGuard[Opt[T]]:
return isinstance(v, _NotSetType) or isinstance(v, type)
def is_optional_list(v, type: Type[T]) -> TypeGuard[Opt[List[T]]]:
return (
isinstance(v, _NotSetType)
or isinstance(v, list)
and all(isinstance(element, type) for element in v)
)
class _ValuedAttribute(Attribute, Generic[T]):
def __init__(self, value: T):
self._value = value