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


Python typing.ClassVar方法代碼示例

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


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

示例1: test_last_args

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import ClassVar [as 別名]
def test_last_args(self):
        T = TypeVar('T')
        S = TypeVar('S')
        self.assertEqual(get_last_args(int), ())
        self.assertEqual(get_last_args(Union), ())
        if WITH_CLASSVAR:
            self.assertEqual(get_last_args(ClassVar[int]), (int,))
        self.assertEqual(get_last_args(Union[T, int]), (T, int))
        self.assertEqual(get_last_args(Union[str, int]), (str, int))
        self.assertEqual(get_last_args(Tuple[T, int]), (T, int))
        self.assertEqual(get_last_args(Tuple[str, int]), (str, int))
        self.assertEqual(get_last_args(Generic[T]), (T, ))
        if GENERIC_TUPLE_PARAMETRIZABLE:
            tp = Iterable[Tuple[T, S]][int, T]
            self.assertEqual(get_last_args(tp), (int, T))
        if LEGACY_TYPING:
            self.assertEqual(get_last_args(Callable[[T, S], int]), (T, S))
            self.assertEqual(get_last_args(Callable[[], int]), ())
        else:
            self.assertEqual(get_last_args(Callable[[T, S], int]), (T, S, int))
            self.assertEqual(get_last_args(Callable[[], int]), (int,)) 
開發者ID:ilevkivskyi,項目名稱:typing_inspect,代碼行數:23,代碼來源:test_typing_inspect.py

示例2: test_pickle

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import ClassVar [as 別名]
def test_pickle(self):
        global C  # pickle wants to reference the class by name
        T = TypeVar('T')

        class B(Generic[T]):
            pass

        class C(B[int]):
            pass

        c = C()
        c.foo = 42
        c.bar = 'abc'
        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            z = pickle.dumps(c, proto)
            x = pickle.loads(z)
            self.assertEqual(x.foo, 42)
            self.assertEqual(x.bar, 'abc')
            self.assertEqual(x.__dict__, {'foo': 42, 'bar': 'abc'})
        simples = [Any, Union, Tuple, Callable, ClassVar, List, typing.Iterable]
        for s in simples:
            for proto in range(pickle.HIGHEST_PROTOCOL + 1):
                z = pickle.dumps(s, proto)
                x = pickle.loads(z)
                self.assertEqual(s, x) 
開發者ID:ShikyoKira,項目名稱:Project-New-Reign---Nemesis-Main,代碼行數:27,代碼來源:test_typing.py

示例3: test_isnt_classvar

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import ClassVar [as 別名]
def test_isnt_classvar(self):
        for typestr in ('CV',
                        't.ClassVar',
                        't.ClassVar[int]',
                        'typing..ClassVar[int]',
                        'Classvar',
                        'Classvar[int]',
                        'typing.ClassVarx[int]',
                        'typong.ClassVar[int]',
                        'dataclasses.ClassVar[int]',
                        'typingxClassVar[str]',
                        ):
            with self.subTest(typestr=typestr):
                @dataclass
                class C:
                    x: typestr

                # x is not a ClassVar, so C() takes one arg.
                self.assertEqual(C(10).x, 10) 
開發者ID:ericvsmith,項目名稱:dataclasses,代碼行數:21,代碼來源:test_dataclasses.py

示例4: test_other_params

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import ClassVar [as 別名]
def test_other_params(self):
        C = make_dataclass('C',
                           [('x', int),
                            ('y', ClassVar[int], 10),
                            ('z', ClassVar[int], field(default=20)),
                            ],
                           init=False)
        # Make sure we have a repr, but no init.
        self.assertNotIn('__init__', vars(C))
        self.assertIn('__repr__', vars(C))

        # Make sure random other params don't work.
        with self.assertRaisesRegex(TypeError, 'unexpected keyword argument'):
            C = make_dataclass('C',
                               [],
                               xxinit=False) 
開發者ID:ericvsmith,項目名稱:dataclasses,代碼行數:18,代碼來源:test_dataclasses.py

示例5: test_class_var

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import ClassVar [as 別名]
def test_class_var(self):
        # Make sure ClassVars are ignored in __init__, __repr__, etc.
        @dataclass
        class C:
            x: int
            y: int = 10
            z: ClassVar[int] = 1000
            w: ClassVar[int] = 2000
            t: ClassVar[int] = 3000
            s: ClassVar      = 4000

        c = C(5)
        self.assertEqual(repr(c), 'TestCase.test_class_var.<locals>.C(x=5, y=10)')
        self.assertEqual(len(fields(C)), 2)                 # We have 2 fields.
        self.assertEqual(len(C.__annotations__), 6)         # And 4 ClassVars.
        self.assertEqual(c.z, 1000)
        self.assertEqual(c.w, 2000)
        self.assertEqual(c.t, 3000)
        self.assertEqual(c.s, 4000)
        C.z += 1
        self.assertEqual(c.z, 1001)
        c = C(20)
        self.assertEqual((c.x, c.y), (20, 10))
        self.assertEqual(c.z, 1001)
        self.assertEqual(c.w, 2000)
        self.assertEqual(c.t, 3000)
        self.assertEqual(c.s, 4000) 
開發者ID:ericvsmith,項目名稱:dataclasses,代碼行數:29,代碼來源:test_dataclasses.py

示例6: test_class_var_no_default

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import ClassVar [as 別名]
def test_class_var_no_default(self):
        # If a ClassVar has no default value, it should not be set on the class.
        @dataclass
        class C:
            x: ClassVar[int]

        self.assertNotIn('x', C.__dict__) 
開發者ID:ericvsmith,項目名稱:dataclasses,代碼行數:9,代碼來源:test_dataclasses.py

示例7: test_class_var_default_factory

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import ClassVar [as 別名]
def test_class_var_default_factory(self):
        # It makes no sense for a ClassVar to have a default factory. When
        #  would it be called? Call it yourself, since it's class-wide.
        with self.assertRaisesRegex(TypeError,
                                    'cannot have a default factory'):
            @dataclass
            class C:
                x: ClassVar[int] = field(default_factory=int)

            self.assertNotIn('x', C.__dict__) 
開發者ID:ericvsmith,項目名稱:dataclasses,代碼行數:12,代碼來源:test_dataclasses.py

示例8: test_class_var_with_default

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import ClassVar [as 別名]
def test_class_var_with_default(self):
        # If a ClassVar has a default value, it should be set on the class.
        @dataclass
        class C:
            x: ClassVar[int] = 10
        self.assertEqual(C.x, 10)

        @dataclass
        class C:
            x: ClassVar[int] = field(default=10)
        self.assertEqual(C.x, 10) 
開發者ID:ericvsmith,項目名稱:dataclasses,代碼行數:13,代碼來源:test_dataclasses.py

示例9: test_classvar_default_factory

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import ClassVar [as 別名]
def test_classvar_default_factory(self):
        # It's an error for a ClassVar to have a factory function.
        with self.assertRaisesRegex(TypeError,
                                    'cannot have a default factory'):
            @dataclass
            class C:
                x: ClassVar[int] = field(default_factory=int) 
開發者ID:ericvsmith,項目名稱:dataclasses,代碼行數:9,代碼來源:test_dataclasses.py

示例10: test_classvar

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import ClassVar [as 別名]
def test_classvar(self):
        # Some expressions recognized as ClassVar really aren't.  But
        #  if you're using string annotations, it's not an exact
        #  science.
        # These tests assume that both "import typing" and "from
        # typing import *" have been run in this file.
        for typestr in ('ClassVar[int]',
                        'ClassVar [int]'
                        ' ClassVar [int]',
                        'ClassVar',
                        ' ClassVar ',
                        'typing.ClassVar[int]',
                        'typing.ClassVar[str]',
                        ' typing.ClassVar[str]',
                        'typing .ClassVar[str]',
                        'typing. ClassVar[str]',
                        'typing.ClassVar [str]',
                        'typing.ClassVar [ str]',

                        # Not syntactically valid, but these will
                        #  be treated as ClassVars.
                        'typing.ClassVar.[int]',
                        'typing.ClassVar+',
                        ):
            with self.subTest(typestr=typestr):
                @dataclass
                class C:
                    x: typestr

                # x is a ClassVar, so C() takes no args.
                C()

                # And it won't appear in the class's dict because it doesn't
                # have a default.
                self.assertNotIn('x', C.__dict__) 
開發者ID:ericvsmith,項目名稱:dataclasses,代碼行數:37,代碼來源:test_dataclasses.py

示例11: get_chain_params

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import ClassVar [as 別名]
def get_chain_params(dash_network: str) -> typing.ClassVar[ChainParams]:
    if dash_network == 'MAINNET':
        return ChainParamsMainNet
    elif dash_network == 'TESTNET':
        return ChainParamsTestNet
    else:
        raise Exception('Invalid \'network\' value.') 
開發者ID:Bertrand256,項目名稱:dash-masternode-tool,代碼行數:9,代碼來源:dash_utils.py

示例12: test_ignores_classvar_when_generating_fields

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import ClassVar [as 別名]
def test_ignores_classvar_when_generating_fields(registry_):
    class SomeOtherType(SomeType):
        frob: typing.ClassVar[int] = 0

    converter = BaseConverter(registry=registry_)
    generated_fields = converter.convert_all(SomeOtherType)

    assert "frob" not in generated_fields 
開發者ID:justanr,項目名稱:marshmallow-annotations,代碼行數:10,代碼來源:test_converter.py

示例13: test_classvar

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import ClassVar [as 別名]
def test_classvar(self):
        T = TypeVar('T')
        samples = [ClassVar, ClassVar[int], ClassVar[List[T]]]
        nonsamples = [int, 42, Iterable, List[int], type, T]
        self.sample_test(is_classvar, samples, nonsamples) 
開發者ID:ilevkivskyi,項目名稱:typing_inspect,代碼行數:7,代碼來源:test_typing_inspect.py

示例14: test_last_origin

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import ClassVar [as 別名]
def test_last_origin(self):
        T = TypeVar('T')
        self.assertEqual(get_last_origin(int), None)
        if WITH_CLASSVAR:
            self.assertEqual(get_last_origin(ClassVar[int]), None)
        self.assertEqual(get_last_origin(Generic[T]), Generic)
        if EXISTING_UNIONS_SUBSCRIPTABLE:
            self.assertEqual(get_last_origin(Union[T, int][str]), Union[T, int])
        if GENERIC_TUPLE_PARAMETRIZABLE:
            tp = List[Tuple[T, T]][int]
            self.assertEqual(get_last_origin(tp), List[Tuple[T, T]])
        self.assertEqual(get_last_origin(List), List) 
開發者ID:ilevkivskyi,項目名稱:typing_inspect,代碼行數:14,代碼來源:test_typing_inspect.py

示例15: test_origin

# 需要導入模塊: import typing [as 別名]
# 或者: from typing import ClassVar [as 別名]
def test_origin(self):
        T = TypeVar('T')
        self.assertEqual(get_origin(int), None)
        if WITH_CLASSVAR:
            self.assertEqual(get_origin(ClassVar[int]), None)
        self.assertEqual(get_origin(Generic), Generic)
        self.assertEqual(get_origin(Generic[T]), Generic)
        if GENERIC_TUPLE_PARAMETRIZABLE:
            tp = List[Tuple[T, T]][int]
            self.assertEqual(get_origin(tp), list if NEW_TYPING else List) 
開發者ID:ilevkivskyi,項目名稱:typing_inspect,代碼行數:12,代碼來源:test_typing_inspect.py


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