Skip to content

Commit

Permalink
improve type typing
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Oct 29, 2023
1 parent fdc6ae9 commit d2e6fdb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
29 changes: 27 additions & 2 deletions tests/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
import typing as t
from abc import ABC

import pytest

Expand Down Expand Up @@ -128,6 +129,12 @@ 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."""

Expand All @@ -140,12 +147,30 @@ class KernelSpecManager(HasTraits):
)
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@128
reveal_type(t.kernel_spec_class()) # R: tests.test_typing.KernelSpec@128
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
Expand Down
4 changes: 2 additions & 2 deletions traitlets/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2053,7 +2053,7 @@ def __init__(
def __init__(
self: Type[S, S],
default_value: S = ...,
klass: type[S] = ...,
klass: S = ...,
allow_none: Literal[False] = ...,
read_only: bool | None = ...,
help: str | None = ...,
Expand All @@ -2066,7 +2066,7 @@ def __init__(
def __init__(
self: Type[S | None, S | None],
default_value: S | None = ...,
klass: type[S] = ...,
klass: S = ...,
allow_none: Literal[True] = ...,
read_only: bool | None = ...,
help: str | None = ...,
Expand Down

0 comments on commit d2e6fdb

Please sign in to comment.