Skip to content

Commit 33ac781

Browse files
committed
Merge branch 'master' into devendorize
# Conflicts: # dandischema/models.py
2 parents 24f800e + 8dff37f commit 33ac781

File tree

10 files changed

+62
-10
lines changed

10 files changed

+62
-10
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010

1111
steps:
1212
- name: Set up environment
13-
uses: actions/checkout@v4
13+
uses: actions/checkout@v5
1414
with: # no need for the history
1515
fetch-depth: 1
1616
- name: Set up Python

.github/workflows/release.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
auto-version: ${{ steps.auto-version.outputs.version }}
1919
steps:
2020
- name: Checkout source
21-
uses: actions/checkout@v4
21+
uses: actions/checkout@v5
2222
with:
2323
# A full checkout is required so that auto will have access to tag
2424
# information.
@@ -44,7 +44,7 @@ jobs:
4444
if: needs.release-check.outputs.auto-version != ''
4545
steps:
4646
- name: Checkout source
47-
uses: actions/checkout@v4
47+
uses: actions/checkout@v5
4848
with:
4949
fetch-depth: 0
5050
path: dandischema
@@ -74,7 +74,7 @@ jobs:
7474
echo "SCHEMA_VERSION=$SCHEMA_VERSION" >> "$GITHUB_ENV"
7575
7676
- name: Checkout dandi/schema
77-
uses: actions/checkout@v4
77+
uses: actions/checkout@v5
7878
with:
7979
repository: dandi/schema
8080
path: schema

.github/workflows/test-dandi-cli.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ jobs:
7979
run: echo DANDI_DEVEL=1 >> "$GITHUB_ENV"
8080

8181
- name: Check out dandischema
82-
uses: actions/checkout@v4
82+
uses: actions/checkout@v5
8383
with:
8484
# Fetch all commits so that versioningit will return something
8585
# compatible with semantic-version

.github/workflows/test-nonetwork.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- '3.12'
2626
steps:
2727
- name: Set up environment
28-
uses: actions/checkout@v4
28+
uses: actions/checkout@v5
2929
with:
3030
# Fetch all commits so that versioningit will return something
3131
# compatible with semantic-version

.github/workflows/test-schema.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
if: contains(github.event.pull_request.labels.*.name, 'release')
1212
steps:
1313
- name: Checkout this repository
14-
uses: actions/checkout@v4
14+
uses: actions/checkout@v5
1515
with:
1616
fetch-depth: 0 # Need history for `git describe`
1717
path: dandischema
@@ -26,7 +26,7 @@ jobs:
2626
working-directory: dandischema
2727

2828
- name: Checkout dandi/schema
29-
uses: actions/checkout@v4
29+
uses: actions/checkout@v5
3030
with:
3131
repository: dandi/schema
3232
path: schema

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
instance_name: EMBER-DANDI
3838
steps:
3939
- name: Set up environment
40-
uses: actions/checkout@v4
40+
uses: actions/checkout@v5
4141
with:
4242
# Fetch all commits so that versioningit will return something
4343
# compatible with semantic-version

.github/workflows/typing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ubuntu-latest
1010
steps:
1111
- name: Check out repository
12-
uses: actions/checkout@v4
12+
uses: actions/checkout@v5
1313
with:
1414
fetch-depth: 0
1515

dandischema/models.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
EmailStr,
2727
Field,
2828
GetJsonSchemaHandler,
29+
SerializerFunctionWrapHandler,
2930
StringConstraints,
3031
ValidationInfo,
32+
field_serializer,
3133
field_validator,
3234
model_validator,
3335
)
@@ -47,6 +49,13 @@
4749
from .types import ByteSizeJsonSchema
4850
from .utils import name2title
4951

52+
try:
53+
from anys import AnyBase
54+
except ImportError:
55+
_has_anys = False
56+
else:
57+
_has_anys = True
58+
5059
# Load needed configurations into constants
5160
_INSTANCE_CONFIG = get_instance_config()
5261

@@ -549,6 +558,14 @@ def json_dict(self) -> dict:
549558
)
550559
return self.model_dump(mode="json", exclude_none=True)
551560

561+
if _has_anys:
562+
563+
@field_serializer("*", mode="wrap")
564+
def preserve_anys_values(
565+
self, value: Any, handler: SerializerFunctionWrapHandler
566+
) -> Any:
567+
return value if isinstance(value, AnyBase) else handler(value)
568+
552569
@field_validator("schemaKey")
553570
@classmethod
554571
def ensure_schemakey(cls, val: str) -> str:

dandischema/tests/test_models.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from inspect import isclass
44
from typing import Any, Dict, List, Literal, Optional, Tuple, Type, Union, cast
55

6+
import anys
67
import pydantic
78
from pydantic import BaseModel, ConfigDict, Field, ValidationError
89
import pytest
@@ -40,6 +41,39 @@
4041
_INSTANCE_CONFIG = get_instance_config()
4142

4243

44+
@pytest.mark.parametrize(
45+
("y_type", "anys_value"),
46+
[
47+
(int, anys.ANY_INT),
48+
(str, anys.ANY_STR),
49+
(list, anys.ANY_LIST),
50+
(dict, anys.ANY_DICT),
51+
],
52+
)
53+
def test_serialize_anys_values(y_type: type, anys_value: anys.AnyBase) -> None:
54+
"""
55+
Test the serialization of invalid model instances constructed with the
56+
`model_construct` method containing `anys` values.
57+
"""
58+
59+
class NewModel(DandiBaseModel):
60+
x: int
61+
y: y_type # type: ignore[valid-type]
62+
63+
m = NewModel.model_construct(x=42, y=anys_value)
64+
65+
m_serialized = m.model_dump()
66+
x = m_serialized["x"]
67+
y = m_serialized["y"]
68+
69+
# Verify that `x` serialized as expected, as an int of value 42
70+
assert type(x) is int
71+
assert x == 42
72+
73+
# Verify that `y` serialized as itself
74+
assert y is anys_value
75+
76+
4377
def test_dandiset() -> None:
4478
assert Dandiset.model_construct() # type: ignore[call-arg]
4579

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ style =
5353
flake8
5454
pre-commit
5555
test =
56+
anys
5657
mypy
5758
pytest
5859
pytest-cov

0 commit comments

Comments
 (0)