summaryrefslogtreecommitdiff
path: root/tests/functional/a/alternative_union_syntax.py
blob: 9e258faa5e8d9e84e169b9b9efb734d42b89543f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Test PEP 604 - Alternative Union syntax"""
# pylint: disable=missing-function-docstring,unused-argument,invalid-name,missing-class-docstring
# pylint: disable=inherit-non-class,too-few-public-methods
import dataclasses
import typing
from dataclasses import dataclass
from typing import NamedTuple, TypedDict

Alias = str | list[int]
lst = [typing.Dict[str, int] | None,]

var1: typing.Dict[str, int | None]
var2: int | str | None
var3: int | list[str | int]
var4: typing.Dict[typing.Tuple[int, int] | int, None]

cast_var = 1
cast_var = typing.cast(str | int, cast_var)

T = typing.TypeVar("T", int | str, bool)

(lambda x: 2)(int | str)

var: str | int

def func(arg: int | str):
    pass

def func2() -> int | str:
    pass

class CustomCls(int):
    pass

Alias2 = CustomCls |  str

var2 = CustomCls(1) | int(2)


# Check typing.NamedTuple
CustomNamedTuple = typing.NamedTuple(
    "CustomNamedTuple", [("my_var", int | str)])

class CustomNamedTuple2(NamedTuple):
    my_var: int | str

class CustomNamedTuple3(typing.NamedTuple):
    my_var: int | str


# Check typing.TypedDict
CustomTypedDict = TypedDict("CustomTypedDict", my_var=(int | str))

CustomTypedDict2 = TypedDict("CustomTypedDict2", {"my_var": int | str})

class CustomTypedDict3(TypedDict):
    my_var: int | str

class CustomTypedDict4(typing.TypedDict):
    my_var: int | str


# Check dataclasses
def my_decorator(*args, **kwargs):
    def wraps(*args, **kwargs):
        pass
    return wraps

@dataclass
class CustomDataClass:
    my_var: int | str

@dataclasses.dataclass
class CustomDataClass2:
    my_var: int | str

@dataclass()
class CustomDataClass3:
    my_var: int | str

@my_decorator
@dataclasses.dataclass
class CustomDataClass4:
    my_var: int | str