Repository: omni-us/jsonargparse
Pull request #352 (2023-08-16 04:46:35)

Add support for decimal.Decimal as a type
## Before submitting

- [x] Did you read the [contributing guideline](https://github.com/omni-us/jsonargparse/blob/main/CONTRIBUTING.rst)?
- [x] 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)


sonarcloud[bot]: Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=omni-us_jsonargparse&pullRequest=352)

[![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=omni-us_jsonargparse&pullRequest=352&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=omni-us_jsonargparse&pullRequest=352&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=omni-us_jsonargparse&pullRequest=352&resolved=false&types=BUG)  
[![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=omni-us_jsonargparse&pullRequest=352&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=omni-us_jsonargparse&pullRequest=352&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=omni-us_jsonargparse&pullRequest=352&resolved=false&types=VULNERABILITY)  
[![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=omni-us_jsonargparse&pullRequest=352&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=omni-us_jsonargparse&pullRequest=352&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=omni-us_jsonargparse&pullRequest=352&resolved=false&types=SECURITY_HOTSPOT)  
[![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=omni-us_jsonargparse&pullRequest=352&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=omni-us_jsonargparse&pullRequest=352&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=omni-us_jsonargparse&pullRequest=352&resolved=false&types=CODE_SMELL)

[![No Coverage information](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/NoCoverageInfo-16px.png 'No Coverage information')](https://sonarcloud.io/component_measures?id=omni-us_jsonargparse&pullRequest=352) No Coverage information  
[![0.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3-16px.png '0.0%')](https://sonarcloud.io/component_measures?id=omni-us_jsonargparse&pullRequest=352&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=omni-us_jsonargparse&pullRequest=352&metric=new_duplicated_lines_density&view=list)


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/omni-us/jsonargparse/pull/352?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us) Report
Patch coverage: **`100.00%`** and project coverage change: **`-0.10%`** :warning:
> Comparison is base [(`8c1b8c6`)](https://app.codecov.io/gh/omni-us/jsonargparse/commit/8c1b8c6cfb9b49583c68a9e3311fe23916124218?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us) 100.00% compared to head [(`f6c0fcf`)](https://app.codecov.io/gh/omni-us/jsonargparse/pull/352?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us) 99.90%.

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


```diff
@@             Coverage Diff             @@
##              main     #352      +/-   ##
===========================================
- Coverage   100.00%   99.90%   -0.10%     
===========================================
  Files           21       21              
  Lines         5549     5550       +1     
===========================================
- Hits          5549     5545       -4     
- Misses           0        5       +5     
```

| Flag | Coverage Δ | |
|---|---|---|
| py3.10 | `?` | |
| py3.10_all | `?` | |
| py3.10_types | `?` | |
| py3.11 | `?` | |
| py3.11_all | `?` | |
| py3.11_types | `?` | |
| py3.6 | `85.97% <100.00%> (+<0.01%)` | :arrow_up: |
| py3.6_all | `97.81% <100.00%> (+<0.01%)` | :arrow_up: |
| py3.7 | `87.95% <100.00%> (+<0.01%)` | :arrow_up: |
| py3.7_all | `99.47% <100.00%> (+<0.01%)` | :arrow_up: |
| py3.7_types | `99.45% <100.00%> (+<0.01%)` | :arrow_up: |
| py3.8 | `88.12% <100.00%> (+<0.01%)` | :arrow_up: |
| py3.8_all | `99.54% <100.00%> (+<0.01%)` | :arrow_up: |
| py3.8_types | `99.53% <100.00%> (+<0.01%)` | :arrow_up: |
| py3.9 | `88.05% <100.00%> (+<0.01%)` | :arrow_up: |
| py3.9_all | `99.54% <100.00%> (+<0.01%)` | :arrow_up: |
| py3.9_types | `?` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Files Changed](https://app.codecov.io/gh/omni-us/jsonargparse/pull/352?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us) | Coverage Δ | |
|---|---|---|
| [jsonargparse/typing.py](https://app.codecov.io/gh/omni-us/jsonargparse/pull/352?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us#diff-anNvbmFyZ3BhcnNlL3R5cGluZy5weQ==) | `100.00% <100.00%> (ø)` | |

... and [2 files with indirect coverage changes](https://app.codecov.io/gh/omni-us/jsonargparse/pull/352/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us)


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/omni-us/jsonargparse/pull/352?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 0c0b0a3..e44b33f 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -18,6 +18,7 @@ v4.24.0 (2023-08-??)
 Added
 ^^^^^
 - New option in ``dump`` for including link targets.
+- Support ``decimal.Decimal`` as a type.
 
 
 v4.23.1 (2023-08-04)
diff --git a/README.rst b/README.rst
index bbf283c..c52569d 100644
--- a/README.rst
+++ b/README.rst
@@ -431,7 +431,7 @@ Some notes about this support are:
   <type>`` instead of ``Union[<type>, <type>]``).
 
 - Fully supported types are: ``str``, ``bool`` (more details in
-  :ref:`boolean-arguments`), ``int``, ``float``, ``complex``,
+  :ref:`boolean-arguments`), ``int``, ``float``, ``Decimal``, ``complex``,
   ``bytes``/``bytearray`` (Base64 encoding), ``range``, ``List`` (more details
   in :ref:`list-append`), ``Iterable``, ``Sequence``, ``Any``, ``Union``,
   ``Optional``, ``Type``, ``Enum``, ``PathLike``, ``UUID``, ``timedelta``,
diff --git a/jsonargparse/typing.py b/jsonargparse/typing.py
index 2af61df..2c2fbca 100644
--- a/jsonargparse/typing.py
+++ b/jsonargparse/typing.py
@@ -360,6 +360,7 @@ Path_drw = path_type("drw", docstring="path to a directory that exists and is re
 
 register_type(os.PathLike, str, str)
 register_type(complex)
+register_type_on_first_use("decimal.Decimal", float)
 register_type_on_first_use("uuid.UUID")
 
 for _path in [pathlib.Path, pathlib.PosixPath, pathlib.WindowsPath]:


Test patch:
diff --git a/jsonargparse_tests/test_typing.py b/jsonargparse_tests/test_typing.py
index 69ffb29..3bfde1a 100644
--- a/jsonargparse_tests/test_typing.py
+++ b/jsonargparse_tests/test_typing.py
@@ -4,6 +4,7 @@ import pickle
 import random
 import uuid
 from datetime import datetime, timedelta
+from decimal import Decimal
 from typing import List, Optional, Union
 
 import pytest
@@ -359,6 +360,14 @@ def test_register_type_on_first_use():
     assert f"{__name__}.RegisterOnFirstUse" not in registration_pending
 
 
+def test_decimal(parser):
+    parser.add_argument("--decimal", type=Decimal)
+    cfg = parser.parse_args(["--decimal=0.1"])
+    assert isinstance(cfg.decimal, Decimal)
+    assert cfg.decimal == Decimal("0.1")
+    assert parser.dump(cfg) == "decimal: 0.1\n"
+
+
 def test_uuid(parser):
     id1 = uuid.uuid4()
     id2 = uuid.uuid4()