當前位置: 首頁>>代碼示例>>Python>>正文


Python typing.get_args方法代碼示例

本文整理匯總了Python中typing.get_args方法的典型用法代碼示例。如果您正苦於以下問題:Python typing.get_args方法的具體用法?Python typing.get_args怎麽用?Python typing.get_args使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在typing的用法示例。


在下文中一共展示了typing.get_args方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_section_configuration_matches_type_specification

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import get_args [as 別名]
def test_section_configuration_matches_type_specification(self):
        """"The section annotations should match the actual types of the sections."""

        sections = (
            cls
            for (name, cls) in inspect.getmembers(constants)
            if hasattr(cls, 'section') and isinstance(cls, type)
        )
        for section in sections:
            for name, annotation in section.__annotations__.items():
                with self.subTest(section=section.__name__, name=name, annotation=annotation):
                    value = getattr(section, name)
                    origin = typing.get_origin(annotation)
                    annotation_args = typing.get_args(annotation)
                    failure_msg = f"{value} is not an instance of {annotation}"

                    if origin is typing.Union:
                        is_instance = is_any_instance(value, annotation_args)
                        self.assertTrue(is_instance, failure_msg)
                    else:
                        is_instance = is_annotation_instance(value, annotation)
                        self.assertTrue(is_instance, failure_msg) 
開發者ID:python-discord,項目名稱:bot,代碼行數:24,代碼來源:test_constants.py

示例2: get_args

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import get_args [as 別名]
def get_args(t):
        return getattr(t, "__args__", None) 
開發者ID:intel,項目名稱:dffml,代碼行數:4,代碼來源:data.py

示例3: get_subtypes

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import get_args [as 別名]
def get_subtypes(
      type: Optional[Type]) -> Tuple[Optional[Type], Optional[Type]]:
    """For Dict hints, returns (keytype, valuetype).

    For List hints, returns (None, valuetype).

    For everything else, returns (None, None)."""

    # In Python 3.8+, you can use typing.get_args.  It returns () if "type"
    # is something like "str" or "int" instead of "typing.List" or
    # "typing.Dict".
    if hasattr(typing, 'get_args'):
      args = typing.get_args(type)  # type: ignore
    elif hasattr(type, '__args__'):
      # Before Python 3.8, you can use this undocumented attribute to get the
      # type parameters.  If this doesn't exist, you are probably dealing with a
      # basic type like "str" or "int".
      args = type.__args__  # type: ignore
    else:
      args = ()

    underlying = Field.get_underlying_type(type)
    if underlying is dict:
      return args
    if underlying is list:
      return (None, args[0])
    return (None, None) 
開發者ID:google,項目名稱:shaka-streamer,代碼行數:29,代碼來源:configuration.py

示例4: get_args

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import get_args [as 別名]
def get_args(cls) -> Tuple:
        if typing_inspect.is_union_type(cls):
            try:
                return cls.__union_params__
            except AttributeError:
                pass
            return cls.__args__
        elif issubclass(cls, List):
            return cls.__args__
        else:
            raise ValueError("Cannot get type arguments for {}".format(cls)) 
開發者ID:stanfordnqp,項目名稱:spins-b,代碼行數:13,代碼來源:schema.py

示例5: _ti_get_args

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import get_args [as 別名]
def _ti_get_args(tp):
        import typing_inspect as ti
        if type(tp) is type(Literal):  # Py<=3.6.
            return tp.__values__
        return ti.get_args(tp, evaluate=True)  # evaluate=True default on Py>=3.7. 
開發者ID:anntzer,項目名稱:defopt,代碼行數:7,代碼來源:defopt.py


注:本文中的typing.get_args方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。