Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintain/2 0 remove deprecations #208

Merged
merged 8 commits into from
Feb 5, 2024
2 changes: 1 addition & 1 deletion gemd/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.18.4"
__version__ = "2.0.0"
3 changes: 2 additions & 1 deletion gemd/demo/cake.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Bake a cake."""
from io import BytesIO
import json
import random

from gemd.entity.attribute import Condition, Parameter, Property, PropertyAndConditions
Expand Down Expand Up @@ -1059,6 +1058,8 @@ def _find_name(name, material):


if __name__ == "__main__":
import json

encoder = GEMDJson()
cake = make_cake(seed=42)

Expand Down
8 changes: 4 additions & 4 deletions gemd/demo/strehlow_and_cook.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,8 @@ def make_display_table(structured):
Defaults to DEMO_TEMPLATE_SCOPE if none provided.
"""
import os.path
import json
import json as json_builtin
import gemd.json as gemd_json
import sys

args = sys.argv[1:]
Expand All @@ -486,11 +487,10 @@ def make_display_table(structured):
break

with open(os.path.join(os.path.dirname(__file__), SMALL_TABLE), 'w') as f:
json.dump(reduced_list, f, indent=2)
json_builtin.dump(reduced_list, f, indent=2)

print("\n\nJSON -- Training table")
import gemd.json as je
print(json.dumps(json.loads(je.dumps(full_table))["object"], indent=2))
print(json_builtin.dumps(json_builtin.loads(gemd_json.dumps(full_table))["object"], indent=2))

print("\n\nCSV -- Display table")
display = make_display_table(full_table)
Expand Down
3 changes: 2 additions & 1 deletion gemd/entity/dict_serializable.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from abc import ABC, ABCMeta
from logging import getLogger

import json
import inspect
import functools
from typing import TypeVar, Union, Iterable, List, Mapping, Dict, Set, Any
Expand Down Expand Up @@ -121,6 +120,8 @@ def dump(self) -> Dict[str, Any]:

"""
from gemd.json import GEMDJson
import json

encoder = GEMDJson()
return json.loads(encoder.raw_dumps(self))

Expand Down
15 changes: 2 additions & 13 deletions gemd/entity/link_by_uid.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""A unique id that stands in for a data object."""
from typing import TypeVar
import uuid
from warnings import warn

from gemd.entity.dict_serializable import DictSerializable

Expand Down Expand Up @@ -31,7 +30,7 @@ def __repr__(self):
return str({"scope": self.scope, "id": self.id})

@classmethod
def from_entity(cls, entity: BaseEntityType, name=None, *, scope=None):
def from_entity(cls, entity: BaseEntityType, *, scope=None):
"""
Create LinkByUID from in-memory object.

Expand All @@ -45,8 +44,6 @@ def from_entity(cls, entity: BaseEntityType, name=None, *, scope=None):
----------
entity: BaseEntity
The entity to substitute with a LinkByUID
name: str, optional (Deprecated)
The desired scope of the id.
scope: str, optional
The desired scope of the id.

Expand All @@ -56,16 +53,8 @@ def from_entity(cls, entity: BaseEntityType, name=None, *, scope=None):
A link object that references `entity` through its scope and id.

"""
if name is None and scope is None:
if scope is None:
scope = "auto" # set default
elif name is None and scope is not None: # The rest of these conditions to be deleted
pass # Normal workflow
elif name is not None and scope is None:
warn("The positional argument 'name' is deprecated. When selecting a default scope, "
"use the 'scope' keyword argument.", DeprecationWarning)
scope = name
elif name is not None and scope is not None:
raise ValueError("Specify the 'name' parameter or 'scope' parameter, not both.")

if scope in entity.uids:
uid = entity.uids[scope]
Expand Down
8 changes: 4 additions & 4 deletions gemd/entity/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ def complete_material_history(mat) -> List[Dict[str, Any]]:

"""
from gemd.entity.base_entity import BaseEntity
import json
from gemd.json import dumps, loads
import json as json_builtin
import gemd.json as gemd_json
from gemd.util.impl import substitute_links

result = []

def body(obj: BaseEntity):
copy = substitute_links(loads(dumps(obj)))
result.append(json.loads(dumps(copy))["context"][0])
copy = substitute_links(gemd_json.loads(gemd_json.dumps(obj)))
result.append(json_builtin.loads(gemd_json.dumps(copy))["context"][0])

recursive_foreach(mat, body, apply_first=False)

Expand Down
30 changes: 0 additions & 30 deletions gemd/enumeration/base_enumeration.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,36 +65,6 @@ def from_str(cls, val: str, *, exception: bool = False) -> Optional["BaseEnumera
raise ValueError(f"{val} is not a valid {cls}; valid choices are {[x for x in cls]}")
return result

@classmethod
@deprecated(deprecated_in="1.15.0",
removed_in="2.0.0",
details="Enumerations autocast to values now.")
def get_value(cls, name: str) -> str:
"""
Return a valid value associated with name.

If name is equal to one of the enum members, or to the value
associated with an enum member, then return the relevant value.
"""
if name is None:
return None
return cls.from_str(name, exception=True).value

@classmethod
@deprecated(deprecated_in="1.15.0",
removed_in="2.0.0",
details="Use from_str for retrieving the correct Enum object.")
def get_enum(cls, name: str) -> "BaseEnumeration":
"""
Return the enumeration associated with name.

If name is equal to one of the enum members, or to the value
associated with an enum member, then return the relevant enumeration.
"""
if name is None:
return None
return cls.from_str(name, exception=True)

def __str__(self):
"""Return the value of the enumeration object."""
return self.value
Expand Down
44 changes: 6 additions & 38 deletions gemd/json/gemd_json.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import inspect
from deprecation import deprecated
import json as json_builtin
import json
from typing import Dict, Any, Type

from gemd.entity.dict_serializable import DictSerializable
Expand Down Expand Up @@ -54,7 +52,7 @@ def dumps(self, obj, **kwargs) -> str:
additional = flatten(res, self.scope)
res = substitute_links(res)
res["context"] = additional
return json_builtin.dumps(res, cls=GEMDEncoder, sort_keys=True, **kwargs)
return json.dumps(res, cls=GEMDEncoder, sort_keys=True, **kwargs)

def loads(self, json_str: str, **kwargs):
"""
Expand All @@ -79,7 +77,7 @@ def loads(self, json_str: str, **kwargs):
index = {}
clazz_index = DictSerializable.class_mapping
clazz_index.update(self._clazz_index)
raw = json_builtin.loads(
raw = json.loads(
json_str,
object_hook=lambda x: self._load_and_index(x,
index,
Expand Down Expand Up @@ -163,7 +161,7 @@ def raw_dumps(self, obj, **kwargs):
A serialized string of `obj`, which could be nested

"""
return json_builtin.dumps(obj, cls=GEMDEncoder, sort_keys=True, **kwargs)
return json.dumps(obj, cls=GEMDEncoder, sort_keys=True, **kwargs)

def thin_dumps(self, obj, **kwargs):
"""
Expand All @@ -184,7 +182,7 @@ def thin_dumps(self, obj, **kwargs):
"""
set_uuids(obj, self.scope)
res = substitute_links(obj)
return json_builtin.dumps(res, cls=GEMDEncoder, sort_keys=True, **kwargs)
return json.dumps(res, cls=GEMDEncoder, sort_keys=True, **kwargs)

def raw_loads(self, json_str, **kwargs):
"""
Expand All @@ -208,41 +206,11 @@ def raw_loads(self, json_str, **kwargs):
index = {}
clazz_index = DictSerializable.class_mapping
clazz_index.update(self._clazz_index)
return json_builtin.loads(
return json.loads(
json_str,
object_hook=lambda x: self._load_and_index(x, index, clazz_index=clazz_index),
**kwargs)

@deprecated(deprecated_in="1.13.0", removed_in="2.0.0",
details="Classes are now automatically registered when extending BaseEntity")
def register_classes(self, classes):
"""
Register additional classes to the custom deserialization object hook.

This allows for additional DictSerializable subclasses to be registered to the class index
that is used to decode the type strings. Existing keys are overwritten, allowing classes
in the gemd package to be subclassed and overridden in the class index by their
subclass.

Parameters
----------
classes: Dict[str, type]
a dict mapping the type string to the class

"""
if not isinstance(classes, dict):
raise ValueError("Must be given a dict from str -> class")
non_string_keys = [x for x in classes.keys() if not isinstance(x, str)]
if len(non_string_keys) > 0:
raise ValueError(
"The keys must be strings, but got {} as keys".format(non_string_keys))
non_class_values = [x for x in classes.values() if not inspect.isclass(x)]
if len(non_class_values) > 0:
raise ValueError(
"The values must be classes, but got {} as values".format(non_class_values))

self._clazz_index.update(classes)

@staticmethod
def _load_and_index(
d: Dict[str, Any],
Expand Down
11 changes: 0 additions & 11 deletions gemd/util/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import functools
from typing import Optional, Union, Type, Iterable, MutableSequence, List, Tuple, Mapping, \
Callable, Any, Reversible, ByteString
from warnings import warn

from gemd.entity.base_entity import BaseEntity
from gemd.entity.dict_serializable import DictSerializable
Expand Down Expand Up @@ -271,7 +270,6 @@ def _make_index(_obj: BaseEntity):
def substitute_links(obj: Any,
scope: Optional[str] = None,
*,
native_uid: str = None,
allow_fallback: bool = True,
inplace: bool = False
):
Expand All @@ -287,21 +285,12 @@ def substitute_links(obj: Any,
target of the operation
scope: Optional[str], optional
preferred scope to use for creating LinkByUID objects (Default: None)
native_uid: str, optional
DEPRECATED; former name for scope argument
allow_fallback: bool, optional
whether to grab another scope/id if chosen scope is missing (Default: True).
inplace: bool, optional
whether to replace objects in place, as opposed to returning a copy (Default: False).

"""
if native_uid is not None:
warn("The keyword argument 'native_uid' is deprecated. When selecting a default scope, "
"use the 'scope' keyword argument.", DeprecationWarning)
if scope is not None:
raise ValueError("Both 'scope' and 'native_uid' keywords passed.")
scope = native_uid

if inplace:
method = _substitute_inplace
else:
Expand Down
10 changes: 5 additions & 5 deletions tests/demo/test_sac.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Test Strehlow & Cook demo."""
from gemd.demo.strehlow_and_cook import make_strehlow_table, make_strehlow_objects, \
minimal_subset, import_table
import gemd.json as je
import json
import gemd.json as gemd_json
import json as json_builtin


def test_sac():
Expand All @@ -12,7 +12,7 @@ def test_sac():

# Check that all shapes of records serialize and deserialize
for comp in sac:
assert je.loads(je.dumps(comp)) == comp
assert gemd_json.loads(gemd_json.dumps(comp)) == comp

# Verify that specs are shared when compounds match
for comp1 in sac:
Expand All @@ -27,7 +27,7 @@ def test_sac():

# Make sure there's no migration with repeated serialization
for row in sac_tbl:
assert je.dumps(je.loads(je.dumps(row))) == je.dumps(row)
assert gemd_json.dumps(gemd_json.loads(gemd_json.dumps(row))) == gemd_json.dumps(row)

# Verify that the serialization trick for mocking a structured table works
json.dumps(json.loads(je.dumps(sac_tbl))["object"], indent=2)
json_builtin.dumps(json_builtin.loads(gemd_json.dumps(sac_tbl))["object"], indent=2)
18 changes: 9 additions & 9 deletions tests/entity/object/test_material_run.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Test of the material run object."""
import pytest
import json
import json as json_builtin
from uuid import uuid4
from copy import deepcopy

from gemd.json import loads, dumps
import gemd.json as gemd_json
from gemd.entity.attribute import PropertyAndConditions, Property
from gemd.entity.object import MaterialRun, ProcessSpec, ProcessRun, MaterialSpec, MeasurementRun
from gemd.entity.template import MaterialTemplate
Expand Down Expand Up @@ -33,7 +33,7 @@ def test_material_run():
)

# Make sure that when property is serialized, origin (an enumeration) is serialized as a string
copy_prop = json.loads(dumps(mat_spec))
copy_prop = json_builtin.loads(gemd_json.dumps(mat_spec))
copy_origin = copy_prop["context"][0]["properties"][0]['property']['origin']
assert isinstance(copy_origin, str)

Expand All @@ -43,8 +43,8 @@ def test_material_run():
mat = MaterialRun("name", spec=mat_spec, sample_type="virtual")

# ensure that serialization does not change the MaterialRun
copy = loads(dumps(mat))
assert dumps(copy) == dumps(mat), \
copy = gemd_json.loads(gemd_json.dumps(mat))
assert gemd_json.dumps(copy) == gemd_json.dumps(mat), \
"Material run is modified by serialization or deserialization"


Expand All @@ -57,8 +57,8 @@ def test_process_run():
assert material_run.process == process_run
assert process_run.output_material == material_run

copy_material = loads(dumps(material_run))
assert dumps(copy_material) == dumps(material_run)
copy_material = gemd_json.loads(gemd_json.dumps(material_run))
assert gemd_json.dumps(copy_material) == gemd_json.dumps(material_run)

assert 'output_material' in repr(process_run)
assert 'process' in repr(material_run)
Expand All @@ -69,8 +69,8 @@ def test_process_id_link():
uid = str(uuid4())
proc_link = LinkByUID(scope='id', id=uid)
mat_run = MaterialRun("Another cake", process=proc_link)
copy_material = loads(dumps(mat_run))
assert dumps(copy_material) == dumps(mat_run)
copy_material = gemd_json.loads(gemd_json.dumps(mat_run))
assert gemd_json.dumps(copy_material) == gemd_json.dumps(mat_run)


def test_process_reassignment():
Expand Down
9 changes: 0 additions & 9 deletions tests/entity/test_link_by_uid.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ def test_from_entity():
link1 = LinkByUID.from_entity(run, scope='foo')
assert (link1.scope, link1.id) == ('foo', 'bar')

with pytest.deprecated_call():
assert LinkByUID.from_entity(run, 'foo').scope == 'foo'

with pytest.deprecated_call():
assert LinkByUID.from_entity(run, name='foo').scope == 'foo'

with pytest.raises(ValueError):
LinkByUID.from_entity(run, name='scope1', scope='scope2')


def test_equality():
"""Test that the __eq__ method performs as expected."""
Expand Down
Loading