Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
nomad-FAIR
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
nomad-lab
nomad-FAIR
Commits
505bb2d7
Commit
505bb2d7
authored
5 months ago
by
Theodore Chang
Browse files
Options
Downloads
Plain Diff
Merge branch 'metainfo-ensure-scalar' into 'develop'
Metainfo ensure scalar input See merge request
!2173
parents
fb66c2a6
2afa240c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!2173
Metainfo ensure scalar input
Pipeline
#224275
passed
5 months ago
Stage: build
Stage: test
Stage: release
Changes
2
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
nomad/metainfo/data_type.py
+10
-4
10 additions, 4 deletions
nomad/metainfo/data_type.py
tests/metainfo/test_metainfo.py
+8
-3
8 additions, 3 deletions
tests/metainfo/test_metainfo.py
with
18 additions
and
7 deletions
nomad/metainfo/data_type.py
+
10
−
4
View file @
505bb2d7
...
...
@@ -290,16 +290,22 @@ class Primitive(Datatype):
if
self
.
is_scalar
:
given_type
=
type
(
value
)
if
given_type
is
np
.
ndarray
:
given_type
=
value
.
dtype
.
type
if
value
.
size
!=
1
:
raise
ValueError
(
f
'
Cannot set
{
value
}
for the scalar quantity
{
self
.
_definition
}
.
'
)
# no conversion and type mismatch
if
self
.
_disable_auto_conversion
and
given_type
!=
self
.
_dtype
:
raise
ValueError
(
f
'
Cannot set
{
value
}
for
{
self
.
_definition
}
.
'
)
value
=
value
[
0
]
given_type
=
value
.
dtype
.
type
# type match, no need to consider conversion
if
given_type
==
self
.
_dtype
:
return
value
# no conversion and type mismatch
if
self
.
_disable_auto_conversion
:
raise
ValueError
(
f
'
Cannot set
{
value
}
for
{
self
.
_definition
}
.
'
)
# conversion is allowed, explicitly convertable
if
self
.
convertible_from
(
given_type
):
return
self
.
_dtype
(
value
)
...
...
This diff is collapsed.
Click to expand it.
tests/metainfo/test_metainfo.py
+
8
−
3
View file @
505bb2d7
...
...
@@ -675,9 +675,14 @@ class TestM1:
test_quantity
=
Quantity
(
type
=
np
.
dtype
(
'
int32
'
))
test_section
=
TestSection
()
test_section
.
test_quantity
=
12
assert
test_section
.
test_quantity
==
12
assert
type
(
test_section
.
test_quantity
)
is
np
.
int32
for
value
in
[
12
,
np
.
array
([
12
],
dtype
=
np
.
int32
)]:
test_section
.
test_quantity
=
value
assert
test_section
.
test_quantity
==
12
assert
type
(
test_section
.
test_quantity
)
is
np
.
int32
with
pytest
.
raises
(
ValueError
):
test_section
.
test_quantity
=
np
.
array
([
12
,
13
],
dtype
=
np
.
int32
)
def
test_scalar_float
(
self
):
class
TestSection
(
MSection
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment