def get_args( tp: TypeHint, *, globalns: Namespace = None, localns: Namespace = None ) -> Tuple[TypeHint, ...]: if globalns is not None or localns is not None: return typing.get_args(typing._eval_type(tp, globalns, localns)) return typing.get_args(tp)
def _process_value(self, cls: type, fieldpath: str, anntype: Any, value: Any, ioattrs: Optional[IOAttrs]) -> Any: # pylint: disable=too-many-return-statements # pylint: disable=too-many-branches # pylint: disable=too-many-statements origin = _get_origin(anntype) if origin is typing.Any: if not _is_valid_for_codec(value, self._codec): raise TypeError( f'Invalid value type for \'{fieldpath}\';' f" 'Any' typed values must contain types directly" f' supported by the specified codec ({self._codec.name});' f' found \'{type(value).__name__}\' which is not.') return value if self._create else None if origin is typing.Union: # Currently, the only unions we support are None/Value # (translated from Optional), which we verified on prep. # So let's treat this as a simple optional case. if value is None: return None childanntypes_l = [ c for c in typing.get_args(anntype) if c is not type(None) ] # noqa (pycodestyle complains about *is* with type) assert len(childanntypes_l) == 1 return self._process_value(cls, fieldpath, childanntypes_l[0], value, ioattrs) # Everything below this point assumes the annotation type resolves # to a concrete type. (This should have been verified at prep time). assert isinstance(origin, type) # For simple flat types, look for exact matches: if origin in SIMPLE_TYPES: if type(value) is not origin: # Special case: if they want to coerce ints to floats, do so. if (self._coerce_to_float and origin is float and type(value) is int): return float(value) if self._create else None _raise_type_error(fieldpath, type(value), (origin, )) return value if self._create else None if origin is tuple: if not isinstance(value, tuple): raise TypeError(f'Expected a tuple for {fieldpath};' f' found a {type(value)}') childanntypes = typing.get_args(anntype) # We should have verified this was non-zero at prep-time assert childanntypes if len(value) != len(childanntypes): raise TypeError(f'Tuple at {fieldpath} contains' f' {len(value)} values; type specifies' f' {len(childanntypes)}.') if self._create: return [ self._process_value(cls, fieldpath, childanntypes[i], x, ioattrs) for i, x in enumerate(value) ] for i, x in enumerate(value): self._process_value(cls, fieldpath, childanntypes[i], x, ioattrs) return None if origin is list: if not isinstance(value, list): raise TypeError(f'Expected a list for {fieldpath};' f' found a {type(value)}') childanntypes = typing.get_args(anntype) # 'Any' type children; make sure they are valid values for # the specified codec. if len(childanntypes) == 0 or childanntypes[0] is typing.Any: for i, child in enumerate(value): if not _is_valid_for_codec(child, self._codec): raise TypeError( f'Item {i} of {fieldpath} contains' f' data type(s) not supported by the specified' f' codec ({self._codec.name}).') # Hmm; should we do a copy here? return value if self._create else None # We contain elements of some specified type. assert len(childanntypes) == 1 if self._create: return [ self._process_value(cls, fieldpath, childanntypes[0], x, ioattrs) for x in value ] for x in value: self._process_value(cls, fieldpath, childanntypes[0], x, ioattrs) return None if origin is set: if not isinstance(value, set): raise TypeError(f'Expected a set for {fieldpath};' f' found a {type(value)}') childanntypes = typing.get_args(anntype) # 'Any' type children; make sure they are valid Any values. if len(childanntypes) == 0 or childanntypes[0] is typing.Any: for child in value: if not _is_valid_for_codec(child, self._codec): raise TypeError( f'Set at {fieldpath} contains' f' data type(s) not supported by the' f' specified codec ({self._codec.name}).') return list(value) if self._create else None # We contain elements of some specified type. assert len(childanntypes) == 1 if self._create: # Note: we output json-friendly values so this becomes # a list. return [ self._process_value(cls, fieldpath, childanntypes[0], x, ioattrs) for x in value ] for x in value: self._process_value(cls, fieldpath, childanntypes[0], x, ioattrs) return None if origin is dict: return self._process_dict(cls, fieldpath, anntype, value, ioattrs) if dataclasses.is_dataclass(origin): if not isinstance(value, origin): raise TypeError(f'Expected a {origin} for {fieldpath};' f' found a {type(value)}.') return self._process_dataclass(cls, value, fieldpath) if issubclass(origin, Enum): if not isinstance(value, origin): raise TypeError(f'Expected a {origin} for {fieldpath};' f' found a {type(value)}.') # At prep-time we verified that these enums had valid value # types, so we can blindly return it here. return value.value if self._create else None if issubclass(origin, datetime.datetime): if not isinstance(value, origin): raise TypeError(f'Expected a {origin} for {fieldpath};' f' found a {type(value)}.') check_utc(value) if ioattrs is not None: ioattrs.validate_datetime(value, fieldpath) if self._codec is Codec.FIRESTORE: return value assert self._codec is Codec.JSON return [ value.year, value.month, value.day, value.hour, value.minute, value.second, value.microsecond ] if self._create else None if origin is bytes: return self._process_bytes(cls, fieldpath, value) raise TypeError( f"Field '{fieldpath}' of type '{anntype}' is unsupported here.")
def get_generic_type(base_type, generic_base_type): # type: (t.Type, t.Type[TType]) -> t.Optional[t.Type[TType]] """Return the generic type arg derived from the generic_base_type type that is associated with the base_type type, if any, otherwise return None.""" # noinspection PyUnresolvedReferences type_arg = t.get_args(base_type.__orig_bases__[0])[0] return None if isinstance(type_arg, generic_base_type) else type_arg
overload, ) # Type Aliases AnyClass = TypeVar("AnyClass", bound=type) TypeHint = Union[ # pylint: disable=unsubscriptable-object Type[type], Type[Any], Type[TypeVar], # Type[Generic], Type[Annotated], Type[Tuple], Type[Callable], ] Namespace = dict[str, Any] TypeT = TypeVar("TypeT", *typing.get_args(TypeHint)) U = TypeVar("U") V = TypeVar("V") class Virtual: """ Mixin class to make a class non-heritable and non-instanciable """ __slots__ = ("__weakref__",) def __init__(self): raise TypeError("Cannot instanciate virtual class") def __init_subclass__(cls, *args, **kwargs):
def test_known_valid_modes_text() -> None: assert sorted("".join(sorted(mode.replace("t", ""))) for mode in get_args(_XZModesTextType)) == sorted( mode for mode in VALID_MODES if "b" not in mode)
def __init__(self): parser = argparse.ArgumentParser(description=( 'A powerful tool to concurrently clone or pull user and org repos and gists to create a GitHub archive.' )) parser.add_argument( '-t', '--token', type=str, required=False, default=None, help= ('Provide your GitHub token to authenticate with the GitHub API and gain access to private repos and' ' gists.'), ) parser.add_argument( '-u', '--users', type=str, required=False, default=None, help='Pass a comma separated list of users to get repos for.', ) parser.add_argument( '-o', '--orgs', type=str, required=False, default=None, help='Pass a comma separated list of orgs to get repos for.', ) parser.add_argument( '-g', '--gists', type=str, required=False, default=None, help='Pass a comma separated list of users to get gists for.', ) parser.add_argument( '-s', '--stars', type=str, required=False, default=None, help= 'Pass a comma separated list of users to get starred repos for.', ) parser.add_argument( '-v', '--view', action='store_true', required=False, default=False, help='Pass this flag to view git assets (dry run).', ) parser.add_argument( '-c', '--clone', action='store_true', required=False, default=False, help='Pass this flag to clone git assets.', ) parser.add_argument( '-p', '--pull', action='store_true', required=False, default=False, help='Pass this flag to pull git assets.', ) parser.add_argument( '-f', '--forks', action='store_true', required=False, default=False, help='Pass this flag to include forked git assets.', ) parser.add_argument( '-i', '--include', type=str, required=False, default=None, help= 'Pass a comma separated list of repos to filter what is included in the Archive.', ) parser.add_argument( '-e', '--exclude', type=str, required=False, default=None, help= 'Pass a comma separated list of repos to filter what is excluded from the Archive.', ) parser.add_argument( '-l', '--location', type=str, required=False, default=DEFAULT_LOCATION, help= 'The location where you want your GitHub Archive to be stored.', ) parser.add_argument( '-ht', '--https', action='store_true', required=False, default=False, help='Use HTTPS URLs instead of SSH.', ) parser.add_argument( '-to', '--timeout', type=int, required=False, default=DEFAULT_TIMEOUT, help='The number of seconds before a git operation times out.', ) parser.add_argument( '-th', '--threads', type=int, required=False, default=DEFAULT_NUM_THREADS, help='The number of concurrent threads to run.', ) parser.add_argument( '--base_url', type=str, required=False, default=DEFAULT_BASE_URL, help= 'The base URL of your GitHub instance (useful for enterprise users with custom hostnames).', ) parser.add_argument( '--log_level', type=str, required=False, default=DEFAULT_LOG_LEVEL, choices=set(get_args(LOG_LEVEL_CHOICES)), help='The log level used for the tool.', ) parser.parse_args(namespace=self)
def build(self) -> None: for suit in get_args(Suit): for rank in get_args(Rank): self.cards.append(Card(suit, rank))
def test_known_valid_modes_binary() -> None: assert sorted( "".join(sorted(mode)) for mode in get_args(_XZModesBinaryType)) == sorted(VALID_MODES)
def is_optional(field_type): # field must be a Union with 2 options, one of which is None return (get_origin(field_type) == Union and len(get_args(field_type)) == 2 and get_args(field_type)[1] == type(None))
pair="btcfxjpy"): url = f"https://api.cryptowat.ch/markets/{market}/{pair}/ohlc" params = {"periods": periods, "after": after} headers = {} if cryptowat_api_public: headers["X-CW-API-Key"] = cryptowat_api_public response = requests.get(url, params=params, headers=headers, timeout=5) response.raise_for_status() data = response.json() return list(data["result"][str(periods)]), data["allowance"] PERIOD = Literal[60, 180, 300, 900, 1800, 3600, 7200, 14400, 21600, 43200, 86400, 259200, 604800] period_list = get_args(PERIOD) def get_ohlc(periods, data_size, market='bitflyer', pair="btcfxjpy"): assert periods in period_list, 'invalid periods arg' after = int(datetime.now().timestamp() - (periods * 5000)) while True: try: data, allo = cryptowat_request(periods, after, market, pair) return np.array(data), allo except requests.exceptions.RequestException as e: print("Cryptowatchの価格取得でエラー発生 : ", e) print("10秒待機してやり直します") time.sleep(10) except BaseException as e:
def typing_get_args(annotation): if sys.version_info >= (3, 8): return typing.get_args(annotation) else: return annotation.__args__
Decorate a class as a resource containing operations and/or subordinate resources. Parameters: • tag: tag to group resources [resource class name] """ if wrapped is None: return functools.partial(resource, tag=tag) wrapped._fondat_resource = types.SimpleNamespace( tag=tag or wrapped.__name__) return wrapped Method = Literal["get", "put", "post", "delete", "patch"] _methods = set(get_args(Method)) @validate_arguments def operation( wrapped=None, *, method: Optional[Method] = None, type: Optional[Literal["query", "mutation"]] = None, policies: Optional[Iterable[Policy]] = None, publish: bool = True, deprecated: bool = False, ): """ Decorate a resource coroutine as an operation.
def args(self) -> List[SakArg]: if self._args: return self._args if self._wrapped_content: d = self._wrapped_content if callable(d) and not isinstance(d, SakCmd): d_list = [d] if hasattr(d, "__call__"): # TODO(witt): Fix this ignore? d_list.append(d.__call__) # type: ignore _params = {} parsed_docs = None docstring = inspect.getdoc(d) if docstring: docs = inspect.cleandoc(docstring) parsed_docs = docstring_parser.parse(docs) for _d in d_list: # Instrospect the function signature signature = inspect.signature(_d) for param_name, param in signature.parameters.items(): if param_name.startswith("_sak_"): continue if str(param.kind) != "POSITIONAL_OR_KEYWORD": continue if param_name not in _params: _helpmsg = "" if parsed_docs is not None: for _doc_param in parsed_docs.params: if _doc_param.arg_name == param_name: _helpmsg = _doc_param.description or "" _params[param_name] = SakArg(name=param_name, helpmsg=_helpmsg) # TODO(witt): Fix this ignore? if param.default is not inspect._empty: # type: ignore _params[param_name].vargs[ "default"] = param.default _params[param_name].vargs["required"] = False else: _params[param_name].vargs["required"] = True # TODO(witt): Fix this ignore? if param.annotation is not inspect._empty: # type: ignore _params[param_name].vargs[ "type"] = param.annotation _type = _params[param_name].vargs.get("type", None) _default = _params[param_name].vargs.get( "default", None) if _type is bool or isinstance(_default, bool): _params[param_name].vargs["action"] = "store_true" if _default is True: _params[param_name].vargs[ "action"] = "store_false" if _type is list or isinstance(_default, list): if (_params[param_name].vargs.get("default", None) is not None): _params[param_name].vargs["nargs"] = "*" else: _params[param_name].vargs["nargs"] = "+" if get_origin(_type) is list: if (_params[param_name].vargs.get("default", None) is not None): _params[param_name].vargs["nargs"] = "*" else: _params[param_name].vargs["nargs"] = "+" # TODO(witt): What to do when we have more then one argument? _params[param_name].vargs["type"] = get_args( _type)[0] if len(get_args(_type)) > 1: raise Exception( "Sak does not support multiple type annotation?!" ) if get_origin(_type) is Union and type( None) in get_args(_type): # TODO(witt): What to do when we have more then one argument? _params[param_name].vargs["type"] = [ x for x in get_args(_type) if x is not type(None) # noqa: E721 ][0] # Check if there are decorators and override the info from the decorator. if hasattr(_d, "_sak_dec_chain"): chain = _d._sak_dec_chain # type: ignore while chain is not None: if isinstance(chain, SakArg): if chain.name not in _params: _params[chain.name] = chain if chain.helpmsg: _params[chain.name].helpmsg = chain.helpmsg if chain.short_name: _params[chain. name].short_name = chain.short_name if chain.completercb is not None: _params[ chain. name].completercb = chain.completercb vargs = dict(chain.vargs) if "type_init" in vargs: orig_type = _params[chain.name].vargs.get( "type", None) type_init = vargs.pop("type_init") vargs["type"] = type_init _params[chain.name].orig_type = orig_type _params[chain.name].vargs.update(vargs) if "default" in chain.vargs: if "type" not in _params[chain.name].vargs: _params[ chain.name].vargs["type"] = type( chain.vargs["default"]) if hasattr(chain._sak_func, "_sak_dec_chain"): chain = chain._sak_func._sak_dec_chain else: chain = None # If there is any parameter then return it. if _params: return list(_params.values()) return []