Skip to content

Commit c9074f7

Browse files
dilpathdweindl
andauthored
Implement petab.* deprecation with __getattr__ (#297)
Co-authored-by: Daniel Weindl <[email protected]> Co-authored-by: Daniel Weindl <[email protected]>
1 parent 96d16fc commit c9074f7

30 files changed

+76
-248
lines changed

petab/C.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

petab/__init__.py

Lines changed: 49 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,66 @@
22
PEtab global
33
============
44
5-
.. warning::
6-
7-
All functions in here are deprecated. Use the respective functions from
8-
:mod:`petab.v1` instead.
9-
105
Attributes:
116
ENV_NUM_THREADS:
127
Name of environment variable to set number of threads or processes
138
PEtab should use for operations that can be performed in parallel.
149
By default, all operations are performed sequentially.
1510
"""
16-
import functools
17-
import inspect
11+
import importlib
1812
import sys
19-
import warnings
13+
from functools import partial
14+
from pathlib import Path
2015
from warnings import warn
2116

22-
# deprecated imports
23-
from petab.v1 import * # noqa: F403, F401, E402
24-
25-
from .v1.format_version import __format_version__ # noqa: F401, E402
26-
27-
# __all__ = [
28-
# 'ENV_NUM_THREADS',
29-
# ]
30-
3117
ENV_NUM_THREADS = "PETAB_NUM_THREADS"
32-
33-
34-
def _deprecated_v1(func):
35-
"""Decorator for deprecation warnings for functions."""
36-
37-
@functools.wraps(func)
38-
def new_func(*args, **kwargs):
39-
warnings.warn(
40-
f"petab.{func.__name__} is deprecated, "
41-
f"please use petab.v1.{func.__name__} instead.",
42-
category=DeprecationWarning,
43-
stacklevel=2,
18+
__all__ = ["ENV_NUM_THREADS"]
19+
20+
21+
def __getattr__(name):
22+
if attr := globals().get(name):
23+
return attr
24+
if name == "v1":
25+
return importlib.import_module("petab.v1")
26+
if name != "__path__":
27+
warn(
28+
f"Accessing `petab.{name}` is deprecated and will be removed in "
29+
f"the next major release. Please use `petab.v1.{name}` instead.",
30+
DeprecationWarning,
31+
stacklevel=3,
4432
)
45-
return func(*args, **kwargs)
46-
47-
return new_func
33+
return getattr(importlib.import_module("petab.v1"), name)
4834

4935

50-
def _deprecated_import_v1(module_name: str):
51-
"""Decorator for deprecation warnings for modules."""
52-
warn(
53-
f"The '{module_name}' module is deprecated and will be removed "
54-
f"in the next major release. Please use "
55-
f"'petab.v1.{module_name.removeprefix('petab.')}' "
56-
"instead.",
57-
DeprecationWarning,
58-
stacklevel=2,
36+
def v1getattr(name, module):
37+
if name != "__path__":
38+
warn(
39+
f"Accessing `petab.{name}` is deprecated and will be removed in "
40+
f"the next major release. Please use `petab.v1.{name}` instead.",
41+
DeprecationWarning,
42+
stacklevel=3,
43+
)
44+
try:
45+
return module.__dict__[name]
46+
except KeyError:
47+
raise AttributeError(name) from None
48+
49+
50+
# Create dummy modules for all old modules
51+
v1_root = Path(__file__).resolve().parent / "v1"
52+
v1_objects = [f.relative_to(v1_root) for f in v1_root.rglob("*")]
53+
for v1_object in v1_objects:
54+
if "__pycache__" in str(v1_object):
55+
continue
56+
if v1_object.suffix not in ["", ".py"]:
57+
continue
58+
if not (v1_root / v1_object).exists():
59+
raise ValueError(v1_root / v1_object)
60+
v1_object_parts = [*v1_object.parts[:-1], v1_object.stem]
61+
module_name = ".".join(["petab", *v1_object_parts])
62+
63+
real_module = importlib.import_module(
64+
f"petab.v1.{'.'.join(v1_object_parts)}"
5965
)
60-
61-
62-
__all__ = [
63-
x
64-
for x in dir(sys.modules[__name__])
65-
if not x.startswith("_")
66-
and x not in {"sys", "warnings", "functools", "warn", "inspect"}
67-
]
68-
69-
70-
# apply decorator to all functions in the module
71-
for name in __all__:
72-
obj = globals().get(name)
73-
if callable(obj) and inspect.isfunction(obj):
74-
globals()[name] = _deprecated_v1(obj)
75-
del name, obj
66+
real_module.__getattr__ = partial(v1getattr, module=real_module)
67+
sys.modules[module_name] = real_module

petab/calculate.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

petab/composite_problem.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

petab/conditions.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

petab/core.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

petab/lint.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

petab/mapping.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

petab/math/__init__.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

petab/math/sympify.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)