Repository: pydicom/pydicom

Issue:
Empty data elements with value representation SQ are set to None
**Describe the bug**
In the current `master`, empty data elements are not read correctly from files. The attribute value is set to `None` instead of `[]`.

**Expected behavior**
Create empty list `[]` for empty sequence, i.e., a sequence with zero items.

**Steps To Reproduce**
```python
import pydicom
ds = pydicom.Dataset()
ds.AcquisitionContextSequence = []
print(ds)
ds.is_little_endian = True
ds.is_implicit_VR = True
ds.save_as('/tmp/test.dcm')

reloaded_ds = pydicom.dcmread('/tmp/test.dcm', force=True)
print(reloaded_ds)
```
This prints:
```
(0040, 0555)  Acquisition Context Sequence   0 item(s) ----
...
TypeError: With tag (0040, 0555) got exception: object of type 'NoneType' has no len()
Traceback (most recent call last):
  File "/private/tmp/pydicom/pydicom/tag.py", line 30, in tag_in_exception
    yield
  File "/private/tmp/pydicom/pydicom/dataset.py", line 1599, in _pretty_str
    len(data_element.value)))
TypeError: object of type 'NoneType' has no len()
```

**Your environment**
```
Darwin-18.6.0-x86_64-i386-64bit
Python  3.7.3 (default, Mar 27 2019, 09:23:15)
[Clang 10.0.1 (clang-1001.0.46.3)]
pydicom  1.4.0.dev0
```

Discussion:
pep8speaks: Hello @mrbean-bremen! Thanks for opening this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:

* In the file [`pydicom/config.py`](https://github.com/pydicom/pydicom/blob/1100467c86fa0911ecd4c9e0657bdb6cb74f632c/pydicom/config.py):

> [Line 91:74](https://github.com/pydicom/pydicom/blob/1100467c86fa0911ecd4c9e0657bdb6cb74f632c/pydicom/config.py#L91): [W291](https://duckduckgo.com/?q=pep8%20W291) trailing whitespace
> [Line 92:71](https://github.com/pydicom/pydicom/blob/1100467c86fa0911ecd4c9e0657bdb6cb74f632c/pydicom/config.py#L92): [W291](https://duckduckgo.com/?q=pep8%20W291) trailing whitespace






darcymason: > which had been discussed in #896, though with no clear conclusion, as far as I can see

Just had a quick read through that again... I thought there was more discussion about sequences (perhaps some other issue/PR?).  I personally like the empty list (and the `is_empty` concept that was discussed, which can always work), but don't have time today to properly look this through and answer.  Will look further over the weekend.

Patch:
diff --git a/pydicom/config.py b/pydicom/config.py
index 00f63d5d8..421799645 100644
--- a/pydicom/config.py
+++ b/pydicom/config.py
@@ -87,9 +87,10 @@ Default ``False``
 """
 
 use_none_as_empty_text_VR_value = False
-""" If ``True``, the value of decoded empty data element is always ``None``.
-If ``False`` (the default), the value of an empty data element with
-a text VR is an empty string, for all other VRs it is also ``None``.
+""" If ``True``, the value of a decoded empty data element with
+a text VR is ``None``, otherwise (the default), it is is an empty string.
+For all other VRs the behavior does not change - the value is en empty
+list for VR 'SQ' and ``None`` for all other VRs.
 Note that the default of this value will change to ``True`` in version 2.0.
 """
 
diff --git a/pydicom/dataelem.py b/pydicom/dataelem.py
index 8dfe3e2f2..5e090ea06 100644
--- a/pydicom/dataelem.py
+++ b/pydicom/dataelem.py
@@ -48,10 +48,12 @@ def empty_value_for_VR(VR, raw=False):
 
     The behavior of this property depends on the setting of
     :attr:`config.use_none_as_empty_value`. If that is set to ``True``,
-    an empty value is always represented by ``None``, otherwise it depends
-    on `VR`. For text VRs (this includes 'AE', 'AS', 'CS', 'DA', 'DT', 'LO',
-    'LT', 'PN', 'SH', 'ST', 'TM', 'UC', 'UI', 'UR' and 'UT') an empty string
-    is used as empty value representation, for all other VRs, ``None``.
+    an empty value is represented by ``None`` (except for VR 'SQ'), otherwise
+    it depends on `VR`. For text VRs (this includes 'AE', 'AS', 'CS', 'DA',
+    'DT', 'LO', 'LT', 'PN', 'SH', 'ST', 'TM', 'UC', 'UI', 'UR' and 'UT') an
+    empty string is used as empty value representation, for all other VRs
+    except 'SQ', ``None``. For empty sequence values (VR 'SQ') an empty list
+    is used in all cases.
     Note that this is used only if decoding the element - it is always
     possible to set the value to another empty value representation,
     which will be preserved during the element object lifetime.
@@ -67,10 +69,12 @@ def empty_value_for_VR(VR, raw=False):
 
     Returns
     -------
-    str or bytes or None
+    str or bytes or None or list
         The value a data element with `VR` is assigned on decoding
         if it is empty.
     """
+    if VR == 'SQ':
+        return []
     if config.use_none_as_empty_text_VR_value:
         return None
     if VR in ('AE', 'AS', 'CS', 'DA', 'DT', 'LO', 'LT',


Test patch:
diff --git a/pydicom/tests/test_dataelem.py b/pydicom/tests/test_dataelem.py
index 5ff1611f0..f03e7f47d 100644
--- a/pydicom/tests/test_dataelem.py
+++ b/pydicom/tests/test_dataelem.py
@@ -503,6 +503,23 @@ class TestDataElement(object):
             check_empty_binary_element(MultiValue(int, []))
             check_empty_binary_element(None)
 
+    def test_empty_sequence_is_handled_as_array(self):
+        ds = Dataset()
+        ds.AcquisitionContextSequence = []
+        elem = ds['AcquisitionContextSequence']
+        assert bool(elem.value) is False
+        assert 0 == elem.VM
+        assert elem.value == []
+
+        fp = DicomBytesIO()
+        fp.is_little_endian = True
+        fp.is_implicit_VR = True
+        filewriter.write_dataset(fp, ds)
+        ds_read = dcmread(fp, force=True)
+        elem = ds_read['AcquisitionContextSequence']
+        assert 0 == elem.VM
+        assert elem.value == []
+
 
 class TestRawDataElement(object):
     """Tests for dataelem.RawDataElement."""