Skip to content

Commit ad1c943

Browse files
authored
Merge pull request #791 from blink1073/more-typing
2 parents 1e99f8c + de33484 commit ad1c943

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

traitlets/config/application.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from copy import deepcopy
1818
from logging.config import dictConfig
1919
from textwrap import dedent
20+
from typing import Any, Callable, TypeVar, cast
2021

2122
from traitlets.config.configurable import Configurable, SingletonConfigurable
2223
from traitlets.config.loader import (
@@ -94,8 +95,10 @@
9495

9596
IS_PYTHONW = sys.executable and sys.executable.endswith("pythonw.exe")
9697

98+
T = TypeVar("T", bound=Callable[..., Any])
9799

98-
def catch_config_error(method):
100+
101+
def catch_config_error(method: T) -> T:
99102
"""Method decorator for catching invalid config (Trait/ArgumentErrors) during init.
100103
101104
On a TraitError (generally caused by bad config), this will print the trait's
@@ -113,7 +116,7 @@ def inner(app, *args, **kwargs):
113116
app.log.debug("Config at the time: %s", app.config)
114117
app.exit(1)
115118

116-
return inner
119+
return cast(T, inner)
117120

118121

119122
class ApplicationError(Exception):

traitlets/traitlets.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ def setup_class(cls, classdict):
981981
super().setup_class(classdict)
982982

983983

984-
def observe(*names, type="change"):
984+
def observe(*names: t.Union[Sentinel, str], type: str = "change") -> "ObserveHandler":
985985
"""A decorator which can be used to observe Traits on a class.
986986
987987
The handler passed to the decorator will be called with one ``change``
@@ -1047,7 +1047,7 @@ def compatible_observer(self, change_or_name, old=Undefined, new=Undefined):
10471047
return compatible_observer
10481048

10491049

1050-
def validate(*names):
1050+
def validate(*names: t.Union[Sentinel, str]) -> "ValidateHandler":
10511051
"""A decorator to register cross validator of HasTraits object's state
10521052
when a Trait is set.
10531053
@@ -1080,7 +1080,7 @@ def validate(*names):
10801080
return ValidateHandler(names)
10811081

10821082

1083-
def default(name):
1083+
def default(name: str) -> "DefaultHandler":
10841084
"""A decorator which assigns a dynamic default for a Trait on a HasTraits object.
10851085
10861086
Parameters

traitlets/utils/decorators.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
import copy
44
from inspect import Parameter, Signature, signature
5+
from typing import Type, TypeVar
56

6-
from ..traitlets import Undefined
7+
from ..traitlets import HasTraits, Undefined
78

89

910
def _get_default(value):
1011
"""Get default argument value, given the trait default value."""
1112
return Parameter.empty if value == Undefined else value
1213

1314

14-
def signature_has_traits(cls):
15+
T = TypeVar("T", bound=HasTraits)
16+
17+
18+
def signature_has_traits(cls: Type[T]) -> Type[T]:
1519
"""Return a decorated class with a constructor signature that contain Trait names as kwargs."""
1620
traits = [
1721
(name, _get_default(value.default_value))
@@ -78,6 +82,6 @@ def signature_has_traits(cls):
7882
# Append **kwargs
7983
new_parameters.append(old_var_keyword_parameter)
8084

81-
cls.__signature__ = Signature(new_parameters)
85+
cls.__signature__ = Signature(new_parameters) # type:ignore[attr-defined]
8286

8387
return cls

0 commit comments

Comments
 (0)