当前位置: 首页>>代码示例>>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;未经允许,请勿转载。