Exemple #1
0
def test_type_name_literal():
    if PY_38_MIN:
        module_name = "typing"
    else:
        module_name = "typing_extensions"
    assert type_name(typing_extensions.Literal[
        1, "a", b"\x00", True, False, None, MyEnum.a, MyStrEnum.a, MyIntEnum.a,
        MyFlag.a, MyIntFlag.a, typing_extensions.Literal[2, 3],
        typing_extensions.Literal[typing_extensions.Literal["b", "c"]], ]) == (
            f"{module_name}.Literal[1, 'a', b'\\x00', True, False, None, "
            "tests.entities.MyEnum.a, tests.entities.MyStrEnum.a, "
            "tests.entities.MyIntEnum.a, tests.entities.MyFlag.a, "
            "tests.entities.MyIntFlag.a, 2, 3, 'b', 'c']")
Exemple #2
0
def test_type_name_pep_585_short():
    assert type_name(list[str], short=True) == "list[str]"
    assert type_name(collections.deque[str], short=True) == "deque[str]"
    assert type_name(tuple[str], short=True) == "tuple[str]"
    assert type_name(set[str], short=True) == "set[str]"
    assert type_name(frozenset[str], short=True) == "frozenset[str]"
    assert type_name(collections.abc.Set[str], short=True) == "Set[str]"
    assert (type_name(collections.abc.MutableSet[str],
                      short=True) == "MutableSet[str]")
    assert type_name(collections.Counter[str], short=True) == "Counter[str]"
    assert (type_name(collections.abc.Sequence[str],
                      short=True) == "Sequence[str]")
    assert (type_name(collections.abc.MutableSequence[str],
                      short=True) == "MutableSequence[str]")
    assert (type_name(collections.ChainMap[str, str],
                      short=True) == "ChainMap[str, str]")
    assert type_name(dict[str, str], short=True) == "dict[str, str]"
    assert (type_name(collections.abc.Mapping[str, str],
                      short=True) == "Mapping[str, str]")
    assert (type_name(collections.OrderedDict[str, str],
                      short=True) == "OrderedDict[str, str]")
Exemple #3
0
def test_type_name_short():
    assert type_name(TAny, short=True) == "Any"
    assert type_name(TInt, short=True) == "int"
    assert type_name(TMyDataClass, short=True) == "MyDataClass"
    assert type_name(TIntStr, short=True) == "Union[int, str]"
    assert type_name(typing.List[TInt], short=True) == "List[int]"
    assert type_name(typing.Tuple[int], short=True) == "Tuple[int]"
    assert type_name(typing.Set[int], short=True) == "Set[int]"
    assert type_name(typing.FrozenSet[int], short=True) == "FrozenSet[int]"
    assert type_name(typing.Deque[int], short=True) == "Deque[int]"
    assert type_name(typing.Dict[int, int], short=True) == "Dict[int, int]"
    assert (type_name(typing.Mapping[int, int],
                      short=True) == "Mapping[int, int]")
    assert (type_name(typing.MutableMapping[int, int],
                      short=True) == "MutableMapping[int, int]")
    assert type_name(typing.Counter[int], short=True) == "Counter[int]"
    assert (type_name(typing.ChainMap[int, int],
                      short=True) == "ChainMap[int, int]")
    assert type_name(typing.Sequence[int], short=True) == "Sequence[int]"
    assert type_name(typing.Union[int, str], short=True) == "Union[int, str]"
    assert (type_name(typing.Union[int, typing.Any],
                      short=True) == "Union[int, Any]")
    assert (type_name(typing_extensions.OrderedDict[int, int],
                      short=True) == "OrderedDict[int, int]")
    assert type_name(typing.Optional[int], short=True) == "Optional[int]"
    assert type_name(typing.Union[None, int], short=True) == "Optional[int]"
    assert type_name(typing.Union[int, None], short=True) == "Optional[int]"
    assert type_name(None, short=True) == "None"
    assert type_name(NoneType, short=True) == "NoneType"
    assert type_name(NoneType, short=True, none_type_as_none=True) == "None"
    assert type_name(typing.List[NoneType], short=True) == "List[NoneType]"
    assert (type_name(typing.Union[int, str, None],
                      short=True) == "Union[int, str, None]")
    assert type_name(typing.Optional[NoneType], short=True) == "NoneType"

    if PY_310_MIN:
        assert type_name(int | None, short=True) == "Optional[int]"
        assert type_name(None | int, short=True) == "Optional[int]"
        assert type_name(int | str, short=True) == "Union[int, str]"
    if PY_310_MIN:
        assert type_name(MyDatetimeNewType, short=True) == "MyDatetimeNewType"
    else:
        assert type_name(MyDatetimeNewType,
                         short=True) == type_name(datetime, short=True)
    assert (type_name(typing_extensions.Annotated[TMyDataClass, None],
                      short=True) == "MyDataClass")
Exemple #4
0
def test_type_name():
    assert type_name(TAny) == "typing.Any"
    assert type_name(TInt) == "int"
    assert type_name(TMyDataClass) == "tests.entities.MyDataClass"
    assert type_name(TIntStr) == "typing.Union[int, str]"
    assert type_name(typing.List[TInt]) == "typing.List[int]"
    assert type_name(typing.Tuple[int]) == "typing.Tuple[int]"
    assert type_name(typing.Set[int]) == "typing.Set[int]"
    assert type_name(typing.FrozenSet[int]) == "typing.FrozenSet[int]"
    assert type_name(typing.Deque[int]) == "typing.Deque[int]"
    assert type_name(typing.Dict[int, int]) == "typing.Dict[int, int]"
    assert type_name(typing.Mapping[int, int]) == "typing.Mapping[int, int]"
    assert (type_name(
        typing.MutableMapping[int, int]) == "typing.MutableMapping[int, int]")
    assert type_name(typing.Counter[int]) == "typing.Counter[int]"
    assert type_name(typing.ChainMap[int, int]) == "typing.ChainMap[int, int]"
    assert type_name(typing.Sequence[int]) == "typing.Sequence[int]"
    assert type_name(typing.Union[int, str]) == "typing.Union[int, str]"
    assert (type_name(
        typing.Union[int, typing.Any]) == "typing.Union[int, typing.Any]")
    if PY_37_MIN:
        assert (type_name(typing_extensions.OrderedDict[int, int]) ==
                "typing.OrderedDict[int, int]")
    else:
        assert (type_name(typing_extensions.OrderedDict[int, int]) ==
                "typing_extensions.OrderedDict[int, int]")
    assert type_name(typing.Optional[int]) == "typing.Optional[int]"
    assert type_name(typing.Union[None, int]) == "typing.Optional[int]"
    assert type_name(typing.Union[int, None]) == "typing.Optional[int]"
    assert type_name(None) == "None"
    assert type_name(NoneType) == "NoneType"
    assert type_name(NoneType, none_type_as_none=True) == "None"
    assert type_name(typing.List[NoneType]) == "typing.List[NoneType]"
    assert (type_name(typing.Union[int, str,
                                   None]) == "typing.Union[int, str, None]")
    assert type_name(typing.Optional[NoneType]) == "NoneType"

    if PY_310_MIN:
        assert type_name(int | None) == "typing.Optional[int]"
        assert type_name(None | int) == "typing.Optional[int]"
        assert type_name(int | str) == "typing.Union[int, str]"
    if PY_310_MIN:
        assert (
            type_name(MyDatetimeNewType) == "tests.entities.MyDatetimeNewType")
    else:
        assert type_name(MyDatetimeNewType) == type_name(datetime)
    assert (type_name(
        typing_extensions.Annotated[TMyDataClass,
                                    None]) == "tests.entities.MyDataClass")
Exemple #5
0
 def holder_class_name(self):
     return type_name(self.holder_class, short=True)
Exemple #6
0
 def field_type_name(self):
     return type_name(self.field_type, short=True)