morphir 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. morphir-0.0.1/.gitignore +75 -0
  2. morphir-0.0.1/LICENSE +201 -0
  3. morphir-0.0.1/PKG-INFO +411 -0
  4. morphir-0.0.1/README.md +388 -0
  5. morphir-0.0.1/pyproject.toml +36 -0
  6. morphir-0.0.1/src/morphir/__init__.py +14 -0
  7. morphir-0.0.1/src/morphir/ir/__init__.py +110 -0
  8. morphir-0.0.1/src/morphir/ir/access_controlled.py +15 -0
  9. morphir-0.0.1/src/morphir/ir/decorations.py +19 -0
  10. morphir-0.0.1/src/morphir/ir/diagnostics.py +105 -0
  11. morphir-0.0.1/src/morphir/ir/distribution.py +21 -0
  12. morphir-0.0.1/src/morphir/ir/document.py +37 -0
  13. morphir-0.0.1/src/morphir/ir/documented.py +11 -0
  14. morphir-0.0.1/src/morphir/ir/format_version.py +483 -0
  15. morphir-0.0.1/src/morphir/ir/fqname.py +9 -0
  16. morphir-0.0.1/src/morphir/ir/json.py +11 -0
  17. morphir-0.0.1/src/morphir/ir/literal.py +25 -0
  18. morphir-0.0.1/src/morphir/ir/meta.py +11 -0
  19. morphir-0.0.1/src/morphir/ir/migrate.py +1267 -0
  20. morphir-0.0.1/src/morphir/ir/module.py +17 -0
  21. morphir-0.0.1/src/morphir/ir/name.py +27 -0
  22. morphir-0.0.1/src/morphir/ir/package.py +15 -0
  23. morphir-0.0.1/src/morphir/ir/path.py +25 -0
  24. morphir-0.0.1/src/morphir/ir/qname.py +9 -0
  25. morphir-0.0.1/src/morphir/ir/ref.py +17 -0
  26. morphir-0.0.1/src/morphir/ir/type.py +37 -0
  27. morphir-0.0.1/src/morphir/ir/type_constraints.py +33 -0
  28. morphir-0.0.1/src/morphir/ir/type_def.py +19 -0
  29. morphir-0.0.1/src/morphir/ir/type_spec.py +25 -0
  30. morphir-0.0.1/src/morphir/ir/v3/__init__.py +64 -0
  31. morphir-0.0.1/src/morphir/ir/v3/_legacy_value.py +201 -0
  32. morphir-0.0.1/src/morphir/ir/v3/access_controlled.py +31 -0
  33. morphir-0.0.1/src/morphir/ir/v3/codec.py +1275 -0
  34. morphir-0.0.1/src/morphir/ir/v3/codec_v1.py +491 -0
  35. morphir-0.0.1/src/morphir/ir/v3/codec_v2.py +187 -0
  36. morphir-0.0.1/src/morphir/ir/v3/distribution.py +42 -0
  37. morphir-0.0.1/src/morphir/ir/v3/documented.py +13 -0
  38. morphir-0.0.1/src/morphir/ir/v3/fqname.py +30 -0
  39. morphir-0.0.1/src/morphir/ir/v3/literal.py +46 -0
  40. morphir-0.0.1/src/morphir/ir/v3/module.py +32 -0
  41. morphir-0.0.1/src/morphir/ir/v3/name.py +42 -0
  42. morphir-0.0.1/src/morphir/ir/v3/package.py +23 -0
  43. morphir-0.0.1/src/morphir/ir/v3/path.py +29 -0
  44. morphir-0.0.1/src/morphir/ir/v3/qname.py +16 -0
  45. morphir-0.0.1/src/morphir/ir/v3/type.py +126 -0
  46. morphir-0.0.1/src/morphir/ir/v3/value.py +235 -0
  47. morphir-0.0.1/src/morphir/ir/v4/__init__.py +160 -0
  48. morphir-0.0.1/src/morphir/ir/v4/access_controlled.py +17 -0
  49. morphir-0.0.1/src/morphir/ir/v4/annotation.py +75 -0
  50. morphir-0.0.1/src/morphir/ir/v4/codec.py +2161 -0
  51. morphir-0.0.1/src/morphir/ir/v4/decorations.py +88 -0
  52. morphir-0.0.1/src/morphir/ir/v4/distribution.py +56 -0
  53. morphir-0.0.1/src/morphir/ir/v4/document.py +69 -0
  54. morphir-0.0.1/src/morphir/ir/v4/documented.py +10 -0
  55. morphir-0.0.1/src/morphir/ir/v4/fqname.py +69 -0
  56. morphir-0.0.1/src/morphir/ir/v4/incompleteness.py +78 -0
  57. morphir-0.0.1/src/morphir/ir/v4/json.py +103 -0
  58. morphir-0.0.1/src/morphir/ir/v4/literal.py +51 -0
  59. morphir-0.0.1/src/morphir/ir/v4/meta.py +27 -0
  60. morphir-0.0.1/src/morphir/ir/v4/module.py +30 -0
  61. morphir-0.0.1/src/morphir/ir/v4/name.py +69 -0
  62. morphir-0.0.1/src/morphir/ir/v4/package.py +19 -0
  63. morphir-0.0.1/src/morphir/ir/v4/path.py +50 -0
  64. morphir-0.0.1/src/morphir/ir/v4/qname.py +34 -0
  65. morphir-0.0.1/src/morphir/ir/v4/ref.py +35 -0
  66. morphir-0.0.1/src/morphir/ir/v4/type.py +140 -0
  67. morphir-0.0.1/src/morphir/ir/v4/type_constraints.py +91 -0
  68. morphir-0.0.1/src/morphir/ir/v4/type_def.py +38 -0
  69. morphir-0.0.1/src/morphir/ir/v4/type_spec.py +64 -0
  70. morphir-0.0.1/src/morphir/ir/v4/value.py +480 -0
  71. morphir-0.0.1/src/morphir/ir/value.py +81 -0
  72. morphir-0.0.1/src/morphir/py.typed +0 -0
  73. morphir-0.0.1/src/morphir/sdk/__init__.py +110 -0
  74. morphir-0.0.1/src/morphir/sdk/_compare.py +187 -0
  75. morphir-0.0.1/src/morphir/sdk/aggregate.py +421 -0
  76. morphir-0.0.1/src/morphir/sdk/basics.py +504 -0
  77. morphir-0.0.1/src/morphir/sdk/char.py +110 -0
  78. morphir-0.0.1/src/morphir/sdk/decimal.py +311 -0
  79. morphir-0.0.1/src/morphir/sdk/dict.py +336 -0
  80. morphir-0.0.1/src/morphir/sdk/instant.py +118 -0
  81. morphir-0.0.1/src/morphir/sdk/int.py +86 -0
  82. morphir-0.0.1/src/morphir/sdk/key.py +522 -0
  83. morphir-0.0.1/src/morphir/sdk/list.py +356 -0
  84. morphir-0.0.1/src/morphir/sdk/local_date.py +436 -0
  85. morphir-0.0.1/src/morphir/sdk/local_time.py +267 -0
  86. morphir-0.0.1/src/morphir/sdk/maybe.py +135 -0
  87. morphir-0.0.1/src/morphir/sdk/number.py +237 -0
  88. morphir-0.0.1/src/morphir/sdk/regex.py +287 -0
  89. morphir-0.0.1/src/morphir/sdk/result.py +162 -0
  90. morphir-0.0.1/src/morphir/sdk/result_list.py +169 -0
  91. morphir-0.0.1/src/morphir/sdk/rule.py +103 -0
  92. morphir-0.0.1/src/morphir/sdk/set.py +214 -0
  93. morphir-0.0.1/src/morphir/sdk/stateful_app.py +49 -0
  94. morphir-0.0.1/src/morphir/sdk/string.py +427 -0
  95. morphir-0.0.1/src/morphir/sdk/tuple.py +43 -0
  96. morphir-0.0.1/src/morphir/sdk/uuid.py +209 -0
@@ -0,0 +1,75 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ build/
8
+ develop-eggs/
9
+ dist/
10
+ downloads/
11
+ eggs/
12
+ .eggs/
13
+ lib/
14
+ lib64/
15
+ parts/
16
+ sdist/
17
+ var/
18
+ wheels/
19
+ *.egg-info/
20
+ .installed.cfg
21
+ *.egg
22
+
23
+ # Virtual environments
24
+ .venv/
25
+ venv/
26
+ ENV/
27
+ env/
28
+
29
+ # IDE
30
+ .idea/
31
+ .vscode/
32
+ *.swp
33
+ *.swo
34
+ *~
35
+
36
+ # Type checkers
37
+ .mypy_cache/
38
+ .pyright/
39
+ .ruff_cache/
40
+
41
+ # Testing
42
+ .pytest_cache/
43
+ .coverage
44
+ htmlcov/
45
+ coverage.xml
46
+ *.cover
47
+ .hypothesis/
48
+
49
+ # Build artifacts
50
+ *.manifest
51
+ *.spec
52
+
53
+ # Environments
54
+ .env
55
+ .env.local
56
+ .env.*.local
57
+
58
+ # Misc
59
+ .DS_Store
60
+ *.log
61
+ npm-debug.log*
62
+ yarn-debug.log*
63
+ yarn-error.log*
64
+
65
+ # mise
66
+ .mise.local.toml
67
+
68
+ # Local agent working copies (specs/plans rendered from beads)
69
+ .dev/
70
+
71
+ # Beads runtime lock at repo root (bd creates this; do not commit)
72
+ .beads.gate.lock
73
+
74
+ # behave writes the pretty formatter to this file when its output is piped
75
+ pretty.output
morphir-0.0.1/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
morphir-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,411 @@
1
+ Metadata-Version: 2.5
2
+ Name: morphir
3
+ Version: 0.0.1
4
+ Summary: Python port of Morphir - functional domain modeling for the enterprise
5
+ Project-URL: Homepage, https://github.com/finos/morphir-python
6
+ Project-URL: Documentation, https://morphir.finos.org
7
+ Project-URL: Repository, https://github.com/finos/morphir-python
8
+ Project-URL: Issues, https://github.com/finos/morphir-python/issues
9
+ Author-email: FINOS <morphir@finos.org>
10
+ License-Expression: Apache-2.0
11
+ License-File: LICENSE
12
+ Keywords: domain-modeling,dsl,functional,ir,morphir,type-safe
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.14
18
+ Classifier: Topic :: Software Development :: Libraries
19
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
+ Classifier: Typing :: Typed
21
+ Requires-Python: >=3.14
22
+ Description-Content-Type: text/markdown
23
+
24
+ # Morphir
25
+
26
+ Python port of [Morphir](https://morphir.finos.org) - functional domain modeling for the enterprise.
27
+
28
+ This is the core library package containing:
29
+
30
+ - Morphir IR (Intermediate Representation) models
31
+ - Type definitions and type algebra
32
+ - Pure functional primitives
33
+ - The Morphir SDK runtime (`morphir.sdk`)
34
+
35
+ ## Installation
36
+
37
+ The `morphir` distribution contains the library and SDK with no third-party
38
+ runtime dependencies. Python code uses `import morphir`. Install `morphir-python`
39
+ separately if you need the CLI.
40
+
41
+ ```bash
42
+ pip install morphir
43
+ ```
44
+
45
+ Or with uv:
46
+
47
+ ```bash
48
+ uv add morphir
49
+ ```
50
+
51
+ ## Usage
52
+
53
+ ```python
54
+ from morphir.ir import Type, Value
55
+
56
+ # Example usage will be added as the library develops
57
+ ```
58
+
59
+ ## Morphir IR versions
60
+
61
+ `morphir.ir` holds two first-class IR versions side by side:
62
+
63
+ | Package | IR | Source of truth | JSON codec |
64
+ |---------|----|-----------------|------------|
65
+ | `morphir.ir.v3` | format 3.0.0, what morphir-elm writes today, and its 3.1.0 revision | [morphir-elm](https://github.com/finos/morphir-elm) at `bc99af69` for 3.0.0; [finos/morphir](https://github.com/finos/morphir) at `efa36060` for what 3.1.0 adds | `morphir.ir.v3.codec`, matches morphir-elm byte for byte for 3.0.0 |
66
+ | `morphir.ir.v3` (legacy) | formats 1 and 2, read-only | morphir-elm `*/CodecV1.elm` at `bc99af69`; `Value/CodecV2.elm` at `02aa36af` | `morphir.ir.v3.codec_v1`, `morphir.ir.v3.codec_v2`, decoders only |
67
+ | `morphir.ir.v4` | IR v4, **draft upstream** | [finos/morphir](https://github.com/finos/morphir) at `efa36060` | `morphir.ir.v4.codec`, **draft** |
68
+
69
+ `morphir.ir` and its old submodule paths (`morphir.ir.name`, `morphir.ir.value`,
70
+ and so on) still re-export the v4 classes; `morphir.ir.Name` is
71
+ `morphir.ir.v4.Name`. The v3 classes are distinct, so a type checker reports a v4
72
+ node where a v3 node is expected.
73
+
74
+ ```python
75
+ from pathlib import Path
76
+
77
+ from morphir.ir import v3, v4
78
+ from morphir.ir.format_version import read_format_version
79
+ from morphir.ir.migrate import Ok, v3_to_v4, v4_to_v3
80
+ from morphir.ir.v3 import codec as v3_codec
81
+ from morphir.ir.v4 import codec as v4_codec
82
+
83
+ text = Path("tests/fixtures/ir/v3/elm-simpleValueTree.json").read_text()
84
+ library = v3_codec.decode_versioned_distribution(text)
85
+ assert isinstance(library, v3.Library)
86
+
87
+ upgraded = v3_to_v4(library, package_version="1.2.0")
88
+ assert isinstance(upgraded.value, v4.LibraryDistribution)
89
+ document = v4_codec.encode_versioned_distribution(upgraded.value)
90
+ assert document["formatVersion"] == 4
91
+
92
+ downgraded = v4_to_v3(upgraded.value)
93
+ assert isinstance(downgraded, Ok) and downgraded.value == library
94
+ assert read_format_version("3.0.0") == read_format_version(3)
95
+
96
+ specs = v3_codec.decode_versioned_distribution(
97
+ Path("tests/fixtures/ir/v3/mck-distributions-0011-specs.json").read_text()
98
+ )
99
+ assert isinstance(specs, v3.Specs)
100
+ assert v3_codec.encode_versioned_distribution(specs)["formatVersion"] == "3.1.0"
101
+ assert isinstance(v3_to_v4(specs).value, v4.SpecsDistribution)
102
+ ```
103
+
104
+ ### IR 3.1.0 and the v3 `Specs` distribution
105
+
106
+ IR `3.1.0` is a minor revision of format 3
107
+ ([finos/morphir decision record 0018](https://github.com/finos/morphir/blob/main/kb/bundles/morphir/morphir-ir/decisions/0018-ir-3-1-adds-specs-and-v3-document-trees.md)).
108
+ It adds `v3.Specs`, a distribution that publishes a package's specification
109
+ without its definitions:
110
+
111
+ ```json
112
+ {"formatVersion": "3.1.0", "distribution": ["Specs", [["my"], ["pkg"]], [], {"modules": []}]}
113
+ ```
114
+
115
+ A v3 distribution is `v3.Library | v3.Specs`, and
116
+ `v3_codec.decode_versioned_distribution` returns either. The rules are:
117
+
118
+ - A `Specs` needs `3.1.0` or later. A `Specs` that declares `3`, `"3"` or any
119
+ `"3.0.x"` fails with `specs_before_3_1`. A `Library` reads at every `3.0.x` and
120
+ `3.1.x` release.
121
+ - The modules of a `Specs` distribution's own package are module
122
+ specifications. A member other than `types`, `values` and `doc`, such as the
123
+ `access` of a module definition, fails with `unknown_member`. `types` and
124
+ `values` are required, as morphir-elm requires them in every module
125
+ specification; morphir-rust lets them default to empty.
126
+ - Writers emit the lowest version that fits: `3` for a `Library`, even one read
127
+ as `"3.1.0"`, and `"3.1.0"` for a `Specs`. Readers that stay on 3.0.0, such as
128
+ morphir-elm, still read every `Library` this package writes.
129
+ - `v3_to_v4` turns a v3 `Specs` into a v4 `SpecsDistribution`, and `v4_to_v3`
130
+ turns a v4 `SpecsDistribution` into a v3 `Specs`, with the diagnostics of a
131
+ `Library`'s dependencies.
132
+
133
+ Decision 0018 also adds v3 JSON and YAML document trees. This package has no
134
+ document tree reader or writer, for v3 or v4; bead `morphir-ukvn.8.17` tracks
135
+ that work.
136
+
137
+ ### Reading `formatVersion`
138
+
139
+ `morphir.ir.format_version` reads the version of every IR document before any
140
+ decoder runs. It accepts the integer `3`, the string `"3"`, and the release
141
+ string `"3.0.0"`, and normalizes all three to release 3.0.0. Writers are
142
+ canonical: integer `N` for `N.0.0`, otherwise the exact release string.
143
+
144
+ Accepting strings is a deliberate divergence, so that migration tools can read
145
+ files written by other tools:
146
+
147
+ - morphir-elm decodes `formatVersion` as an integer only, so it rejects
148
+ `"3"` and `"3.0.0"`;
149
+ - the [finos/morphir format-version contract](https://github.com/finos/morphir/blob/main/docs/spec/ir/format-version.md)
150
+ accepts `3` and `"3.0.0"` but rejects `"3"`.
151
+
152
+ The support table is `[3.0.0,3.2.0),[4.0.0,4.1.0)`, the finos reference table.
153
+ Formats 1 and 2 are not in it (see below) and fail there with
154
+ `unsupported_format_version_major`; a later 3.x minor such as `"3.2.0"` fails
155
+ with `unsupported_format_version_minor`.
156
+ Failures raise `morphir.ir.diagnostics.DiagnosticError`, whose diagnostics carry
157
+ the stable codes of the finos contract.
158
+
159
+ ### Legacy formats 1 and 2
160
+
161
+ morphir-elm wrote `"formatVersion": 1` until October 2021 and `2` until May 2023.
162
+ The finos release grammar starts at major 3, so these legacy formats are outside
163
+ every support table. `read_document_with_legacy` and `read_legacy_document`
164
+ recognize them, only as the integers `1` and `2`, and `morphir.ir.v3.codec_v1` and
165
+ `morphir.ir.v3.codec_v2` decode them into the format 3.0.0 model, as morphir-elm
166
+ does. Encoding the result with `morphir.ir.v3.codec` writes format 3, so decoding
167
+ is the upgrade. No writer emits format 1 or 2.
168
+
169
+ Format 2 is format 3.0.0 with snake_case value and pattern tags
170
+ (`"let_definition"`, `"wildcard_pattern"`). Format 1 is snake_case throughout and
171
+ also differs in access control, documentation, record fields, and package modules;
172
+ the `codec_v1` docstring lists the differences. morphir-elm reads format 1 but no
173
+ longer reads format 2; reading format 2 here is a deliberate divergence.
174
+
175
+ ```python
176
+ from pathlib import Path
177
+
178
+ from morphir.ir.format_version import LegacyFormat, read_document_with_legacy
179
+ from morphir.ir.v3 import codec, codec_v1, codec_v2
180
+
181
+ legacy = Path("tests/fixtures/ir/legacy")
182
+ text = (legacy / "elm-rentals-format-1.json").read_text()
183
+ version, _ = read_document_with_legacy(text)
184
+ assert version is LegacyFormat.V1
185
+
186
+ library = codec_v1.decode_versioned_distribution(text)
187
+ same = codec_v2.decode_versioned_distribution(
188
+ (legacy / "elm-rentals-format-2.json").read_text()
189
+ )
190
+ assert library == same
191
+ assert codec.encode_versioned_distribution(library)["formatVersion"] == 3
192
+ ```
193
+
194
+ ### Draft IR v4
195
+
196
+ IR v4 is not frozen upstream. `morphir.ir.v4.codec` follows the finos/morphir v4
197
+ JSON profile at commit `efa36060` (`FINOS_MORPHIR_PIN`), and its spellings may
198
+ change in a later minor release of this package. It writes `formatVersion` 4 and
199
+ the distribution wrapped by its kind, never a format-3 shape. The module
200
+ docstring lists the gaps between the Python v4 model and upstream v4, each of
201
+ which fails with a diagnostic instead of guessing or dropping data.
202
+
203
+ The v4 model follows the upstream reference model at that commit:
204
+
205
+ - A value definition is one of four bodies (`ValueDefinitionBody`): the
206
+ expression body `ValueDefinition`, `NativeBody`, `ExternalBody` and
207
+ `IncompleteBody`. Module values and let bindings hold any of them.
208
+ - `IncompleteTypeDefinition` and `IncompleteBody` carry an `Incompleteness`: a
209
+ hole with a typed `HoleReason` (`UnresolvedReference`, `DeletedDuringRefactor`,
210
+ `TypeMismatch`) or a draft. A `Hole` value's `reason` is a `HoleReason` too.
211
+ - Module, type and value specifications carry `annotations`, compact
212
+ (`CompactAnnotation`) or structured (`StructuredAnnotation`).
213
+ - `morphir.ir.v4.type.Union` is the draft untagged union, with two or more
214
+ alternatives.
215
+ - The `Native` and `External` value nodes and `TypeConstraints` are deprecated:
216
+ upstream v4 cannot spell them.
217
+
218
+ ```python
219
+ from morphir.ir.v4 import Name, codec
220
+ from morphir.ir.v4 import type as t
221
+ from morphir.ir.v4 import value as v
222
+
223
+ int_type = t.Reference(
224
+ t.TypeAttributes(), codec.decode_fqname("morphir/SDK:basics#int")
225
+ )
226
+ add = v.NativeBody(
227
+ [(Name(("a",)), int_type), (Name(("b",)), int_type)],
228
+ int_type,
229
+ v.NativeInfo(v.Arithmetic()),
230
+ )
231
+ assert codec.decode_value_definition(codec.encode_value_definition(add)) == add
232
+ ```
233
+
234
+ `PackageInfo.version` is optional (`str | None`). Neither format 3 nor v4 JSON has
235
+ a package release version, so the draft codec does not write it and decodes it as
236
+ absent. `v3_to_v4` sets it only from `package_version`, and `v4_to_v3` drops it
237
+ with the warning `package_version_dropped`.
238
+
239
+ ### Migrations
240
+
241
+ `morphir.ir.migrate.v3_to_v4` always succeeds for a v3 Library or Specs.
242
+ `v4_to_v3` returns `Ok(value, warnings)` or `Err(diagnostics)` and reports every
243
+ construct format 3 cannot hold in one pass: Application distributions, `Hole`,
244
+ `Native`, `External`, `DocumentLiteral`, native, external and incomplete
245
+ bodies, incomplete type definitions, `Union`, annotations, a missing
246
+ `inferred_type`, semantic attributes, and missing member documentation. Value
247
+ source locations fail unless you pass `drop_source_locations=True`.
248
+
249
+ v4 JSON spells a value-definition input as a bare type, so it has no place for
250
+ the per-input attributes of a typed v3 model. morphir-elm writes the resolved
251
+ type there, which can differ from the declared type (an alias expanded, a type
252
+ variable instantiated), and the draft codec then fails with `no_v4_spelling`.
253
+ Opt in to dropping them with `v3_to_v4(..., drop_input_attributes=True)`,
254
+ `drop_input_attributes(dist)` for any v4 distribution, or
255
+ `morphir-python ir convert --to v4 --drop-input-attributes`. Each input that loses
256
+ information reports `input_attributes_dropped`; without the opt-in the in-memory
257
+ migration stays lossless.
258
+
259
+ ```python
260
+ from pathlib import Path
261
+
262
+ from morphir.ir.migrate import v3_to_v4
263
+ from morphir.ir.v3 import codec as v3_codec
264
+ from morphir.ir.v4 import codec as v4_codec
265
+
266
+ text = Path("tests/fixtures/ir/v3/elm-reference-model.json").read_text()
267
+ upgraded = v3_to_v4(
268
+ v3_codec.decode_versioned_distribution(text), drop_input_attributes=True
269
+ )
270
+ assert {w.code for w in upgraded.warnings} == {"input_attributes_dropped"}
271
+ assert v4_codec.encode_versioned_distribution(upgraded.value)["formatVersion"] == 4
272
+ ```
273
+
274
+ ## Morphir SDK
275
+
276
+ `morphir.sdk` is the standard library that Morphir models compile against. The
277
+ set of functions is the `Morphir.IR.SDK.*` package specification of
278
+ [morphir-elm](https://github.com/finos/morphir-elm). The reference behaviour is
279
+ elm/core 1.0.5 plus the `Morphir.SDK.*` Elm runtime. The package has no
280
+ dependencies and does no IO.
281
+
282
+ | Module | Elm module | Python data |
283
+ |--------|------------|-------------|
284
+ | `morphir.sdk.basics` | `Morphir.SDK.Basics` | `int`, `float`, `bool`, `Order` (`LT`, `EQ`, `GT`) |
285
+ | `morphir.sdk.char` | `Morphir.SDK.Char` | `str` with one code point |
286
+ | `morphir.sdk.string` | `Morphir.SDK.String` | `str` |
287
+ | `morphir.sdk.list` | `Morphir.SDK.List` | `tuple[A, ...]` |
288
+ | `morphir.sdk.dict` | `Morphir.SDK.Dict` | `Dict[K, V]`, entries sorted by key |
289
+ | `morphir.sdk.set` | `Morphir.SDK.Set` | `Set[A]`, members sorted |
290
+ | `morphir.sdk.maybe` | `Morphir.SDK.Maybe` | `Just[A] \| Nothing` |
291
+ | `morphir.sdk.result` | `Morphir.SDK.Result` | `Ok[A] \| Err[E]`, as `Result[E, A]` |
292
+ | `morphir.sdk.tuple` | `Morphir.SDK.Tuple` | `tuple[A, B]` |
293
+ | `morphir.sdk.decimal` | `Morphir.SDK.Decimal` | `decimal.Decimal` |
294
+ | `morphir.sdk.int` | `Morphir.SDK.Int` | `Int8`, `Int16`, `Int32`, `Int64` (`NewType` over `int`) |
295
+ | `morphir.sdk.number` | `Morphir.SDK.Number` | `Number`, an exact rational |
296
+ | `morphir.sdk.local_date` | `Morphir.SDK.LocalDate` | `datetime.date`, no time zone |
297
+ | `morphir.sdk.local_time` | `Morphir.SDK.LocalTime` | `LocalTime`, milliseconds from the epoch |
298
+ | `morphir.sdk.instant` | `Morphir.SDK.Instant` | `Instant`, milliseconds from the epoch |
299
+ | `morphir.sdk.uuid` | `Morphir.SDK.UUID` | `uuid.UUID` |
300
+ | `morphir.sdk.regex` | `Morphir.SDK.Regex` | `Regex` over the `re` module |
301
+ | `morphir.sdk.aggregate` | `Morphir.SDK.Aggregate` | `Aggregation`, `Operator` |
302
+ | `morphir.sdk.rule` | `Morphir.SDK.Rule` | `Callable[[A], Maybe[B]]` |
303
+ | `morphir.sdk.key` | `Morphir.SDK.Key` | `int` and flat tuples |
304
+ | `morphir.sdk.stateful_app` | `Morphir.SDK.StatefulApp` | `StatefulApp` |
305
+ | `morphir.sdk.result_list` | `Morphir.SDK.ResultList` | `tuple[Result[E, A], ...]` |
306
+
307
+ This is every module of the `Morphir.SDK` package specification. The `Json`
308
+ modules of morphir-elm are not part of that specification and are not included.
309
+
310
+ Notes on the modules beyond elm/core:
311
+
312
+ - `instant.Instant` is its own class over a number of milliseconds, not an alias
313
+ of `datetime.datetime`, so a naive `datetime` cannot pass as an instant. Use
314
+ `instant.from_datetime` and `instant.to_datetime` to convert.
315
+ - `local_time.LocalTime` is a point in time, not a time of day. In Elm it is an
316
+ alias of `Time.Posix`, so it does not go around midnight.
317
+ - `regex` patterns use Python `re` syntax, which differs from JavaScript in
318
+ places (named groups are `(?P<name>...)`, `\d` and `\w` follow Unicode). The
319
+ module docstring lists the differences.
320
+ - `aggregate` leaves out the part of the Elm module that inspects Morphir IR
321
+ values (`constructAggregationCall`); the specification does not list it.
322
+ `aggregate.aggregate` yields its rows in ascending key order.
323
+ - `key.Key4` and larger are flat tuples. Elm nests them only because an Elm tuple
324
+ holds three elements at most; the order of two keys is the same.
325
+
326
+ ### Conventions
327
+
328
+ - **Names** are the Elm names in snake_case: `withDefault` is `with_default`,
329
+ `integerDivide` is `integer_divide`, `toInt8` is `to_int8`. A name that is a
330
+ Python keyword gets a trailing underscore: `not_`, `and_`, `or_`. The one
331
+ exception is `isNaN`, which is `is_nan`. The test
332
+ `tests/unit/sdk/test_spec_coverage.py` lists every Elm name with its Python name.
333
+ - **Arguments** are in Elm order, with the data last, and are not curried:
334
+ `list.map(f, xs)`, `dict.get(key, d)`, `maybe.with_default(0, m)`.
335
+ - **Data is immutable.** An Elm list is a tuple; functions accept any `Sequence`
336
+ and yield a tuple. `Maybe`, `Result`, `Dict`, `Set` and `Number` are frozen
337
+ dataclasses, so they compare by value and work with `match`.
338
+ - **Module names** are the Elm names in lower case, so `list`, `dict`, `set`,
339
+ `int`, `tuple`, `string` and `decimal` have the name of a built-in or of a
340
+ standard library module. This is safe because Python 3 imports are absolute:
341
+ `import decimal` always gives the standard library. (Do not run a file of the
342
+ `sdk` directory as a script, or put that directory on `sys.path`; that would hide
343
+ the standard library modules.) Many functions also have the
344
+ name of a built-in (`map`, `filter`, `round`, ...). Import the modules with an
345
+ alias and call the functions through the module:
346
+
347
+ ```python
348
+ from morphir.sdk import dict as Dict
349
+ from morphir.sdk import list as List
350
+ ```
351
+
352
+ - **Comparable keys.** `Dict` keys, `Set` members and the arguments of
353
+ `basics.compare`, `list.sort` and so on must be numbers, strings, or tuples or
354
+ lists of these. Other values (also `bool`, and a mix of `int` and `str`) raise
355
+ `TypeError`, because Python has no compile-time check for Elm's `comparable`.
356
+ A list inside a key or member is stored as a tuple, so a later change to the
357
+ list cannot break the sorted order, and the value stays hashable. Build a
358
+ `Dict` or `Set` with `from_list`, `singleton` or `insert`; the class constructor
359
+ accepts only entries that are already sorted, and raises `ValueError` otherwise.
360
+ - **Failures.** Where Elm's runtime fails, the Python function raises:
361
+ `basics.mod_by(0, x)` raises `ZeroDivisionError`, and `basics.equal` raises
362
+ `TypeError` on a function, also one held inside a tuple, a list, a `Maybe`, a
363
+ `Result`, a `Dict` or a `Set`.
364
+ - **Powers stay real.** `basics.power` follows JavaScript `Math.pow`, as the Elm
365
+ runtime does: `power(-1.0, 0.5)` is `NaN` and `power(0.0, -1.0)` is infinity,
366
+ where Python `**` gives a complex number and raises. Two integers with an
367
+ exponent of zero or more give an exact integer.
368
+
369
+ ### Departures from Elm
370
+
371
+ - Python integers have no size limit. `Int` arithmetic never overflows, `Int64`
372
+ covers the full 64-bit range, and `Number` is exact at any size.
373
+ - A `str` is a sequence of code points, not of UTF-16 code units. `string.length`,
374
+ `string.slice` and the other index functions count a character above U+FFFF as
375
+ 1, where Elm counts 2. Strings compare by code point.
376
+ - `basics.remainder_by(0, x)` raises `ZeroDivisionError`; Elm yields `NaN`.
377
+ `basics.round`, `floor`, `ceiling` and `truncate` raise on `NaN` and the
378
+ infinities, because the result is an `int`.
379
+ - `char.to_locale_upper` and `char.to_locale_lower` do the same as `to_upper` and
380
+ `to_lower`; Python has no locale rules for case.
381
+ - `decimal.from_float` raises `ValueError` on `NaN` and the infinities.
382
+ `decimal.shift_decimal_left` moves the exponent; the Elm runtime multiplies by a
383
+ float. Arithmetic results keep 50 significant digits with half-up rounding, from
384
+ a context that is local to the module. The global `decimal` context is never
385
+ read or changed.
386
+ - `number` comparisons give the correct answer for a negative denominator (the
387
+ Elm runtime compares `3/-4` and `1/2` wrongly), and `number.simplify` yields a
388
+ positive denominator (`3/-6` becomes `-1/2`). As in Elm, arithmetic results are
389
+ not reduced, so `==` on two `Number` values is structural; use `number.equal`
390
+ to compare by value.
391
+
392
+ ## Requirements
393
+
394
+ - Python 3.14+
395
+
396
+ ## Design Principles
397
+
398
+ This library follows functional programming principles:
399
+
400
+ - **Immutability**: All data structures are immutable (frozen dataclasses)
401
+ - **Type Safety**: Complete type annotations with strict mypy/pyright checking
402
+ - **Algebraic Data Types**: Using unions and frozen dataclasses
403
+ - **Making Illegal States Unrepresentable**: Domain modeling prevents invalid states
404
+
405
+ ## License
406
+
407
+ Apache-2.0 - see the [LICENSE](../../LICENSE) file for details.
408
+
409
+ ## Related Packages
410
+
411
+ - [morphir-python](https://pypi.org/project/morphir-python/) - CLI tools and extensions