Skip to content

Commit

Permalink
Merge pull request #92 from aglavic/fix_empty_dict
Browse files Browse the repository at this point in the history
Fix bug in Sample
  • Loading branch information
aglavic authored Jun 28, 2022
2 parents 5e99ee4 + 5c36156 commit c1b8ed3
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 67 deletions.
9 changes: 8 additions & 1 deletion orsopy/fileio/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,14 @@ def _resolve_type(hint: type, item: Any) -> Any:
else:
return hbase([Header._resolve_type(t0, item)])
elif hbase is dict:
value_type = get_args(hint)[1]
try:
value_type = get_args(hint)[1]
except IndexError:
warnings.warn(
"The evaluation of type hints requires key/value definition for Dict, "
"if you want to use unspecified dictionaries use dict instead of Dict."
)
raise
try:
for key, value in item.items():
# resolve the type of any value in the dictionary
Expand Down
4 changes: 2 additions & 2 deletions orsopy/fileio/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import yaml

from .base import File, Header, Person, Value, ValueRange, ValueVector, orsodataclass
from .base import ComplexValue, File, Header, Person, Value, ValueRange, ValueVector, orsodataclass

# typing stuff introduced in python 3.8
try:
Expand Down Expand Up @@ -66,7 +66,7 @@ class Sample(Header):
composition: Optional[str] = None
description: Optional[str] = None
environment: Optional[List[str]] = None
sample_parameters: Optional[Dict] = field(
sample_parameters: Optional[Dict[str, Union[Value, ValueRange, ValueVector, ComplexValue]]] = field(
default=None, metadata={"description": "Using keys for parameters and Value* objects for values."}
)

Expand Down
200 changes: 137 additions & 63 deletions orsopy/fileio/schema/refl_header.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,47 +168,54 @@
"probe"
]
},
"Sample": {
"title": "Sample",
"Value": {
"title": "Value",
"type": "object",
"properties": {
"name": {
"magnitude": {
"type": [
"string",
"number",
"null"
]
},
"category": {
"unit": {
"description": "SI unit string",
"type": [
"string",
"null"
]
},
"composition": {
"comment": {
"type": [
"string",
"null"
]
},
"description": {
}
},
"required": [
"magnitude"
]
},
"ValueRange": {
"title": "ValueRange",
"type": "object",
"properties": {
"min": {
"type": [
"string",
"number",
"null"
]
},
"environment": {
"items": {
"type": "string"
},
"max": {
"type": [
"array",
"number",
"null"
]
},
"sample_parameters": {
"description": "Using keys for parameters and Value* objects for values.",
"unit": {
"description": "SI unit string",
"type": [
"object",
"string",
"null"
]
},
Expand All @@ -220,14 +227,27 @@
}
},
"required": [
"name"
"min",
"max"
]
},
"Value": {
"title": "Value",
"ValueVector": {
"title": "ValueVector",
"type": "object",
"properties": {
"magnitude": {
"x": {
"type": [
"number",
"null"
]
},
"y": {
"type": [
"number",
"null"
]
},
"z": {
"type": [
"number",
"null"
Expand All @@ -248,23 +268,45 @@
}
},
"required": [
"magnitude"
"x",
"y",
"z"
]
},
"ValueRange": {
"title": "ValueRange",
"ComplexValue": {
"title": "ComplexValue",
"type": "object",
"properties": {
"min": {
"type": [
"number",
"null"
"real": {
"anyOf": [
{
"type": "number"
},
{
"type": "array",
"items": {
"type": "number"
}
},
{
"type": "null"
}
]
},
"max": {
"type": [
"number",
"null"
"imag": {
"anyOf": [
{
"type": "number"
},
{
"type": "array",
"items": {
"type": "number"
}
},
{
"type": "null"
}
]
},
"unit": {
Expand All @@ -282,55 +324,69 @@
}
},
"required": [
"min",
"max"
"real"
]
},
"Polarization": {
"title": "Polarization",
"description": "The first symbol indicates the magnetisation direction of the incident\nbeam. An optional second symbol indicates the direction of the scattered\nbeam, if a spin analyser is present.",
"enum": [
"unpolarized",
"po",
"mo",
"op",
"om",
"mm",
"mp",
"pm",
"pp"
],
"type": "string"
},
"ValueVector": {
"title": "ValueVector",
"Sample": {
"title": "Sample",
"type": "object",
"properties": {
"x": {
"name": {
"type": [
"number",
"string",
"null"
]
},
"y": {
"category": {
"type": [
"number",
"string",
"null"
]
},
"z": {
"composition": {
"type": [
"number",
"string",
"null"
]
},
"unit": {
"description": "SI unit string",
"description": {
"type": [
"string",
"null"
]
},
"environment": {
"items": {
"type": "string"
},
"type": [
"array",
"null"
]
},
"sample_parameters": {
"description": "Using keys for parameters and Value* objects for values.",
"additionalProperties": {
"anyOf": [
{
"$ref": "#/definitions/Value"
},
{
"$ref": "#/definitions/ValueRange"
},
{
"$ref": "#/definitions/ValueVector"
},
{
"$ref": "#/definitions/ComplexValue"
}
]
},
"type": [
"object",
"null"
]
},
"comment": {
"type": [
"string",
Expand All @@ -339,11 +395,25 @@
}
},
"required": [
"x",
"y",
"z"
"name"
]
},
"Polarization": {
"title": "Polarization",
"description": "The first symbol indicates the magnetisation direction of the incident\nbeam. An optional second symbol indicates the direction of the scattered\nbeam, if a spin analyser is present.",
"enum": [
"unpolarized",
"po",
"mo",
"op",
"om",
"mm",
"mp",
"pm",
"pp"
],
"type": "string"
},
"InstrumentSettings": {
"title": "InstrumentSettings",
"type": "object",
Expand Down Expand Up @@ -760,6 +830,8 @@
"enum": [
"1/angstrom",
"1/nm",
"1",
"1/s",
null
]
},
Expand Down Expand Up @@ -802,6 +874,8 @@
"enum": [
"1/angstrom",
"1/nm",
"1",
"1/s",
null
]
},
Expand Down
Loading

0 comments on commit c1b8ed3

Please sign in to comment.