Skip to content

Commit bb50c27

Browse files
committed
suppress deprecation warnings within migration, expand to TableItem, add tests
Signed-off-by: Panos Vagenas <[email protected]>
1 parent c1b58a1 commit bb50c27

File tree

2 files changed

+103
-83
lines changed

2 files changed

+103
-83
lines changed

docling_core/types/doc/document.py

Lines changed: 89 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,65 +1883,68 @@ class PictureItem(FloatingItem):
18831883
@model_validator(mode="after")
18841884
def _migrate_annotations_to_meta(self) -> Self:
18851885
"""Migrate the `annotations` field to `meta`."""
1886-
if self.annotations:
1887-
_logger.info(
1888-
"Migrating deprecated `annotations` to `meta`; this will be removed in the future. "
1889-
"Note that only the first available instance of each annotation type will be migrated."
1890-
)
1891-
for ann in self.annotations:
1892-
# migrate annotations to meta
1893-
1894-
# ensure meta field is present
1895-
if self.meta is None:
1896-
self.meta = PictureMeta()
1897-
1898-
if isinstance(ann, PictureClassificationData):
1899-
self.meta.classification = PictureClassificationMetaField(
1900-
predictions=[
1901-
PictureClassificationPrediction(
1902-
class_name=pred.class_name,
1903-
confidence=pred.confidence,
1904-
created_by=ann.provenance,
1905-
)
1906-
for pred in ann.predicted_classes
1907-
],
1908-
)
1909-
elif isinstance(ann, DescriptionAnnotation):
1910-
self.meta.description = DescriptionMetaField(
1911-
text=ann.text,
1912-
created_by=ann.provenance,
1913-
)
1914-
elif isinstance(ann, PictureMoleculeData):
1915-
self.meta.molecule = MoleculeMetaField(
1916-
smi=ann.smi,
1917-
confidence=ann.confidence,
1918-
created_by=ann.provenance,
1919-
**{
1920-
MetaUtils._create_migrated_meta_field_name(
1921-
name="segmentation"
1922-
): ann.segmentation,
1923-
MetaUtils._create_migrated_meta_field_name(
1924-
name="class_name"
1925-
): ann.class_name,
1926-
},
1927-
)
1928-
elif isinstance(ann, PictureTabularChartData):
1929-
self.meta.tabular_chart = TabularChartMetaField(
1930-
title=ann.title,
1931-
chart_data=ann.chart_data,
1932-
)
1933-
else:
1934-
self.meta.set_custom_field(
1935-
namespace=MetaUtils._META_FIELD_LEGACY_NAMESPACE,
1936-
name=ann.kind,
1937-
value=(
1938-
ann.content
1939-
if isinstance(ann, MiscAnnotation)
1940-
else ann.model_dump(mode="json")
1941-
),
1942-
)
1886+
with warnings.catch_warnings():
1887+
warnings.simplefilter("ignore", category=DeprecationWarning)
19431888

1944-
return self
1889+
if self.annotations:
1890+
_logger.info(
1891+
"Migrating deprecated `annotations` to `meta`; this will be removed in the future. "
1892+
"Note that only the first available instance of each annotation type will be migrated."
1893+
)
1894+
for ann in self.annotations:
1895+
# migrate annotations to meta
1896+
1897+
# ensure meta field is present
1898+
if self.meta is None:
1899+
self.meta = PictureMeta()
1900+
1901+
if isinstance(ann, PictureClassificationData):
1902+
self.meta.classification = PictureClassificationMetaField(
1903+
predictions=[
1904+
PictureClassificationPrediction(
1905+
class_name=pred.class_name,
1906+
confidence=pred.confidence,
1907+
created_by=ann.provenance,
1908+
)
1909+
for pred in ann.predicted_classes
1910+
],
1911+
)
1912+
elif isinstance(ann, DescriptionAnnotation):
1913+
self.meta.description = DescriptionMetaField(
1914+
text=ann.text,
1915+
created_by=ann.provenance,
1916+
)
1917+
elif isinstance(ann, PictureMoleculeData):
1918+
self.meta.molecule = MoleculeMetaField(
1919+
smi=ann.smi,
1920+
confidence=ann.confidence,
1921+
created_by=ann.provenance,
1922+
**{
1923+
MetaUtils._create_migrated_meta_field_name(
1924+
name="segmentation"
1925+
): ann.segmentation,
1926+
MetaUtils._create_migrated_meta_field_name(
1927+
name="class_name"
1928+
): ann.class_name,
1929+
},
1930+
)
1931+
elif isinstance(ann, PictureTabularChartData):
1932+
self.meta.tabular_chart = TabularChartMetaField(
1933+
title=ann.title,
1934+
chart_data=ann.chart_data,
1935+
)
1936+
else:
1937+
self.meta.set_custom_field(
1938+
namespace=MetaUtils._META_FIELD_LEGACY_NAMESPACE,
1939+
name=ann.kind,
1940+
value=(
1941+
ann.content
1942+
if isinstance(ann, MiscAnnotation)
1943+
else ann.model_dump(mode="json")
1944+
),
1945+
)
1946+
1947+
return self
19451948

19461949
# Convert the image to Base64
19471950
def _image_to_base64(self, pil_image, format="PNG"):
@@ -2097,34 +2100,37 @@ class TableItem(FloatingItem):
20972100
@model_validator(mode="after")
20982101
def _migrate_annotations_to_meta(self) -> Self:
20992102
"""Migrate the `annotations` field to `meta`."""
2100-
if self.annotations:
2101-
_logger.warning(
2102-
"Migrating deprecated `annotations` to `meta`; this will be removed in the future. "
2103-
"Note that only the first available instance of each annotation type will be migrated."
2104-
)
2105-
for ann in self.annotations:
2103+
with warnings.catch_warnings():
2104+
warnings.simplefilter("ignore", category=DeprecationWarning)
21062105

2107-
# ensure meta field is present
2108-
if self.meta is None:
2109-
self.meta = FloatingMeta()
2106+
if self.annotations:
2107+
_logger.info(
2108+
"Migrating deprecated `annotations` to `meta`; this will be removed in the future. "
2109+
"Note that only the first available instance of each annotation type will be migrated."
2110+
)
2111+
for ann in self.annotations:
21102112

2111-
if isinstance(ann, DescriptionAnnotation):
2112-
self.meta.description = DescriptionMetaField(
2113-
text=ann.text,
2114-
created_by=ann.provenance,
2115-
)
2116-
else:
2117-
self.meta.set_custom_field(
2118-
namespace=MetaUtils._META_FIELD_LEGACY_NAMESPACE,
2119-
name=ann.kind,
2120-
value=(
2121-
ann.content
2122-
if isinstance(ann, MiscAnnotation)
2123-
else ann.model_dump(mode="json")
2124-
),
2125-
)
2113+
# ensure meta field is present
2114+
if self.meta is None:
2115+
self.meta = FloatingMeta()
21262116

2127-
return self
2117+
if isinstance(ann, DescriptionAnnotation):
2118+
self.meta.description = DescriptionMetaField(
2119+
text=ann.text,
2120+
created_by=ann.provenance,
2121+
)
2122+
else:
2123+
self.meta.set_custom_field(
2124+
namespace=MetaUtils._META_FIELD_LEGACY_NAMESPACE,
2125+
name=ann.kind,
2126+
value=(
2127+
ann.content
2128+
if isinstance(ann, MiscAnnotation)
2129+
else ann.model_dump(mode="json")
2130+
),
2131+
)
2132+
2133+
return self
21282134

21292135
def export_to_dataframe(
21302136
self, doc: Optional["DoclingDocument"] = None

test/test_docling_doc.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import re
3+
import warnings
34
from collections import deque
45
from copy import deepcopy
56
from pathlib import Path
@@ -1966,3 +1967,16 @@ def test_validate_rules():
19661967

19671968
with pytest.warns(UserWarning, match=message):
19681969
doc._validate_rules(raise_on_error=False)
1970+
1971+
1972+
def test_meta_migration_warnings():
1973+
# the following should not raise any warnings
1974+
with warnings.catch_warnings():
1975+
warnings.simplefilter("error")
1976+
doc = DoclingDocument.load_from_yaml("test/data/doc/dummy_doc_2.yaml")
1977+
1978+
# the following should raise a deprecation warning
1979+
with pytest.warns(DeprecationWarning):
1980+
_ = doc.pictures[0].annotations
1981+
with pytest.warns(DeprecationWarning):
1982+
_ = doc.tables[0].annotations

0 commit comments

Comments
 (0)