Skip to content

Commit

Permalink
expose values with getattr (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekorman committed May 13, 2024
1 parent 1bc8965 commit 1c10888
Show file tree
Hide file tree
Showing 19 changed files with 154 additions and 138 deletions.
8 changes: 3 additions & 5 deletions client/unit-tests/coretypes/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,11 @@ def test_prediction():
),
]

# valid
Prediction(datum=datum, annotations=pds)

string = str(Prediction(datum=datum, annotations=pds))
pred = Prediction(datum=datum, annotations=pds)
string = str(pred)
assert (
string
== "{'datum': {'uid': 'somefile', 'metadata': {}}, 'annotations': [{'task_type': 'classification', 'metadata': {}, 'labels': [{'key': 'test', 'value': 'value', 'score': 1.0}], 'bounding_box': None, 'polygon': None, 'raster': None, 'embedding': None}, {'task_type': 'classification', 'metadata': {}, 'labels': [{'key': 'test', 'value': 'value', 'score': 1.0}], 'bounding_box': None, 'polygon': None, 'raster': None, 'embedding': None}]}"
== "{'datum': {'uid': 'somefile', 'metadata': {}}, 'annotations': [{'task_type': <TaskType.CLASSIFICATION: 'classification'>, 'metadata': {}, 'labels': [{'key': 'test', 'value': 'value', 'score': 1.0}], 'bounding_box': None, 'polygon': None, 'raster': None, 'embedding': None}, {'task_type': <TaskType.CLASSIFICATION: 'classification'>, 'metadata': {}, 'labels': [{'key': 'test', 'value': 'value', 'score': 1.0}], 'bounding_box': None, 'polygon': None, 'raster': None, 'embedding': None}]}"
)
assert "dataset_name" not in string

Expand Down
2 changes: 1 addition & 1 deletion client/unit-tests/coretypes/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test__format_filter(geojson, polygon):
Dataset.metadata["some_str"] == "foobar",
Dataset.metadata["some_float"] >= 0.123,
Dataset.metadata["some_datetime"] > datetime.timedelta(days=1),
Dataset.metadata["some_geospatial"].intersects(polygon),
Dataset.metadata["some_geospatial"].intersects(polygon), # type: ignore
]
)

Expand Down
6 changes: 3 additions & 3 deletions client/unit-tests/symbolic/collections/test_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from valor.schemas import Dictionary, Float, Integer
from valor.schemas import Dictionary


def test_validate_metadata():
Expand All @@ -14,8 +14,8 @@ def test_validate_metadata():
Dictionary({123: 123}) # type: ignore

# Check int to float conversion
assert type(Dictionary({"test": 1})["test"]) is Integer
assert type(Dictionary({"test": 1.0})["test"]) is Float
assert type(Dictionary({"test": 1})["test"]) is int
assert type(Dictionary({"test": 1.0})["test"]) is float


def test_init_dictionary_from_builtin_dict():
Expand Down
15 changes: 11 additions & 4 deletions client/unit-tests/symbolic/collections/test_static_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,17 @@ class A(StaticCollection):
}

# test value members
assert v1.w.to_dict() == {"type": "integer", "value": 101}
assert v1.x.to_dict() == {"type": "float", "value": 0.123}
assert v1.y.to_dict() == {"type": "string", "value": "foobar"}
assert v1.z.to_dict() == {"type": "bool", "value": True}
assert isinstance(v1.w, int)
assert v1.w == 101

assert isinstance(v1.x, float)
assert v1.x == 0.123

assert isinstance(v1.y, str)
assert v1.y == "foobar"

assert isinstance(v1.z, bool)
assert v1.z is True


def test__get_static_types():
Expand Down
2 changes: 1 addition & 1 deletion client/unit-tests/symbolic/collections/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def test_list():

# test creating valued lists
variable = List[Float]([0.1, 0.2, 0.3])
assert variable.__str__() == "[0.1, 0.2, 0.3]"
assert variable.__str__() == "[Float(0.1), Float(0.2), Float(0.3)]"
assert variable.to_dict() == {
"type": "list[float]",
"value": [0.1, 0.2, 0.3],
Expand Down
16 changes: 12 additions & 4 deletions client/unit-tests/symbolic/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ def test_function(variables):
x, y, z = variables

# test stringify
assert Function(x, y, z).__repr__() == "Function(1, '2', 0.3)"
assert Function(x, y, z).__str__() == "Function(1, '2', 0.3)"
assert (
Function(x, y, z).__repr__()
== "Function(Integer(1), String('2'), Float(0.3))"
)
assert (
Function(x, y, z).__str__()
== "Function(Integer(1), String('2'), Float(0.3))"
)

# test dictionary generation
assert Function(x, y, z).to_dict() == {
Expand All @@ -43,8 +49,10 @@ def test_function(variables):
# test stringify w/ operator
assert issubclass(And, Function)
assert And._operator is not None
assert And(x, y, z).__repr__() == "And(1, '2', 0.3)"
assert And(x, y, z).__str__() == "(1 & '2' & 0.3)"
assert (
And(x, y, z).__repr__() == "And(Integer(1), String('2'), Float(0.3))"
)
assert And(x, y, z).__str__() == "(Integer(1) & String('2') & Float(0.3))"

# test logical operators
assert type(Function(x) & Function(y)) is And
Expand Down
2 changes: 1 addition & 1 deletion client/unit-tests/symbolic/types/test_symbolic_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_symbol():
def _test_symbolic_outputs(v, s=Symbol(name="test")):
assert s.to_dict() == v.to_dict()
assert s.to_dict() == v.get_symbol().to_dict()
assert s.__repr__() == v.__repr__()
assert f"Variable({s.__repr__()})" == v.__repr__()
assert s.__str__() == v.__str__()
assert v.is_symbolic and not v.is_value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ def test_label_typing():
assert type(Label.score) is Float

label = Label(key="k1", value="v1")
assert type(label.key) is String
assert type(label.value) is String
assert type(label.score) is Float
assert type(label.key) is str
assert type(label.value) is str
assert label.score is None

label = Label(key="k1", value="v1", score=None)
assert type(label.key) is String
assert type(label.value) is String
assert type(label.score) is Float
assert type(label.key) is str
assert type(label.value) is str
assert label.score is None

label = Label(key="k1", value="v1", score=1.0)
assert type(label.key) is String
assert type(label.value) is String
assert type(label.score) is Float
assert type(label.key) is str
assert type(label.value) is str
assert type(label.score) is float


def test_annotation_typing():
Expand All @@ -55,12 +55,12 @@ def test_annotation_typing():
task_type=enums.TaskType.CLASSIFICATION,
labels=[],
)
assert type(annotation.task_type) is TaskTypeEnum
assert type(annotation.task_type) is enums.TaskType
assert type(annotation.labels) is List[Label]
assert type(annotation.metadata) is Dictionary
assert type(annotation.bounding_box) is Box
assert type(annotation.polygon) is Polygon
assert type(annotation.raster) is Raster
assert annotation.bounding_box is None
assert annotation.polygon is None
assert annotation.raster is None

bbox = Box.from_extrema(0, 1, 0, 1)
polygon = Polygon([bbox.boundary])
Expand All @@ -73,7 +73,7 @@ def test_annotation_typing():
polygon=polygon,
raster=raster,
)
assert type(annotation.task_type) is TaskTypeEnum
assert type(annotation.task_type) is enums.TaskType
assert type(annotation.labels) is List[Label]
assert type(annotation.metadata) is Dictionary
assert type(annotation.bounding_box) is Box
Expand All @@ -86,11 +86,11 @@ def test_datum_typing():
assert type(Datum.metadata) is Dictionary

datum = Datum(uid="test")
assert type(datum.uid) is String
assert type(datum.uid) is str
assert type(datum.metadata) is Dictionary

datum = Datum(uid="test", metadata={})
assert type(datum.uid) is String
assert type(datum.uid) is str
assert type(datum.metadata) is Dictionary


Expand All @@ -113,11 +113,11 @@ def test_dataset_typing():
assert type(Dataset.metadata) is Dictionary

dataset = Dataset(name="test")
assert type(dataset.name) is String
assert type(dataset.name) is str
assert type(dataset.metadata) is Dictionary

dataset = Dataset(name="test", metadata={})
assert type(dataset.name) is String
assert type(dataset.name) is str
assert type(dataset.metadata) is Dictionary


Expand All @@ -126,9 +126,9 @@ def test_model_typing():
assert type(Model.metadata) is Dictionary

model = Model(name="test")
assert type(model.name) is String
assert type(model.name) is str
assert type(model.metadata) is Dictionary

model = Model(name="test", metadata={})
assert type(model.name) is String
assert type(model.name) is str
assert type(model.metadata) is Dictionary
2 changes: 1 addition & 1 deletion client/unit-tests/test_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def test_draw_detections_on_image(bounding_poly: Polygon):
Annotation(
task_type=TaskType.OBJECT_DETECTION,
labels=[Label(key="k", value="v")],
polygon=Polygon(bounding_poly.get_value()),
polygon=bounding_poly,
)
],
),
Expand Down

0 comments on commit 1c10888

Please sign in to comment.