pgzero icon indicating copy to clipboard operation
pgzero copied to clipboard

Better detection and error messages for invalid values during `Actor` initialization

Open charlesej opened this issue 7 years ago • 0 comments

Better detection and error messages for invalid values during Actor initialization would be useful to aid users in debugging.

Expected result: Better and consistent error messages to aid in debugging and catch obvious invalid values.

Actual result: The error messages are confusing (for beginner programmers) and inconsistent. And in one case the error is not detected at all.

Here are some examples of invalid pos values. Some of these may be easier than others to debug for beginners.

alien = Actor('alien', pos=1)
# TypeError: cannot unpack non-iterable int object
alien = Actor('alien', pos=(0, 1, 2))
# ValueError: too many values to unpack (expected 2)
alien = Actor('alien', pos=([0], [0]))
# TypeError: unsupported operand type(s) for -: 'list' and 'float'
alien = Actor('alien', pos=('x', 'y'))
# TypeError: unsupported operand type(s) for -: 'str' and 'float'

These invalid topleft values are not detected until drawn (while the above pos errors are detected when the actor is initialized). They also give different errors than pos does when set to the same invalid values.

alien = Actor('alien', topleft=([0], [0]))
def draw():
    alien.draw()
# TypeError: invalid destination position for blit
alien = Actor('alien', topleft=('x', 'y'))
def draw():
    alien.draw()
# TypeError: invalid destination position for blit

This invalid pos value is not detected at all.

alien = Actor('alien', pos=0)
def draw():
    alien.draw()

System details:

  • os: windows 10 (64bit)
  • python: 3.7.2 (64bit)
  • pygame: 1.9.4
  • pgzero: 1.2 (also master branch at ef963beaa22db89cb011695e96587bb268cbec2a)
  • numpy: 1.15.4

Related to #170.

charlesej avatar Jan 14 '19 19:01 charlesej