From 114b29131c1fe92eb945bf1118574f1732b3fa15 Mon Sep 17 00:00:00 2001 From: Corran Webster Date: Thu, 23 Sep 2021 13:17:05 +0100 Subject: [PATCH] Fix/flake8 code issues (#1757) * Fix redefinition of unused name errors (F811) * Fix module level imports not at top of file (E402). We exempt the toolkit.py modules from this because ordering toolkit selection and initialization is important. * Fix comparisons to True/False (E712) * Fix invalid escape sequences (W605) * Fix Handler.init() which do not return True/False; warn 3rd party code. * Add tests for Handler.init() API return values. * Skip new tests for null backend. * Fix all "from ... import *" uses (F403 and F405) * Apply suggestions from code review Co-authored-by: Poruri Sai Rahul Co-authored-by: Poruri Sai Rahul --- .../tutorials/code_snippets/code_block1.py | 5 ++-- .../tutorials/code_snippets/container.py | 7 +++-- .../tutorials/code_snippets/echo_box.py | 2 +- .../tutorials/code_snippets/event_loop.py | 2 +- ets-demo/etsdemo/app.py | 2 ++ examples/tutorials/traitsui_4.0/buttons.py | 4 +-- examples/tutorials/traitsui_4.0/deferred.py | 4 +-- .../tutorials/traitsui_4.0/editors/led.py | 1 + examples/tutorials/traitsui_4.0/items.py | 22 ++++++-------- examples/tutorials/traitsui_4.0/model_view.py | 9 +++--- integrationtests/styled_date_editor_test.py | 3 -- integrationtests/ui/buttons_test.py | 14 ++++----- integrationtests/ui/enum_dynamic_test.py | 4 +-- integrationtests/ui/instance_editor_test.py | 9 +++--- integrationtests/ui/instance_editor_test2.py | 8 ++--- integrationtests/ui/instance_editor_test3.py | 9 +++--- integrationtests/ui/instance_editor_test4.py | 7 ++--- integrationtests/ui/instance_editor_test5.py | 8 ++--- integrationtests/ui/instance_editor_test6.py | 8 ++--- integrationtests/ui/list_traits_ui_test.py | 2 +- integrationtests/ui/set_dynamic_test.py | 4 +-- integrationtests/ui/shell_editor_test.py | 5 ++-- integrationtests/ui/table_editor_test.py | 2 +- integrationtests/ui/table_editor_test2.py | 2 +- setup.cfg | 7 ++--- .../Dynamic_range_trait_and_editor.py | 3 +- .../List_editor_notebook_selection_demo.py | 2 +- traitsui/examples/demo/Extras/LED_display.py | 1 + traitsui/extras/_demo_legacy.py | 2 ++ traitsui/qt4/button_editor.py | 2 +- traitsui/tests/test_handler.py | 29 +++++++++++++++++-- .../tests/test_splitter_prefs_restored.py | 1 + traitsui/ui.py | 12 ++++++-- traitsui/view.py | 1 - traitsui/wx/table_editor.py | 1 + traitsui/wx/tabular_editor.py | 5 ---- 36 files changed, 113 insertions(+), 96 deletions(-) diff --git a/docs/source/tutorials/code_snippets/code_block1.py b/docs/source/tutorials/code_snippets/code_block1.py index e5714653f..b15fa8059 100644 --- a/docs/source/tutorials/code_snippets/code_block1.py +++ b/docs/source/tutorials/code_snippets/code_block1.py @@ -1,9 +1,8 @@ # code_block1.py -from traits.api import * -from traitsui.api import * +from traits.api import CInt, Enum, HasTraits -class Camera( HasTraits ): +class Camera(HasTraits): """ Camera object """ gain = Enum(1, 2, 3, desc="the gain index of the camera", diff --git a/docs/source/tutorials/code_snippets/container.py b/docs/source/tutorials/code_snippets/container.py index f84a207dd..2ea641864 100644 --- a/docs/source/tutorials/code_snippets/container.py +++ b/docs/source/tutorials/code_snippets/container.py @@ -1,7 +1,7 @@ # container.py -from traits.api import * -from traitsui.api import * +from traits.api import CInt, Enum, HasTraits, Instance, String +from traitsui.api import Item, View class Camera(HasTraits): """ Camera object """ @@ -14,11 +14,13 @@ class Camera(HasTraits): desc="the exposure time, in ms", label="Exposure", ) + class Display(HasTraits): string = String() view= View( Item('string', show_label=False, springy=True, style='custom' )) + class Container(HasTraits): camera = Instance(Camera, ()) display = Instance(Display, ()) @@ -28,5 +30,6 @@ class Container(HasTraits): Item('display', style='custom', show_label=False, ), ) + Container().configure_traits() diff --git a/docs/source/tutorials/code_snippets/echo_box.py b/docs/source/tutorials/code_snippets/echo_box.py index 3aed5a824..ec887ba0b 100644 --- a/docs/source/tutorials/code_snippets/echo_box.py +++ b/docs/source/tutorials/code_snippets/echo_box.py @@ -1,6 +1,6 @@ # echo_box.py -from traits.api import * +from traits.api import HasTraits, Str class EchoBox(HasTraits): input = Str() diff --git a/docs/source/tutorials/code_snippets/event_loop.py b/docs/source/tutorials/code_snippets/event_loop.py index 80270d1d4..d81ce2c70 100644 --- a/docs/source/tutorials/code_snippets/event_loop.py +++ b/docs/source/tutorials/code_snippets/event_loop.py @@ -1,6 +1,6 @@ # event_loop.py -from traits.api import * +from traits.api import HasTraits, Int from pyface.api import GUI class Counter(HasTraits): diff --git a/ets-demo/etsdemo/app.py b/ets-demo/etsdemo/app.py index 03955f036..f2a69be43 100644 --- a/ets-demo/etsdemo/app.py +++ b/ets-demo/etsdemo/app.py @@ -212,6 +212,7 @@ def init(self, info): demo_file = info.object with _set_stdout(self): demo_file.init() + return True def closed(self, info, is_ok): """ Closes the view. @@ -1057,6 +1058,7 @@ def init(self, info): info.ui.title = self.title if self.model.has_children(): self.selected_node = self.model.get_children()[0] + return True def _get__next_node(self): if self.selected_node is None: diff --git a/examples/tutorials/traitsui_4.0/buttons.py b/examples/tutorials/traitsui_4.0/buttons.py index 4ccd54381..a0c545987 100644 --- a/examples/tutorials/traitsui_4.0/buttons.py +++ b/examples/tutorials/traitsui_4.0/buttons.py @@ -47,8 +47,8 @@ #---------------------------------------------------------------- -from traits.api import * -from traitsui.api import * +from traits.api import Float, HasTraits, Property +from traitsui.api import Item, View #--[Adder Class]---------------------------------------------------------- diff --git a/examples/tutorials/traitsui_4.0/deferred.py b/examples/tutorials/traitsui_4.0/deferred.py index 8bd03c442..31b593b4a 100644 --- a/examples/tutorials/traitsui_4.0/deferred.py +++ b/examples/tutorials/traitsui_4.0/deferred.py @@ -34,8 +34,8 @@ #---------------------------------------------------------------- -from traits.api import * -from traitsui.api import * +from traits.api import Button, HasTraits, Int +from traitsui.api import Item, View #--[Count Class]---------------------------------------------------------- diff --git a/examples/tutorials/traitsui_4.0/editors/led.py b/examples/tutorials/traitsui_4.0/editors/led.py index 80ea2875f..aed843647 100644 --- a/examples/tutorials/traitsui_4.0/editors/led.py +++ b/examples/tutorials/traitsui_4.0/editors/led.py @@ -59,6 +59,7 @@ class LEDDemoHandler(Handler): def init(self, info): self.info = info Thread(target=self._update_counter).start() + return True def closed(self, info, is_ok): self.running = False diff --git a/examples/tutorials/traitsui_4.0/items.py b/examples/tutorials/traitsui_4.0/items.py index e82d41e18..50ea1e538 100644 --- a/examples/tutorials/traitsui_4.0/items.py +++ b/examples/tutorials/traitsui_4.0/items.py @@ -84,15 +84,13 @@ #---------------------------------------------------------------- -from traits.api \ - import * - -from traitsui.api \ - import * +from traits.api import ( + Button, HasTraits, Instance, Int, List, Property, Str, cached_property, +) -from traitsui.table_column \ - import * -from functools import reduce +from traitsui.api import ( + HGroup, Item, ModelView, ObjectColumn, TableEditor, View, VGroup, +) #--[Player Class]--------------------------------------------------------- @@ -118,7 +116,7 @@ class Team(HasTraits): name = Str('') # The players on the team: - players = List(Player) + players = List(Instance(Player)) # The number of players on the team: num_players = Property(observe='players') @@ -139,7 +137,7 @@ class League(HasTraits): name = Str('') # The teams in the league: - teams = List(Team) + teams = List(Instance(Team)) #--[LeagueModelView Class]----------------------------------------------------- @@ -164,9 +162,7 @@ class LeagueModelView(ModelView): def _get_total_hits(self): """ Returns the total number of hits across all teams and players. """ - return 0 - return reduce(add, [reduce(add, [p.hits for p in t.players], 0) - for t in self.model.teams], 0) + return sum(sum(p.hits for p in t.players) for t in self.model.teams) view = View( VGroup( diff --git a/examples/tutorials/traitsui_4.0/model_view.py b/examples/tutorials/traitsui_4.0/model_view.py index 980f5765e..702f7b2a7 100644 --- a/examples/tutorials/traitsui_4.0/model_view.py +++ b/examples/tutorials/traitsui_4.0/model_view.py @@ -97,9 +97,10 @@ #---------------------------------------------------------------- -from traits.api import * -from traitsui.api import * -from traitsui.table_column import * +from traits.api import ( + HasTraits, Instance, List, Property, PrototypedFrom, Str, +) +from traitsui.api import Item, ModelView, ObjectColumn, TableEditor, View #--[Parent Class]--------------------------------------------------------- @@ -118,7 +119,7 @@ class Child(HasTraits): father = Instance(Parent) first_name = Str() - last_name = Delegate('father') + last_name = PrototypedFrom('father') #--[ChildModelView Class]------------------------------------------------- diff --git a/integrationtests/styled_date_editor_test.py b/integrationtests/styled_date_editor_test.py index 28e724e6c..02db226bb 100644 --- a/integrationtests/styled_date_editor_test.py +++ b/integrationtests/styled_date_editor_test.py @@ -10,9 +10,6 @@ from datetime import date -from traits.etsconfig.api import ETSConfig -ETSConfig.toolkit = 'qt4' - from traits.api import Date, Dict, HasTraits, List, observe from traitsui.api import View, Item, StyledDateEditor from traitsui.editors.styled_date_editor import CellFormat diff --git a/integrationtests/ui/buttons_test.py b/integrationtests/ui/buttons_test.py index e0340efc0..91c75a296 100644 --- a/integrationtests/ui/buttons_test.py +++ b/integrationtests/ui/buttons_test.py @@ -8,14 +8,12 @@ # # Thanks for using Enthought open source! -from traits.api \ - import * +from traits.api import Int, Regex, Str +from traitsui.api import ( + Action, CancelButton, Group, Handler, LiveButtons, ModalButtons, OKButton, + View, +) -from traitsui.api \ - import * - -from traitsui.menu \ - import * #------------------------------------------------------------------------- # 'Person' class: @@ -30,7 +28,7 @@ class Person(Handler): name = Str() age = Int() - phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d') + phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') notes = Str() #------------------------------------------------------------------------- diff --git a/integrationtests/ui/enum_dynamic_test.py b/integrationtests/ui/enum_dynamic_test.py index 42ee92cdb..c8f33e8da 100644 --- a/integrationtests/ui/enum_dynamic_test.py +++ b/integrationtests/ui/enum_dynamic_test.py @@ -8,8 +8,8 @@ # # Thanks for using Enthought open source! -from traits.api import * -from traitsui.api import * +from traits.api import HasTraits, List, Str +from traitsui.api import EnumEditor, Item, View def evaluate_value(v): diff --git a/integrationtests/ui/instance_editor_test.py b/integrationtests/ui/instance_editor_test.py index f1d749900..9e40044d8 100644 --- a/integrationtests/ui/instance_editor_test.py +++ b/integrationtests/ui/instance_editor_test.py @@ -8,9 +8,8 @@ # # Thanks for using Enthought open source! -from traits.api import * -from traitsui.api import * -from traitsui.instance_choice import InstanceChoice +from traits.api import HasStrictTraits, Instance, Int, List, Regex, Str +from traitsui.api import InstanceEditor, Item, View #------------------------------------------------------------------------- # 'Person' class: @@ -25,7 +24,7 @@ class Person(HasStrictTraits): name = Str() age = Int() - phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d') + phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') #------------------------------------------------------------------------- # Traits view definition: @@ -61,7 +60,7 @@ class Team(HasStrictTraits): name = Str() captain = Instance(Person) - roster = List(Person) + roster = List(Instance(Person)) #------------------------------------------------------------------------- # Traits view definitions: diff --git a/integrationtests/ui/instance_editor_test2.py b/integrationtests/ui/instance_editor_test2.py index 9368729e1..a5590dcf6 100644 --- a/integrationtests/ui/instance_editor_test2.py +++ b/integrationtests/ui/instance_editor_test2.py @@ -8,10 +8,8 @@ # # Thanks for using Enthought open source! -from traits.api import * -from traitsui.api import * -from traitsui.instance_choice \ - import InstanceChoice, InstanceFactoryChoice +from traits.api import HasStrictTraits, Instance, Int, List, Regex, Str +from traitsui.api import InstanceEditor, InstanceFactoryChoice, Item, View #------------------------------------------------------------------------- # 'Person' class: @@ -26,7 +24,7 @@ class Person(HasStrictTraits): name = Str() age = Int() - phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d') + phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') #------------------------------------------------------------------------- # Traits view definition: diff --git a/integrationtests/ui/instance_editor_test3.py b/integrationtests/ui/instance_editor_test3.py index 7b239533b..5dc3ab6a3 100644 --- a/integrationtests/ui/instance_editor_test3.py +++ b/integrationtests/ui/instance_editor_test3.py @@ -8,9 +8,8 @@ # # Thanks for using Enthought open source! -from traits.api import * -from traitsui.api import * -from traitsui.instance_choice import InstanceChoice +from traits.api import HasStrictTraits, Instance, Int, List, Regex, Str +from traitsui.api import InstanceEditor, Item, View #------------------------------------------------------------------------- # 'Person' class: @@ -25,7 +24,7 @@ class Person(HasStrictTraits): name = Str() age = Int() - phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d') + phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') #------------------------------------------------------------------------- # Traits view definition: @@ -61,7 +60,7 @@ class Team(HasStrictTraits): name = Str() captain = Instance(Person) - roster = List(Person) + roster = List(Instance(Person)) #------------------------------------------------------------------------- # Traits view definitions: diff --git a/integrationtests/ui/instance_editor_test4.py b/integrationtests/ui/instance_editor_test4.py index 9718a509f..3f91fc87f 100644 --- a/integrationtests/ui/instance_editor_test4.py +++ b/integrationtests/ui/instance_editor_test4.py @@ -8,9 +8,8 @@ # # Thanks for using Enthought open source! -from traits.api import * -from traitsui.api import * -from traitsui.instance_choice import InstanceChoice +from traits.api import HasStrictTraits, Instance, Int, List, Regex, Str +from traitsui.api import InstanceEditor, Item, View #------------------------------------------------------------------------- # 'Person' class: @@ -25,7 +24,7 @@ class Person(HasStrictTraits): name = Str() age = Int() - phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d') + phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') #------------------------------------------------------------------------- # Traits view definition: diff --git a/integrationtests/ui/instance_editor_test5.py b/integrationtests/ui/instance_editor_test5.py index 11a85a008..b20b48181 100644 --- a/integrationtests/ui/instance_editor_test5.py +++ b/integrationtests/ui/instance_editor_test5.py @@ -8,10 +8,8 @@ # # Thanks for using Enthought open source! -from traits.api import * -from traitsui.api import * -from traitsui.instance_choice \ - import InstanceChoice, InstanceFactoryChoice +from traits.api import HasStrictTraits, Instance, Int, List, Regex, Str +from traitsui.api import InstanceEditor, InstanceFactoryChoice, Item, View #------------------------------------------------------------------------- # 'Person' class: @@ -26,7 +24,7 @@ class Person(HasStrictTraits): name = Str() age = Int() - phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d') + phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') #------------------------------------------------------------------------- # Traits view definition: diff --git a/integrationtests/ui/instance_editor_test6.py b/integrationtests/ui/instance_editor_test6.py index afa95ace0..8506b28c6 100644 --- a/integrationtests/ui/instance_editor_test6.py +++ b/integrationtests/ui/instance_editor_test6.py @@ -8,10 +8,8 @@ # # Thanks for using Enthought open source! -from traits.api import * -from traitsui.api import * -from traitsui.instance_choice \ - import InstanceChoice, InstanceFactoryChoice +from traits.api import HasStrictTraits, Instance, Int, List, Regex, Str +from traitsui.api import InstanceEditor, InstanceFactoryChoice, Item, View #------------------------------------------------------------------------- # 'Person' class: @@ -26,7 +24,7 @@ class Person(HasStrictTraits): name = Str() age = Int() - phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d') + phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') #------------------------------------------------------------------------- # Traits view definition: diff --git a/integrationtests/ui/list_traits_ui_test.py b/integrationtests/ui/list_traits_ui_test.py index d86ef9256..fed2b2a46 100644 --- a/integrationtests/ui/list_traits_ui_test.py +++ b/integrationtests/ui/list_traits_ui_test.py @@ -38,7 +38,7 @@ class Person(HasStrictTraits): name = Str() age = Int() - phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d') + phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') #------------------------------------------------------------------------- # Traits view definition: diff --git a/integrationtests/ui/set_dynamic_test.py b/integrationtests/ui/set_dynamic_test.py index 1caeb72d0..1a91ad02a 100644 --- a/integrationtests/ui/set_dynamic_test.py +++ b/integrationtests/ui/set_dynamic_test.py @@ -8,8 +8,8 @@ # # Thanks for using Enthought open source! -from traits.api import * -from traitsui.api import * +from traits.api import HasTraits, List, Str +from traitsui.api import Item, SetEditor, View class Team(HasTraits): diff --git a/integrationtests/ui/shell_editor_test.py b/integrationtests/ui/shell_editor_test.py index b51be2b56..9fbbfac10 100644 --- a/integrationtests/ui/shell_editor_test.py +++ b/integrationtests/ui/shell_editor_test.py @@ -8,9 +8,8 @@ # # Thanks for using Enthought open source! -from traits.api import * -from traitsui.api import * -from traitsui.menu import * +from traits.api import Dict, Float, HasPrivateTraits, Int, Str +from traitsui.api import Item, ShellEditor, View #------------------------------------------------------------------------- # 'ShellTest' class: diff --git a/integrationtests/ui/table_editor_test.py b/integrationtests/ui/table_editor_test.py index 96cfb41d8..aaa7554fe 100644 --- a/integrationtests/ui/table_editor_test.py +++ b/integrationtests/ui/table_editor_test.py @@ -38,7 +38,7 @@ class Person(HasStrictTraits): name = Str() age = Int() - phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d') + phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') state = Str() #------------------------------------------------------------------------- diff --git a/integrationtests/ui/table_editor_test2.py b/integrationtests/ui/table_editor_test2.py index c07fc01fd..173a0a779 100644 --- a/integrationtests/ui/table_editor_test2.py +++ b/integrationtests/ui/table_editor_test2.py @@ -31,7 +31,7 @@ class Person(HasStrictTraits): name = Str() age = Int() - phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d') + phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') #------------------------------------------------------------------------- # Traits view definition: diff --git a/setup.cfg b/setup.cfg index edb2c4a94..bc5b25304 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,12 +1,11 @@ [flake8] exclude = docs -per-file-ignores = */api.py:F401,examples/*:H101 +per-file-ignores = */api.py:F401,*/toolkit.py:E402,examples/*:H101 ignore = # codes we ignore as part of ETS style E266,W503,E722,E731,E741 # codes we are ignoring pending clean-up - F401,F402,F403,F405,F406,F811,F841 - E402,E712 + F401,F841 # codes for formatting pending clean-up E116,E121,E123,E126,E127,E128,E203,E226,E241,E251,E265,E272,E302,E303,E305,E306,E501 - W291,W293,W504,W605 + W291,W293,W504 diff --git a/traitsui/examples/demo/Advanced/Dynamic_range_trait_and_editor.py b/traitsui/examples/demo/Advanced/Dynamic_range_trait_and_editor.py index e16c1069e..ffa9ab112 100644 --- a/traitsui/examples/demo/Advanced/Dynamic_range_trait_and_editor.py +++ b/traitsui/examples/demo/Advanced/Dynamic_range_trait_and_editor.py @@ -63,7 +63,6 @@ import logging import sys -logging.basicConfig(stream=sys.stderr) from random \ import choice @@ -75,6 +74,8 @@ from traitsui.api \ import View, VGroup, HGroup, Item, ListEditor, spring +logging.basicConfig(stream=sys.stderr) + #-- The Hotel class ------------------------------------------------------ diff --git a/traitsui/examples/demo/Advanced/List_editor_notebook_selection_demo.py b/traitsui/examples/demo/Advanced/List_editor_notebook_selection_demo.py index ad6550e72..877c26a06 100644 --- a/traitsui/examples/demo/Advanced/List_editor_notebook_selection_demo.py +++ b/traitsui/examples/demo/Advanced/List_editor_notebook_selection_demo.py @@ -42,7 +42,7 @@ class Person(HasStrictTraits): # Trait definitions: name = Str() age = Int() - phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d') + phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') # Traits view definition: traits_view = View('name', 'age', 'phone', diff --git a/traitsui/examples/demo/Extras/LED_display.py b/traitsui/examples/demo/Extras/LED_display.py index e97d4347a..11282cfc8 100644 --- a/traitsui/examples/demo/Extras/LED_display.py +++ b/traitsui/examples/demo/Extras/LED_display.py @@ -44,6 +44,7 @@ class LEDDemoHandler(Handler): def init(self, info): self.info = info Thread(target=self._update_counter).start() + return True def closed(self, info, is_ok): self.running = False diff --git a/traitsui/extras/_demo_legacy.py b/traitsui/extras/_demo_legacy.py index c0e91a17c..f88d5b5d5 100644 --- a/traitsui/extras/_demo_legacy.py +++ b/traitsui/extras/_demo_legacy.py @@ -216,6 +216,7 @@ def init(self, info): demo_file = info.object with _set_stdout(self): demo_file.init() + return True def closed(self, info, is_ok): """ Closes the view. @@ -1008,6 +1009,7 @@ def do_parent(self, event=None): def init(self, info): info.ui.title = self.title + return True def _get__next_node(self): next = None diff --git a/traitsui/qt4/button_editor.py b/traitsui/qt4/button_editor.py index 90474e2e8..33578b242 100644 --- a/traitsui/qt4/button_editor.py +++ b/traitsui/qt4/button_editor.py @@ -27,7 +27,7 @@ from pyface.qt import QtCore, QtGui from pyface.api import Image -from traits.api import Str, List, Str, observe, on_trait_change +from traits.api import List, Str, observe, on_trait_change from .editor import Editor diff --git a/traitsui/tests/test_handler.py b/traitsui/tests/test_handler.py index 023106998..6c17010e6 100644 --- a/traitsui/tests/test_handler.py +++ b/traitsui/tests/test_handler.py @@ -8,10 +8,10 @@ # # Thanks for using Enthought open source! -from unittest import TestCase +from unittest import TestCase, skipIf from pyface.action.api import ActionEvent -from traits.api import HasTraits, Bool +from traits.api import Any, HasTraits, Bool, TraitError from traitsui.api import ( Action, CloseAction, @@ -22,7 +22,7 @@ UI, UndoAction, ) -from traitsui.tests._tools import BaseTestMixin +from traitsui.tests._tools import BaseTestMixin, is_null class PyfaceAction(Action): @@ -65,6 +65,11 @@ class SampleHandler(Handler): help_performed = Bool() + init_return_value = Any(True) + + def init(self, info): + return self.init_return_value + def action_handler(self): self.action_performed = True @@ -287,3 +292,21 @@ def test_help_handler(self): handler.perform(info, action, event) self.assertTrue(handler.help_performed) + + @skipIf(is_null(), "Null toolkit can't create UI") + def test_handler_init_false(self): + object = SampleObject() + handler = SampleHandler(init_return_value=False) + + with self.assertRaises(TraitError): + object.edit_traits(handler=handler) + + @skipIf(is_null(), "Null toolkit can't create UI") + def test_handler_init_none(self): + object = SampleObject() + handler = SampleHandler(init_return_value=None) + + with self.assertWarns(DeprecationWarning): + ui = object.edit_traits(handler=handler) + + ui.dispose() diff --git a/traitsui/tests/test_splitter_prefs_restored.py b/traitsui/tests/test_splitter_prefs_restored.py index 9387e2bdf..3d17e1c30 100644 --- a/traitsui/tests/test_splitter_prefs_restored.py +++ b/traitsui/tests/test_splitter_prefs_restored.py @@ -30,6 +30,7 @@ class TmpClass(Handler): def init(self, ui_info): super().init(ui_info) self.save_prefs(ui_info) + return True def reset_prefs(self, ui_info): """ Reset the split to be equally wide. diff --git a/traitsui/ui.py b/traitsui/ui.py index a55164cc5..908295744 100644 --- a/traitsui/ui.py +++ b/traitsui/ui.py @@ -14,6 +14,7 @@ import shelve import os +from warnings import warn from pyface.ui_traits import Image from traits.api import ( @@ -32,7 +33,6 @@ observe, property_depends_on, ) -from traits.observation.api import trait from traits.trait_base import traits_home, is_str @@ -407,8 +407,16 @@ def prepare_ui(self): # Invoke the handler's 'init' method, and abort if it indicates # failure: - if handler.init(info) == False: + started = handler.init(info) + if started is False: raise TraitError("User interface creation aborted") + elif not isinstance(started, bool): + warn( + "Handler.init() must return True or False, but instead " + f"returned {started}. " + "This will become an error in a future release.", + DeprecationWarning, + ) # For each Handler method whose name is of the form # 'object_name_changed', where 'object' is the name of an object in the diff --git a/traitsui/view.py b/traitsui/view.py index 483e84f10..38e4470d0 100644 --- a/traitsui/view.py +++ b/traitsui/view.py @@ -41,7 +41,6 @@ EditorStyle, ExportType, HelpId, - Image, SequenceTypes, ViewStatus, ) diff --git a/traitsui/wx/table_editor.py b/traitsui/wx/table_editor.py index 95024eaa3..ea2c20180 100644 --- a/traitsui/wx/table_editor.py +++ b/traitsui/wx/table_editor.py @@ -1297,6 +1297,7 @@ def init(self, info): self.filter = info.filter.factory.values[0] self._filter = self.filter self._filter_copy = self.filter.clone_traits() + return True def closed(self, info, is_ok): """ Handles a dialog-based user interface being closed by the user. diff --git a/traitsui/wx/tabular_editor.py b/traitsui/wx/tabular_editor.py index 1c1c9dcc8..454316ea8 100644 --- a/traitsui/wx/tabular_editor.py +++ b/traitsui/wx/tabular_editor.py @@ -29,11 +29,6 @@ Dict, ) -# FIXME: TabularEditor (the editor factory for tabular editors) is a proxy class -# defined here just for backward compatibility. The class has been moved to the -# traitsui.editors.tabular_editor file. -from traitsui.editors.tabular_editor import TabularEditor - from traitsui.ui_traits import Image from traitsui.tabular_adapter import TabularAdapter