Skip to content

Commit

Permalink
more typing and lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Dec 21, 2022
1 parent 0b039a1 commit 2a85a13
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 123 deletions.
28 changes: 13 additions & 15 deletions traitlets/config/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,23 @@ class Application(SingletonConfigurable):

# The name of the application, will usually match the name of the command
# line application
name: t.Union[str, Unicode] = Unicode("application")
name = Unicode("application")

log = Any(help="Logger or LoggerAdapter instance", allow_none=False)

# The description of the application that is printed at the beginning
# of the help.
description: t.Union[str, Unicode] = Unicode("This is an application.")
description = Unicode("This is an application.")
# default section descriptions
option_description: t.Union[str, Unicode] = Unicode(option_description)
keyvalue_description: t.Union[str, Unicode] = Unicode(keyvalue_description)
subcommand_description: t.Union[str, Unicode] = Unicode(subcommand_description)
option_description = Unicode(option_description)
keyvalue_description = Unicode(keyvalue_description)
subcommand_description = Unicode(subcommand_description)

python_config_loader_class = PyFileConfigLoader
json_config_loader_class = JSONFileConfigLoader

# The usage and example string that goes at the end of the help string.
examples: t.Union[str, Unicode] = Unicode()
examples = Unicode()

# A sequence of Configurable subclasses whose config=True attributes will
# be exposed at the command line.
Expand All @@ -193,30 +193,28 @@ def _classes_inc_parents(self, classes=None):
yield parent

# The version string of this application.
version: t.Union[str, Unicode] = Unicode("0.0")
version = Unicode("0.0")

# the argv used to initialize the application
argv: t.Union[t.List[str], List] = List()

# Whether failing to load config files should prevent startup
raise_config_file_errors: t.Union[bool, Bool] = Bool(
TRAITLETS_APPLICATION_RAISE_CONFIG_FILE_ERROR
)
raise_config_file_errors = Bool(TRAITLETS_APPLICATION_RAISE_CONFIG_FILE_ERROR)

# The log level for the application
log_level: t.Union[str, int, Enum] = Enum( # type:ignore[assignment]
log_level = Enum(
(0, 10, 20, 30, 40, 50, "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL"),
default_value=logging.WARN,
help="Set the log level by value or name.",
).tag(config=True)

_log_formatter_cls = LevelFormatter

log_datefmt: t.Union[str, Unicode] = Unicode( # type:ignore[assignment]
log_datefmt = Unicode(
"%Y-%m-%d %H:%M:%S", help="The date format used by logging formatters for %(asctime)s"
).tag(config=True)

log_format: t.Union[str, Unicode] = Unicode( # type:ignore[assignment]
log_format = Unicode(
"[%(name)s]%(highlevel)s %(message)s",
help="The Logging format template",
).tag(config=True)
Expand Down Expand Up @@ -423,11 +421,11 @@ def _log_default(self):

_loaded_config_files = List()

show_config: t.Union[bool, Bool] = Bool( # type:ignore[assignment]
show_config = Bool(
help="Instead of starting the Application, dump configuration to stdout"
).tag(config=True)

show_config_json: t.Union[bool, Bool] = Bool( # type:ignore[assignment]
show_config_json = Bool(
help="Instead of starting the Application, dump configuration to stdout (as JSON)"
).tag(config=True)

Expand Down
2 changes: 1 addition & 1 deletion traitlets/config/tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ def __init__(self, *args, **kwargs):
self.foo = Foo(parent=self)

class TestApp(Application):
name = "test"

This comment has been minimized.

Copy link
@blink1073

blink1073 Dec 21, 2022

Contributor

I had added the explicit types in Application so we wouldn't have to use #type: ignore in all of the subclasses.

This comment has been minimized.

Copy link
@maartenbreddels

maartenbreddels Dec 21, 2022

Author Contributor

But the type has changed now. It's not a string, but a Unicode now.

This comment has been minimized.

Copy link
@blink1073

blink1073 Dec 21, 2022

Contributor

It was always a Unicode object in the base class. I made it more lenient (less correct) in the base class to avoid every downstream consumer from having to add type ignores

This comment has been minimized.

Copy link
@maartenbreddels

maartenbreddels Dec 21, 2022

Author Contributor

Ah ok, basically I need to revert all changes in application.py right?

This comment has been minimized.

Copy link
@blink1073

blink1073 Dec 21, 2022

Contributor

Yes please. 😄

This comment has been minimized.

Copy link
@maartenbreddels

maartenbreddels Dec 21, 2022

Author Contributor

How do we deal with the linter errors (about unused type:ignore)?

This comment has been minimized.

Copy link
@maartenbreddels

maartenbreddels Dec 21, 2022

Author Contributor

Hmm, I don't think we need it anymore

name = "test" # type:ignore

aliases = {"val": "Bar.Foo.val"}
classes = [Foo, Bar]
Expand Down
36 changes: 18 additions & 18 deletions traitlets/tests/test_traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class A(HasTraitsStub):
self.assertEqual(a._notify_new, 10)

def test_validate(self):
class MyTT(TraitType):
class MyTT(TraitType[int, int]):
def validate(self, inst, value):
return -1

Expand All @@ -127,7 +127,7 @@ class A(HasTraitsStub):
self.assertEqual(a.tt, -1)

def test_default_validate(self):
class MyIntTT(TraitType):
class MyIntTT(TraitType[int, int]):
def validate(self, obj, value):
if isinstance(value, int):
return value
Expand All @@ -154,7 +154,7 @@ class A(HasTraits):

def test_error(self):
class A(HasTraits):
tt = TraitType()
tt = TraitType[int, int]()

a = A()
self.assertRaises(TraitError, A.tt.error, a, 10)
Expand Down Expand Up @@ -271,14 +271,14 @@ def _default_x(self):
self.assertEqual(a._trait_values, {"x": 11})

def test_tag_metadata(self):
class MyIntTT(TraitType):
class MyIntTT(TraitType[int, int]):
metadata = {"a": 1, "b": 2}

a = MyIntTT(10).tag(b=3, c=4)
self.assertEqual(a.metadata, {"a": 1, "b": 3, "c": 4})

def test_metadata_localized_instance(self):
class MyIntTT(TraitType):
class MyIntTT(TraitType[int, int]):
metadata = {"a": 1, "b": 2}

a = MyIntTT(10)
Expand Down Expand Up @@ -326,7 +326,7 @@ class Foo(HasTraits):
self.assertEqual(Foo().bar, {})

def test_deprecated_metadata_access(self):
class MyIntTT(TraitType):
class MyIntTT(TraitType[int, int]):
metadata = {"a": 1, "b": 2}

a = MyIntTT(10)
Expand Down Expand Up @@ -395,12 +395,12 @@ class C(HasTraits):

def test_this_class(self):
class A(HasTraits):
t = This()
tt = This()
t = This["A"]()
tt = This["A"]()

class B(A):
tt = This()
ttt = This()
tt = This["A"]()
ttt = This["A"]()

self.assertEqual(A.t.this_class, A)
self.assertEqual(B.t.this_class, A)
Expand Down Expand Up @@ -1098,7 +1098,7 @@ class Bar(Foo):
class Bah:
pass

class FooInstance(Instance):
class FooInstance(Instance[Foo]):
klass = Foo

class A(HasTraits):
Expand Down Expand Up @@ -1174,15 +1174,15 @@ class Foo:

def inner():
class A(HasTraits):
inst = Instance(Foo())
inst = Instance(Foo()) # type:ignore

self.assertRaises(TraitError, inner)


class TestThis(TestCase):
def test_this_class(self):
class Foo(HasTraits):
this = This()
this = This["Foo"]()

f = Foo()
self.assertEqual(f.this, None)
Expand All @@ -1193,15 +1193,15 @@ class Foo(HasTraits):

def test_this_inst(self):
class Foo(HasTraits):
this = This()
this = This["Foo"]()

f = Foo()
f.this = Foo()
self.assertTrue(isinstance(f.this, Foo))

def test_subclass(self):
class Foo(HasTraits):
t = This()
t = This["Foo"]()

class Bar(Foo):
pass
Expand All @@ -1215,7 +1215,7 @@ class Bar(Foo):

def test_subclass_override(self):
class Foo(HasTraits):
t = This()
t = This["Foo"]()

class Bar(Foo):
t = This()
Expand Down Expand Up @@ -2479,12 +2479,12 @@ def test_notification_order():
###
class ForwardDeclaredInstanceTrait(HasTraits):

value = ForwardDeclaredInstance("ForwardDeclaredBar", allow_none=True)
value = ForwardDeclaredInstance["ForwardDeclaredBar"]("ForwardDeclaredBar", allow_none=True)


class ForwardDeclaredTypeTrait(HasTraits):

value = ForwardDeclaredType("ForwardDeclaredBar", allow_none=True)
value = ForwardDeclaredType[t.Any, t.Any]("ForwardDeclaredBar", allow_none=True)


class ForwardDeclaredInstanceListTrait(HasTraits):
Expand Down
33 changes: 32 additions & 1 deletion traitlets/tests/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import pytest
from typing_extensions import reveal_type

from traitlets import Bool, CInt, HasTraits, Int, TCPAddress
from traitlets import Bool, CInt, HasTraits, Instance, Int, TCPAddress


class Foo:
def __init__(self, c):
self.c = c


@pytest.mark.mypy_testing
Expand Down Expand Up @@ -132,3 +137,29 @@ class T(HasTraits):
t.tcp = "foo" # E: Incompatible types in assignment (expression has type "str", variable has type "Tuple[str, int]") [assignment]
t.otcp = "foo" # E: Incompatible types in assignment (expression has type "str", variable has type "Optional[Tuple[str, int]]") [assignment]
t.tcp = None # E: Incompatible types in assignment (expression has type "None", variable has type "Tuple[str, int]") [assignment]


@pytest.mark.mypy_testing
def mypy_instance_typing():
class T(HasTraits):
inst = Instance(Foo)
oinst = Instance(Foo, allow_none=True)

t = T()
reveal_type(t.inst) # R: traitlets.tests.test_typing.Foo
reveal_type(T.inst) # R: traitlets.traitlets.Instance[traitlets.tests.test_typing.Foo]
reveal_type(
T.inst.tag(sync=True) # R: traitlets.traitlets.Instance[traitlets.tests.test_typing.Foo]
)
reveal_type(t.oinst) # R: Union[traitlets.tests.test_typing.Foo, None]
reveal_type(
T.oinst # R: traitlets.traitlets.Instance[Union[traitlets.tests.test_typing.Foo, None]]
)
reveal_type(
T.oinst.tag( # R: traitlets.traitlets.Instance[Union[traitlets.tests.test_typing.Foo, None]]
sync=True
)
)
t.inst = "foo" # E: Incompatible types in assignment (expression has type "str", variable has type "Foo") [assignment]
t.oinst = "foo" # E: Incompatible types in assignment (expression has type "str", variable has type "Optional[Foo]") [assignment]
t.inst = None # E: Incompatible types in assignment (expression has type "None", variable has type "Foo") [assignment]
Loading

0 comments on commit 2a85a13

Please sign in to comment.