Skip to content

Commit

Permalink
Fix/flake8 code issues (#1757)
Browse files Browse the repository at this point in the history
* 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 <rporuri@enthought.com>

Co-authored-by: Poruri Sai Rahul <rporuri@enthought.com>
  • Loading branch information
corranwebster and Poruri Sai Rahul authored Sep 23, 2021
1 parent 9f4acb8 commit 114b291
Show file tree
Hide file tree
Showing 36 changed files with 113 additions and 96 deletions.
5 changes: 2 additions & 3 deletions docs/source/tutorials/code_snippets/code_block1.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
7 changes: 5 additions & 2 deletions docs/source/tutorials/code_snippets/container.py
Original file line number Diff line number Diff line change
@@ -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 """
Expand All @@ -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, ())
Expand All @@ -28,5 +30,6 @@ class Container(HasTraits):
Item('display', style='custom', show_label=False, ),
)


Container().configure_traits()

2 changes: 1 addition & 1 deletion docs/source/tutorials/code_snippets/echo_box.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# echo_box.py

from traits.api import *
from traits.api import HasTraits, Str

class EchoBox(HasTraits):
input = Str()
Expand Down
2 changes: 1 addition & 1 deletion docs/source/tutorials/code_snippets/event_loop.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
2 changes: 2 additions & 0 deletions ets-demo/etsdemo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions examples/tutorials/traitsui_4.0/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@

#--<Imports>--------------------------------------------------------------

from traits.api import *
from traitsui.api import *
from traits.api import Float, HasTraits, Property
from traitsui.api import Item, View

#--[Adder Class]----------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions examples/tutorials/traitsui_4.0/deferred.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@

#--<Imports>--------------------------------------------------------------

from traits.api import *
from traitsui.api import *
from traits.api import Button, HasTraits, Int
from traitsui.api import Item, View

#--[Count Class]----------------------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions examples/tutorials/traitsui_4.0/editors/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 9 additions & 13 deletions examples/tutorials/traitsui_4.0/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,13 @@

#--<Imports>--------------------------------------------------------------

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]---------------------------------------------------------

Expand All @@ -118,7 +116,7 @@ class Team(HasTraits):
name = Str('<new team>')

# The players on the team:
players = List(Player)
players = List(Instance(Player))

# The number of players on the team:
num_players = Property(observe='players')
Expand All @@ -139,7 +137,7 @@ class League(HasTraits):
name = Str('<new league>')

# The teams in the league:
teams = List(Team)
teams = List(Instance(Team))

#--[LeagueModelView Class]-----------------------------------------------------

Expand All @@ -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(
Expand Down
9 changes: 5 additions & 4 deletions examples/tutorials/traitsui_4.0/model_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@

#--<Imports>--------------------------------------------------------------

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]---------------------------------------------------------

Expand All @@ -118,7 +119,7 @@ class Child(HasTraits):
father = Instance(Parent)

first_name = Str()
last_name = Delegate('father')
last_name = PrototypedFrom('father')

#--[ChildModelView Class]-------------------------------------------------

Expand Down
3 changes: 0 additions & 3 deletions integrationtests/styled_date_editor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 6 additions & 8 deletions integrationtests/ui/buttons_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()

#-------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions integrationtests/ui/enum_dynamic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 4 additions & 5 deletions integrationtests/ui/instance_editor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -61,7 +60,7 @@ class Team(HasStrictTraits):

name = Str()
captain = Instance(Person)
roster = List(Person)
roster = List(Instance(Person))

#-------------------------------------------------------------------------
# Traits view definitions:
Expand Down
8 changes: 3 additions & 5 deletions integrationtests/ui/instance_editor_test2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
9 changes: 4 additions & 5 deletions integrationtests/ui/instance_editor_test3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -61,7 +60,7 @@ class Team(HasStrictTraits):

name = Str()
captain = Instance(Person)
roster = List(Person)
roster = List(Instance(Person))

#-------------------------------------------------------------------------
# Traits view definitions:
Expand Down
7 changes: 3 additions & 4 deletions integrationtests/ui/instance_editor_test4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
8 changes: 3 additions & 5 deletions integrationtests/ui/instance_editor_test5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
8 changes: 3 additions & 5 deletions integrationtests/ui/instance_editor_test6.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion integrationtests/ui/list_traits_ui_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions integrationtests/ui/set_dynamic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading

0 comments on commit 114b291

Please sign in to comment.