nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: cogent3/cogent3
Pull request #1292 (2023-02-09 04:56:04)

ENH: added app for converting to / from json


Patch:
diff --git a/src/cogent3/app/io_new.py b/src/cogent3/app/io_new.py
index 763602572..8f520c7c6 100644
--- a/src/cogent3/app/io_new.py
+++ b/src/cogent3/app/io_new.py
@@ -247,6 +247,18 @@ class from_primitive:
         return self.deserialiser(data)
 
 
+@define_app
+def to_json(data: SerialisableType) -> str:
+    """Convert primitive python types to json string."""
+    return json.dumps(data)
+
+
+@define_app
+def from_json(data: str) -> SerialisableType:
+    """Convert json string to primitive python types."""
+    return json.loads(data)
+
+
 class tabular(Enum):
     table = "table"
     distances = "distances"


Test patch:
diff --git a/tests/test_app/test_io_new.py b/tests/test_app/test_io_new.py
index 6d5bf0036..65f06b876 100644
--- a/tests/test_app/test_io_new.py
+++ b/tests/test_app/test_io_new.py
@@ -588,3 +588,28 @@ def test_default_serialiser_deserialiser(data):
     s = DEFAULT_SERIALISER(data)
     ds = DEFAULT_DESERIALISER(s)
     assert ds == data
+
+
+def test_to_json():
+    to_j = get_app("to_json")
+    data = {"a": [0, 1]}
+    assert to_j(data) == json.dumps(data)
+
+
+def test_from_json():
+    from_j = get_app("from_json")
+    assert from_j('{"a": [0, 1]}') == {"a": [0, 1]}
+
+
+def test_to_from_json():
+    to_j = get_app("to_json")
+    from_j = get_app("from_json")
+    app = to_j + from_j
+    data = {"a": [0, 1]}
+    assert app(data) == data
+    assert app(data) is not data
+
+
+def test_to_json_combines():
+    app = get_app("to_primitive") + get_app("to_json")
+    assert app(DNA) == DNA.to_json()
Repository: cogent3/cogent3
Pull request #1358 (2023-04-14 00:58:15)

DEV: more fixes to deprecation decorator


Patch:
diff --git a/src/cogent3/util/warning.py b/src/cogent3/util/warning.py
index 73b06c960..cb3a1460e 100644
--- a/src/cogent3/util/warning.py
+++ b/src/cogent3/util/warning.py
@@ -36,7 +36,7 @@ def deprecated(_type, old, new, version, reason=None, stack_level=3):
     """
     msg = f"{_type} {old} which will be removed in version {version}, use {new} instead"
     if reason is not None:
-        msg = f"{msg}\n{reason}"
+        msg = f"{msg}\nreason={reason!r}"
 
     with catch_warnings():
         simplefilter("always")
@@ -62,7 +62,7 @@ def discontinued(_type, old, version, reason=None, stack_level=3):
     """
     msg = f"{_type} {old} is discontinued and will be removed in version {version}"
     if reason is not None:
-        msg = f"{msg}\n{reason}"
+        msg = f"{msg}\nreason={reason!r}"
 
     with catch_warnings():
         simplefilter("always")
@@ -136,6 +136,7 @@ def deprecated_callable(
     reason: str,
     new: Optional[str] = None,
     is_discontinued: bool = False,
+    stack_level=2,
 ) -> Callable:
     """
     A decorator that marks callables (function or method) as deprecated or discontinued..
@@ -149,6 +150,8 @@ def deprecated_callable(
         If the callable is being replaced, this is the replacement, e.g. 'ClassName.new_method()'
     is_discontinued : bool
         If True the callable is being discontinued.
+    stack_level
+        as per warnings.warn
 
     Returns
     -------
@@ -171,10 +174,15 @@ def deprecated_callable(
     """
 
     def decorator(func: Callable) -> Callable:
-        _type = "method" if inspect.ismethod(func) else "function"
+        sig = set(inspect.signature(func).parameters)
+        _type = "method" if sig & {"self", "cls", "klass"} else "function"
         old = func.__name__
         params = dict(
-            _type=_type, old=old, version=version, reason=reason, stack_level=2
+            _type=_type,
+            old=old,
+            version=version,
+            reason=reason,
+            stack_level=stack_level,
         )
         if is_discontinued:
             depr_func = discontinued


Test patch:
diff --git a/tests/test_util/test_warning.py b/tests/test_util/test_warning.py
index 72b013041..bf193fb0c 100644
--- a/tests/test_util/test_warning.py
+++ b/tests/test_util/test_warning.py
@@ -216,3 +216,11 @@ def test_method_deprecated_method_pickling(recwarn):
 
     # the method still works after pickling
     assert unpickled.squared(20) == instance.squared(20)
+
+
+@pytest.mark.parametrize(
+    "func,_type", ((foo2().old_meth, "method"), (cubed, "function"))
+)
+def test_deprecated_callable_resolves_type(recwarn, func, _type):
+    func(2)
+    assert _type in recwarn.list[0].message.args[0]