Skip to content

Commit ae4790f

Browse files
authored
Merge pull request #808 from rhayes777/feature/collection_type_annotations
feature/collection type annotations
2 parents 8ddcb5f + 3e1a1f5 commit ae4790f

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

autofit/mapper/prior_model/prior_model.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import builtins
2+
import collections.abc
23
import copy
34
import inspect
45
import logging
6+
import typing
57

68
from autoconf.class_path import get_class_path
79
from autoconf.exc import ConfigException
@@ -164,7 +166,20 @@ def __init__(
164166
elif hasattr(spec, "__args__") and type(None) in spec.__args__:
165167
setattr(self, arg, None)
166168
else:
167-
setattr(self, arg, Model(annotations[arg]))
169+
annotation = annotations[arg]
170+
171+
if (
172+
hasattr(annotation, "__origin__")
173+
and issubclass(
174+
annotation.__origin__, collections.abc.Collection
175+
)
176+
) or isinstance(annotation, collections.abc.Collection):
177+
from autofit.mapper.prior_model.collection import Collection
178+
179+
value = Collection()
180+
else:
181+
value = Model(annotation)
182+
setattr(self, arg, value)
168183
else:
169184
prior = self.make_prior(arg)
170185
if (

test_autofit/mapper/model/test_regression.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import List
2+
13
import pytest
24

35
import autofit as af
@@ -157,3 +159,13 @@ def test_instance_from_vector(model_with_assertion):
157159

158160
def test_random_instance(model_with_assertion):
159161
model_with_assertion.random_instance(ignore_prior_limits=True)
162+
163+
164+
class TestModel:
165+
def __init__(self, items: List[float]):
166+
self.items = items
167+
168+
169+
def test_typing_annotations():
170+
model = af.Model(TestModel)
171+
assert model.items == af.Collection()

0 commit comments

Comments
 (0)