Repository: omni-us/jsonargparse
Pull request #656 (2024-12-27 08:58:08)

Fail when Namespace is used as a type to make it clear about being unsupported
## What does this PR do?

Related to #653 and #345

## Before submitting

- [x] Did you read the [contributing guideline](https://github.com/omni-us/jsonargparse/blob/main/CONTRIBUTING.rst)?
- [n/a] Did you update **the documentation**? (readme and public docstrings)
- [x] Did you write **unit tests** such that there is 100% coverage on related code? (required for bug fixes and new features)
- [x] Did you verify that new and existing **tests pass locally**?
- [x] Did you make sure that all changes preserve **backward compatibility**?
- [x] Did you update **the CHANGELOG**? (not for typos, docs, test updates, or minor internal changes/refactors)


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/omni-us/jsonargparse/pull/656?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 99.90%. Comparing base [(`6fcae42`)](https://app.codecov.io/gh/omni-us/jsonargparse/commit/6fcae422e17a5256c02533efffb3e62472acf354?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us) to head [(`57fa412`)](https://app.codecov.io/gh/omni-us/jsonargparse/commit/57fa412482fa978492c06a0e8d2490a2a5ba6e55?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us).

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main     #656   +/-   ##
=======================================
  Coverage   99.90%   99.90%           
=======================================
  Files          22       22           
  Lines        6559     6559           
=======================================
  Hits         6553     6553           
  Misses          6        6           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/omni-us/jsonargparse/pull/656?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us).


Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 50a762a..0c3153f 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -31,6 +31,8 @@ Changed
   parameter of ``ArgumentParser.{dump, save, get_defaults}`` renamed to
   ``skip_validation`` (`#639
   <https://github.com/omni-us/jsonargparse/pull/639>`__).
+- Fail when ``Namespace`` is used as a type to make it clear about being
+  unsupported (`#656 <https://github.com/omni-us/jsonargparse/pull/656>`__).
 
 Fixed
 ^^^^^
diff --git a/jsonargparse/_typehints.py b/jsonargparse/_typehints.py
index d3a89d0..bd6b2f7 100644
--- a/jsonargparse/_typehints.py
+++ b/jsonargparse/_typehints.py
@@ -299,6 +299,9 @@ class ActionTypeHint(Action):
         """Whether the given type hint is supported."""
         typehint = get_unaliased_type(typehint)
 
+        if is_subclass(typehint, Namespace):
+            raise ValueError("jsonargparse.Namespace is only intended for parsing results and not supported as a type.")
+
         supported = (
             typehint in root_types
             or get_typehint_origin(typehint) in root_types


Test patch:
diff --git a/jsonargparse_tests/test_typehints.py b/jsonargparse_tests/test_typehints.py
index 1be16ec..16ae809 100644
--- a/jsonargparse_tests/test_typehints.py
+++ b/jsonargparse_tests/test_typehints.py
@@ -67,6 +67,12 @@ def test_add_argument_failure_given_type_and_action(parser):
     ctx.match("Providing both type and action not allowed")
 
 
+@pytest.mark.parametrize("typehint", [Namespace, Optional[Namespace], Union[int, Namespace], List[Namespace]])
+def test_namespace_unsupported_as_type(parser, typehint):
+    with pytest.raises(ValueError, match="Namespace .* not supported as a type"):
+        parser.add_argument("--ns", type=typehint)
+
+
 # basic types tests