mirror of
https://github.com/status-im/PyGithub.git
synced 2026-08-31 19:01:15 +00:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user