Repository: pyqtgraph/pyqtgraph

Issue:
PlotDataItem.setData() raises generic Exception instead of TypeError.
When using setData() method in the context of a try: except statement (plenty possible application since that's the low level way of redrawing fast) one has to resort to a generic Exception catch which looks nooby and can lead to undetected bugs (just what happened to me). setData() should probably be raising more specific exception types, judging by the Exception messages in place these should probably both be TypeErrors.

### Short description
PlotDataItem.setData() raises generic Exception instead of TypeError.

### Code to reproduce 1
```
import pyqtgraph as pg
plotWidget = pg.plot()
plot_data_item = plotWidget.plot()
plot_data_item.setData(None)
```

### Expected behavior
```
TypeError: Invalid data type <class 'NoneType'>
```

### Real behavior
```
Exception: Invalid data type <class 'NoneType'>
```

### Code to reproduce 2
```
import pyqtgraph as pg
plotWidget = pg.plot()
plot_data_item = plotWidget.plot()
plot_data_item.setData(None,None)
```

### Expected behavior
```
TypeError: When passing two unnamed arguments, both must be a list or array of values. (got <class 'NoneType'>, <class 'NoneType'>)
```

### Real behavior
```
Exception: When passing two unnamed arguments, both must be a list or array of values. (got <class 'NoneType'>, <class 'NoneType'>)
```

### Tested environment(s)
  - python=3.7.6=cpython_h60c2a47_6
  - pyqtgraph=0.11.0=pyh9f0ad1d_0 (this is certainly not the latest but the raise Except statements are there in master still)

PS: There is another 'raise Exception' in PlotDataItem.py relating to wrong dimensioning on numpy arrays. Here some specific class ArrayDimensionError(Exception) could be created or perhaps numpy readily implements some applicable exception type.


Discussion:
Hello @mjablons1 and @j9ac9k , I would like to work on this issue if no one else is already assigned to it.
@Nibba2018 please have at it!

Resolving patch:
diff --git a/pyqtgraph/graphicsItems/PlotDataItem.py b/pyqtgraph/graphicsItems/PlotDataItem.py
--- a/pyqtgraph/graphicsItems/PlotDataItem.py
+++ b/pyqtgraph/graphicsItems/PlotDataItem.py
@@ -707,7 +707,7 @@ def setData(self, *args, **kargs):
                     x = np.array(data['x'])
                 if 'y' in data:
                     y = np.array(data['y'])
-            elif dt ==  'listOfDicts':
+            elif dt == 'listOfDicts':
                 if 'x' in data[0]:
                     x = np.array([d.get('x',None) for d in data])
                 if 'y' in data[0]:
@@ -719,13 +719,13 @@ def setData(self, *args, **kargs):
                 y = data.view(np.ndarray)
                 x = data.xvals(0).view(np.ndarray)
             else:
-                raise Exception('Invalid data type %s' % type(data))
+                raise TypeError('Invalid data type %s' % type(data))
 
         elif len(args) == 2:
             seq = ('listOfValues', 'MetaArray', 'empty')
             dtyp = dataType(args[0]), dataType(args[1])
             if dtyp[0] not in seq or dtyp[1] not in seq:
-                raise Exception('When passing two unnamed arguments, both must be a list or array of values. (got %s, %s)' % (str(type(args[0])), str(type(args[1]))))
+                raise TypeError('When passing two unnamed arguments, both must be a list or array of values. (got %s, %s)' % (str(type(args[0])), str(type(args[1]))))
             if not isinstance(args[0], np.ndarray):
                 #x = np.array(args[0])
                 if dtyp[0] == 'MetaArray':
@@ -1214,7 +1214,7 @@ def dataType(obj):
             elif obj.ndim == 2 and obj.dtype.names is None and obj.shape[1] == 2:
                 return 'Nx2array'
             else:
-                raise Exception('array shape must be (N,) or (N,2); got %s instead' % str(obj.shape))
+                raise ValueError('array shape must be (N,) or (N,2); got %s instead' % str(obj.shape))
         elif isinstance(first, dict):
             return 'listOfDicts'
         else:


Test patch: