diff options
| author | robot-contrib <[email protected]> | 2023-11-14 19:18:35 +0300 |
|---|---|---|
| committer | robot-contrib <[email protected]> | 2023-11-14 20:27:03 +0300 |
| commit | 4eb8b66b95f8ec1535b20a7da702a992b07671ca (patch) | |
| tree | de40079b443e5d159d7b199ad9ee4e22a26ae97c /contrib/python/traitlets/py3/tests/test_typing.py | |
| parent | 874ef51d3d3edfa25f5a505ec6ab50e172965d1e (diff) | |
Update contrib/python/traitlets/py3 to 5.13.0
Diffstat (limited to 'contrib/python/traitlets/py3/tests/test_typing.py')
| -rw-r--r-- | contrib/python/traitlets/py3/tests/test_typing.py | 145 |
1 files changed, 102 insertions, 43 deletions
diff --git a/contrib/python/traitlets/py3/tests/test_typing.py b/contrib/python/traitlets/py3/tests/test_typing.py index 2b4073ecf72..04f027af123 100644 --- a/contrib/python/traitlets/py3/tests/test_typing.py +++ b/contrib/python/traitlets/py3/tests/test_typing.py @@ -1,6 +1,8 @@ from __future__ import annotations -import typing +import logging +import typing as t +from abc import ABC import pytest @@ -9,6 +11,7 @@ from traitlets import ( Bool, CInt, Dict, + Enum, HasTraits, Instance, Int, @@ -24,9 +27,9 @@ from traitlets import ( ) from traitlets.config import Config -if not typing.TYPE_CHECKING: +if not t.TYPE_CHECKING: - def reveal_type(*args, **kwargs): + def reveal_type(*args: t.Any, **kwargs: t.Any) -> None: pass @@ -34,12 +37,12 @@ if not typing.TYPE_CHECKING: class Foo: - def __init__(self, c): + def __init__(self, c: t.Any) -> None: self.c = c @pytest.mark.mypy_testing -def mypy_decorator_typing(): +def mypy_decorator_typing() -> None: class T(HasTraits): foo = Unicode("").tag(config=True) @@ -48,11 +51,11 @@ def mypy_decorator_typing(): return "hi" @observe("foo") - def _foo_observer(self, change: typing.Any) -> bool: + def _foo_observer(self, change: t.Any) -> bool: return True @validate("foo") - def _foo_validate(self, commit: typing.Any) -> bool: + def _foo_validate(self, commit: t.Any) -> bool: return True t = T() @@ -62,7 +65,7 @@ def mypy_decorator_typing(): @pytest.mark.mypy_testing -def mypy_config_typing(): +def mypy_config_typing() -> None: c = Config( { "ExtractOutputPreprocessor": {"enabled": True}, @@ -72,7 +75,7 @@ def mypy_config_typing(): @pytest.mark.mypy_testing -def mypy_union_typing(): +def mypy_union_typing() -> None: class T(HasTraits): style = Union( [Unicode("default"), Type(klass=object)], @@ -90,40 +93,48 @@ def mypy_union_typing(): @pytest.mark.mypy_testing -def mypy_list_typing(): +def mypy_list_typing() -> None: class T(HasTraits): latex_command = List( ["xelatex", "{filename}", "-quiet"], help="Shell command used to compile latex." ).tag(config=True) t = T() - reveal_type(List("foo")) # R: traitlets.traitlets.List - reveal_type(List("").tag(sync=True)) # R: traitlets.traitlets.List - reveal_type(List(None, allow_none=True)) # R: traitlets.traitlets.List - reveal_type(List(None, allow_none=True).tag(sync=True)) # R: traitlets.traitlets.List - reveal_type(T.latex_command) # R: traitlets.traitlets.List - reveal_type(t.latex_command) # R: builtins.list[Any] + reveal_type(List(["foo"])) # R: traitlets.traitlets.List[builtins.str] + reveal_type(List([""]).tag(sync=True)) # R: traitlets.traitlets.List[builtins.str] + reveal_type(List(None, allow_none=True)) # R: traitlets.traitlets.List[<nothing>] + reveal_type( + List(None, allow_none=True).tag(sync=True) # R: traitlets.traitlets.List[<nothing>] + ) + reveal_type(T.latex_command) # R: traitlets.traitlets.List[builtins.str] + reveal_type(t.latex_command) # R: builtins.list[builtins.str] @pytest.mark.mypy_testing -def mypy_dict_typing(): +def mypy_dict_typing() -> None: class T(HasTraits): foo = Dict({}, help="Shell command used to compile latex.").tag(config=True) t = T() - reveal_type(Dict("foo")) # R: traitlets.traitlets.Dict - reveal_type(Dict("").tag(sync=True)) # R: traitlets.traitlets.Dict - reveal_type(Dict(None, allow_none=True)) # R: traitlets.traitlets.Dict - reveal_type(Dict(None, allow_none=True).tag(sync=True)) # R: traitlets.traitlets.Dict - reveal_type(T.foo) # R: traitlets.traitlets.Dict - reveal_type(t.foo) # R: builtins.dict[Any, Any] + reveal_type(Dict(None, allow_none=True)) # R: traitlets.traitlets.Dict[builtins.str, Any] + reveal_type( + Dict(None, allow_none=True).tag(sync=True) # R: traitlets.traitlets.Dict[builtins.str, Any] + ) + reveal_type(T.foo) # R: traitlets.traitlets.Dict[builtins.str, Any] + reveal_type(t.foo) # R: builtins.dict[builtins.str, Any] @pytest.mark.mypy_testing -def mypy_type_typing(): +def mypy_type_typing() -> None: class KernelSpec: item = Unicode("foo") + class KernelSpecSubclass(KernelSpec): + other = Unicode("bar") + + class GatewayTokenRenewerBase(ABC): + item = Unicode("foo") + class KernelSpecManager(HasTraits): """A manager for kernel specs.""" @@ -136,16 +147,34 @@ def mypy_type_typing(): ) other_class = Type("foo.bar.baz") + other_kernel_spec_class = Type( + default_value=KernelSpecSubclass, + klass=KernelSpec, + config=True, + ) + + gateway_token_renewer_class = Type( + klass=GatewayTokenRenewerBase, + config=True, + help="""The class to use for Gateway token renewal. (JUPYTER_GATEWAY_TOKEN_RENEWER_CLASS env var)""", + ) + t = KernelSpecManager() - reveal_type(t.kernel_spec_class) # R: def () -> tests.test_typing.KernelSpec@124 - reveal_type(t.kernel_spec_class()) # R: tests.test_typing.KernelSpec@124 + reveal_type(t.kernel_spec_class) # R: def () -> tests.test_typing.KernelSpec@129 + reveal_type(t.kernel_spec_class()) # R: tests.test_typing.KernelSpec@129 reveal_type(t.kernel_spec_class().item) # R: builtins.str reveal_type(t.other_class) # R: builtins.type reveal_type(t.other_class()) # R: Any + reveal_type(t.other_kernel_spec_class) # R: def () -> tests.test_typing.KernelSpec@129 + reveal_type(t.other_kernel_spec_class()) # R: tests.test_typing.KernelSpec@129 + reveal_type( + t.gateway_token_renewer_class # R: def () -> tests.test_typing.GatewayTokenRenewerBase@135 + ) + reveal_type(t.gateway_token_renewer_class()) # R: tests.test_typing.GatewayTokenRenewerBase@135 @pytest.mark.mypy_testing -def mypy_unicode_typing(): +def mypy_unicode_typing() -> None: class T(HasTraits): export_format = Unicode( allow_none=False, @@ -163,9 +192,7 @@ def mypy_unicode_typing(): reveal_type( Unicode( # R: traitlets.traitlets.Unicode[builtins.str, Union[builtins.str, builtins.bytes]] "" - ).tag( - sync=True - ) + ).tag(sync=True) ) reveal_type( Unicode( # R: traitlets.traitlets.Unicode[Union[builtins.str, None], Union[builtins.str, builtins.bytes, None]] @@ -175,9 +202,7 @@ def mypy_unicode_typing(): reveal_type( Unicode( # R: traitlets.traitlets.Unicode[Union[builtins.str, None], Union[builtins.str, builtins.bytes, None]] None, allow_none=True - ).tag( - sync=True - ) + ).tag(sync=True) ) reveal_type( T.export_format # R: traitlets.traitlets.Unicode[builtins.str, Union[builtins.str, builtins.bytes]] @@ -186,7 +211,43 @@ def mypy_unicode_typing(): @pytest.mark.mypy_testing -def mypy_set_typing(): +def mypy_enum_typing() -> None: + class T(HasTraits): + log_level = Enum( + (0, 10, 20, 30, 40, 50), + default_value=logging.WARN, + help="Set the log level by value or name.", + ).tag(config=True) + + t = T() + reveal_type( + Enum( # R: traitlets.traitlets.Enum[builtins.str] + ("foo",) + ) + ) + reveal_type( + Enum( # R: traitlets.traitlets.Enum[builtins.str] + [""] + ).tag(sync=True) + ) + reveal_type( + Enum( # R: traitlets.traitlets.Enum[None] + None, allow_none=True + ) + ) + reveal_type( + Enum( # R: traitlets.traitlets.Enum[None] + None, allow_none=True + ).tag(sync=True) + ) + reveal_type( + T.log_level # R: traitlets.traitlets.Enum[builtins.int] + ) + reveal_type(t.log_level) # R: builtins.int + + [email protected]_testing +def mypy_set_typing() -> None: class T(HasTraits): remove_cell_tags = Set( Unicode(), @@ -222,7 +283,7 @@ def mypy_set_typing(): @pytest.mark.mypy_testing -def mypy_any_typing(): +def mypy_any_typing() -> None: class T(HasTraits): attributes = Any( config=True, @@ -244,7 +305,7 @@ def mypy_any_typing(): @pytest.mark.mypy_testing -def mypy_bool_typing(): +def mypy_bool_typing() -> None: class T(HasTraits): b = Bool(True).tag(sync=True) ob = Bool(None, allow_none=True).tag(sync=True) @@ -266,9 +327,7 @@ def mypy_bool_typing(): reveal_type( Bool( # R: traitlets.traitlets.Bool[Union[builtins.bool, None], Union[builtins.bool, builtins.int, None]] None, allow_none=True - ).tag( - sync=True - ) + ).tag(sync=True) ) reveal_type( T.b # R: traitlets.traitlets.Bool[builtins.bool, Union[builtins.bool, builtins.int]] @@ -287,7 +346,7 @@ def mypy_bool_typing(): @pytest.mark.mypy_testing -def mypy_int_typing(): +def mypy_int_typing() -> None: class T(HasTraits): i: Int[int, int] = Int(42).tag(sync=True) oi: Int[int | None, int | None] = Int(42, allow_none=True).tag(sync=True) @@ -318,7 +377,7 @@ def mypy_int_typing(): @pytest.mark.mypy_testing -def mypy_cint_typing(): +def mypy_cint_typing() -> None: class T(HasTraits): i = CInt(42).tag(sync=True) oi = CInt(42, allow_none=True).tag(sync=True) @@ -342,7 +401,7 @@ def mypy_cint_typing(): @pytest.mark.mypy_testing -def mypy_tcp_typing(): +def mypy_tcp_typing() -> None: class T(HasTraits): tcp = TCPAddress() otcp = TCPAddress(None, allow_none=True) @@ -372,7 +431,7 @@ def mypy_tcp_typing(): @pytest.mark.mypy_testing -def mypy_instance_typing(): +def mypy_instance_typing() -> None: class T(HasTraits): inst = Instance(Foo) oinst = Instance(Foo, allow_none=True) |
