Skip to content

Commit f9ca783

Browse files
authored
Merge pull request #386 from linkml/no_merge_create_fn
emit warnings for python 3.13
2 parents 638d873 + 193cdb5 commit f9ca783

File tree

3 files changed

+53
-41
lines changed

3 files changed

+53
-41
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,15 @@
1+
import sys
2+
import warnings
13
import dataclasses
2-
#
3-
# The dataclass library builds a rigid `__init__` function that doesn't allow any unrecognized named parameters
4-
#
5-
# The purpose of this extension is to enhance the library to allow additional keyword arguments to passed in
6-
# and then on to the __post_init__ function that can deal with them accordingly
74

8-
# Beware that there is no promise that signature of the create function will remain consistent
9-
loc_fn = dataclasses._create_fn
5+
warnings.warn(
6+
"The LinkML dataclass extension patch is deprecated and will be removed in a future release.\n"
7+
"If you're currently using Python < 3.13, where this patch still applies, you should:\n"
8+
" • Upgrade your LinkML tooling to version >= 1.9.0 (which no longer needs this patch), OR\n"
9+
" • Migrate to Pydantic models if you're using LinkML's generated classes at runtime.\n",
10+
DeprecationWarning,
11+
stacklevel=2
12+
)
1013

11-
12-
def dc_create_fn(name, args, body, *_posargs, **_kwargs):
13-
# If overriding the initializer and using a post init
14-
if name == '__init__' and dataclasses._POST_INIT_NAME in body[-1]:
15-
# Then insert the kwargs into the both the call and the post init
16-
pi_parms = body[-1].rsplit(')', 1)[0]
17-
body[-1] = pi_parms + ('' if pi_parms[-1] == '(' else ',') + ' **_kwargs)'
18-
return loc_fn(name, list(args) + ["**_kwargs"], body, *_posargs, **_kwargs)
19-
else:
20-
return loc_fn(name, args, body, *_posargs, **_kwargs)
21-
22-
23-
dataclasses._create_fn = dc_create_fn
24-
25-
# The following line is here solely to be backwards compatible.
2614
dataclasses_init_fn_with_kwargs = dataclasses._init_fn
27-
28-
# The following line can be used to make certain that the import of the new create function doesn't get
29-
# discarded as being potentially unused
30-
DC_CREATE_FN = True
15+
DC_CREATE_FN = False

poetry.lock

+15-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import sys
2+
import pytest
3+
import importlib.util
4+
import dataclasses
5+
6+
7+
def import_patch_module():
8+
"""Fresh import to ensure warning is triggered"""
9+
spec = importlib.util.find_spec("linkml_runtime.utils.dataclass_extensions_376")
10+
mod = importlib.util.module_from_spec(spec)
11+
spec.loader.exec_module(mod)
12+
return mod
13+
14+
def test_patch_module_emits_deprecation_warning():
15+
"""All Python versions: emits DeprecationWarning and defines compatibility symbols"""
16+
with pytest.warns(DeprecationWarning):
17+
mod = import_patch_module()
18+
19+
assert hasattr(mod, "DC_CREATE_FN")
20+
assert hasattr(mod, "dataclasses_init_fn_with_kwargs")
21+
assert mod.DC_CREATE_FN is False
22+
23+
# Check consistency with actual dataclasses module
24+
init_fn = getattr(dataclasses, "_init_fn", None)
25+
assert mod.dataclasses_init_fn_with_kwargs == init_fn
26+
27+

0 commit comments

Comments
 (0)