Skip to content

Commit a8e9f31

Browse files
authored
[SYNPY-1426] Deprecate annotations (#1221)
* Deprecate annotation methods in Synapse class with migration guidance to new dataclass models
1 parent e7b5b02 commit a8e9f31

File tree

2 files changed

+373
-9
lines changed

2 files changed

+373
-9
lines changed

synapseclient/annotations.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
import datetime
7575
import typing
7676

77+
from deprecated import deprecated
78+
7779
from synapseclient.core.utils import (
7880
from_unix_epoch_time,
7981
id_of,
@@ -284,6 +286,13 @@ def set_privacy(
284286
raise KeyError('The key "%s" couldn\'t be found in the annotations.' % key)
285287

286288

289+
@deprecated(
290+
version="4.9.0",
291+
reason="Use the dataclass model attributes instead. "
292+
"All dataclass models support annotations: File, Folder, Project, Table, EntityView, Dataset, "
293+
"DatasetCollection, MaterializedView, SubmissionView, VirtualTable. "
294+
"Access annotations directly via `instance.annotations` attribute.",
295+
)
287296
class Annotations(dict):
288297
"""
289298
Represent Synapse Entity annotations as a flat dictionary with the system assigned properties id, etag
@@ -295,15 +304,42 @@ class Annotations(dict):
295304
values: (Optional) dictionary of values to be copied into annotations
296305
**kwargs: additional key-value pairs to be added as annotations
297306
298-
Example: Creating a few instances
299-
Creating and setting annotations
300-
301-
from synapseclient import Annotations
307+
Example: Migrating from this class to dataclass models
308+
**Legacy approach (deprecated):**
309+
```python
310+
from synapseclient import Annotations
311+
312+
example1 = Annotations('syn123','40256475-6fb3-11ea-bb0a-9cb6d0d8d984', {'foo':'bar'})
313+
example2 = Annotations('syn123','40256475-6fb3-11ea-bb0a-9cb6d0d8d984', foo='bar')
314+
example3 = Annotations('syn123','40256475-6fb3-11ea-bb0a-9cb6d0d8d984')
315+
example3['foo'] = 'bar'
316+
```
317+
318+
**New approach using dataclass models:**
319+
```python
320+
import synapseclient
321+
from synapseclient.models import (
322+
File, Folder, Project, Table, EntityView, Dataset,
323+
DatasetCollection, MaterializedView, SubmissionView, VirtualTable
324+
)
302325
303-
example1 = Annotations('syn123','40256475-6fb3-11ea-bb0a-9cb6d0d8d984', {'foo':'bar'})
304-
example2 = Annotations('syn123','40256475-6fb3-11ea-bb0a-9cb6d0d8d984', foo='bar')
305-
example3 = Annotations('syn123','40256475-6fb3-11ea-bb0a-9cb6d0d8d984')
306-
example3['foo'] = 'bar'
326+
# Create client and login
327+
syn = synapseclient.Synapse()
328+
syn.login()
329+
330+
# File - don't download the file content, just get metadata
331+
file_instance = File(id="syn12345", download_file=False)
332+
file_instance = file_instance.get()
333+
file_instance.annotations = {
334+
"foo": ["bar"],
335+
"species": ["Homo sapiens"]
336+
}
337+
file_instance = file_instance.store()
338+
print(f"File annotations: {file_instance.annotations}")
339+
340+
# All other dataclass models work the same way
341+
# annotations is always a dict by default (empty {} if no annotations exist)
342+
```
307343
"""
308344

309345
id: str

0 commit comments

Comments
 (0)