Repository: pydicom/pydicom

Issue:
With tag (0040, 9096) got exception: 'NoneType' object is not callable
** Describe the error*** Clear and concise description of what the error is.

When I run dcm.save_as(filename, write_like_original)  , it shows this error
```
With tag (0040, 9096) got exception: 'NoneType' object is not callable
Traceback (most recent call last):
  File "C:\Users\saintkay\IdeaProjects\dcm\venv\lib\site-packages\pydicom\tag.py", line 28, in tag_in_exception
    yield
  File "C:\Users\saintkay\IdeaProjects\dcm\venv\lib\site-packages\pydicom\filewriter.py", line 660, in write_dataset
    write_data_element(fp, dataset.get_item(tag), dataset_encoding)
  File "C:\Users\saintkay\IdeaProjects\dcm\venv\lib\site-packages\pydicom\filewriter.py", line 560, in write_data_element
    fn(buffer, elem, encodings=encodings)  # type: ignore[operator]
  File "C:\Users\saintkay\IdeaProjects\dcm\venv\lib\site-packages\pydicom\filewriter.py", line 698, in write_sequence
    write_sequence_item(fp, ds, encodings)
  File "C:\Users\saintkay\IdeaProjects\dcm\venv\lib\site-packages\pydicom\filewriter.py", line 724, in write_sequence_item
    write_dataset(fp, dataset, parent_encoding=encodings)
  File "C:\Users\saintkay\IdeaProjects\dcm\venv\lib\site-packages\pydicom\filewriter.py", line 643, in write_dataset
    dataset = correct_ambiguous_vr(dataset, fp.is_little_endian)
  File "C:\Users\saintkay\IdeaProjects\dcm\venv\lib\site-packages\pydicom\filewriter.py", line 248, in correct_ambiguous_vr
    correct_ambiguous_vr_element(elem, ds, is_little_endian)
  File "C:\Users\saintkay\IdeaProjects\dcm\venv\lib\site-packages\pydicom\filewriter.py", line 204, in correct_ambiguous_vr_element
    _correct_ambiguous_vr_element(elem, ds, is_little_endian)
  File "C:\Users\saintkay\IdeaProjects\dcm\venv\lib\site-packages\pydicom\filewriter.py", line 100, in _correct_ambiguous_vr_element
    and ds.parent_seq().parent_dataset()   # type: ignore
TypeError: 'NoneType' object is not callable
```

** Expected Behavior*** What you expect to happen (include DICOM standard reference if relevant).
Run with no error

** Steps to Reproduce*** How to reproduce the problem. Please include a minimum working code example, backtracking (if available) and an anonymized DICOM dataset (if relevant).

is error
```
dicom= pydicom.dcmread('1.dcm')
dicom.save_as('2.dcm', dicom)
```

and I modified the code
filewriter.py
```
'PixelRepresentation' not in ds
and ds.parent_seq is not None
and ds.parent_seq().parent_dataset is not None  # I added a new one
and ds.parent_seq().parent_dataset() 
```

this my dcm file
[1.zip](https://github.com/pydicom/pydicom/files/12075578/1.zip)


Your environment **** Please use the ``pydicom.env_info`` module (available in **pydicom 2 or later ****) to gather information about your environment and paste it into the problem:

``bash $ python -m pydicom.env_info ````
|module|version|
|-|-|
|python|3.10.11|
|pydicom|2.4.1|

Discussion:
darcymason: This may still need further investigation -- the call to convert the ambiguous VR shouldn't be necessary for writing (the original issues showed the error on file writing) as the file is implicit VR.  But also, this fix just ignores the actual conversion, essentially, by not checking the (unknown) parent for tags needed to disambiguate.  I'll open another issue to keep it in mind for eventual deeper fix. 

Also will cherry-pick into 2.4.X branch and publish a patch release.
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/pydicom/pydicom/pull/1842?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydicom) Report
> Merging [#1842](https://app.codecov.io/gh/pydicom/pydicom/pull/1842?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydicom) (1a6a9aa) into [main](https://app.codecov.io/gh/pydicom/pydicom/commit/04c7f2eac3e4e797c8ac7ca4b7e8a8afd64768be?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydicom) (04c7f2e) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

```diff
@@           Coverage Diff           @@
##             main    #1842   +/-   ##
=======================================
  Coverage   97.46%   97.46%           
=======================================
  Files          65       65           
  Lines       10851    10854    +3     
=======================================
+ Hits        10576    10579    +3     
  Misses        275      275           
```


| [Impacted Files](https://app.codecov.io/gh/pydicom/pydicom/pull/1842?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydicom) | Coverage Δ | |
|---|---|---|
| [src/pydicom/filewriter.py](https://app.codecov.io/gh/pydicom/pydicom/pull/1842?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydicom#diff-c3JjL3B5ZGljb20vZmlsZXdyaXRlci5weQ==) | `97.80% <ø> (ø)` | |
| [src/pydicom/pixel\_data\_handlers/rle\_handler.py](https://app.codecov.io/gh/pydicom/pydicom/pull/1842?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydicom#diff-c3JjL3B5ZGljb20vcGl4ZWxfZGF0YV9oYW5kbGVycy9ybGVfaGFuZGxlci5weQ==) | `100.00% <ø> (ø)` | |
| [src/pydicom/util/codify.py](https://app.codecov.io/gh/pydicom/pydicom/pull/1842?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydicom#diff-c3JjL3B5ZGljb20vdXRpbC9jb2RpZnkucHk=) | `97.63% <100.00%> (ø)` | |
| [src/pydicom/values.py](https://app.codecov.io/gh/pydicom/pydicom/pull/1842?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydicom#diff-c3JjL3B5ZGljb20vdmFsdWVzLnB5) | `98.07% <100.00%> (+0.51%)` | :arrow_up: |

... and [1 file with indirect coverage changes](https://app.codecov.io/gh/pydicom/pydicom/pull/1842/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydicom)

:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pydicom)

darcymason: Thanks, @mrbean-bremen 

Patch:
diff --git a/src/pydicom/filewriter.py b/src/pydicom/filewriter.py
index acd31f105..a25273625 100644
--- a/src/pydicom/filewriter.py
+++ b/src/pydicom/filewriter.py
@@ -122,6 +122,7 @@ def _correct_ambiguous_vr_element(
         while (
             "PixelRepresentation" not in ds
             and ds.parent_seq is not None
+            and ds.parent_seq().parent_dataset is not None  # type: ignore[union-attr]
             and ds.parent_seq().parent_dataset()  # type: ignore
         ):
             # Make weakrefs into strong refs (locally here) by calling () them


Test patch:
diff --git a/tests/test_filewriter.py b/tests/test_filewriter.py
index b55063b00..26a2a7b04 100644
--- a/tests/test_filewriter.py
+++ b/tests/test_filewriter.py
@@ -167,6 +167,29 @@ class TestWriteFile:
         them identical (JPEG2K file)."""
         self.compare(jpeg_name)
 
+    def test_None_parent(self):
+        """Ensure can write nested sequence with no parent dataset"""
+        # from issues 1836, 1838, 1839
+
+        # (0040,9096)  Real World Value Mapping Sequence  1 item(s) ----
+        #    (0040,9211) Real World Value Last Value Mapped  US: 8699
+        byts = (
+            b"\0" * 128
+            + b"DICM"
+            + bytes.fromhex(
+                "4000 9690 FFFFFFFF"  # (0040,9096) Sequence undefined length
+                "  FEFF 00E0 FFFFFFFF"  # Sequence Item undefined length
+                "    4000 1192 02000000"  # (0040,9211) length 2
+                "    FB 21                  "  # value
+                "  FEFF 0DE0 00000000"  # Item Delimiter
+                "FEFF DDE0 00000000"  # Sequence Delimiter
+            )
+        )
+
+        ds = dcmread(BytesIO(byts))
+        # original bug raises 'NoneType' object is not callable on decode
+        ds.decode()
+
     def test_pathlib_path_filename(self):
         """Check that file can be written using pathlib.Path"""
         ds = dcmread(Path(ct_name))