Skip to content

Commit 8422877

Browse files
committed
push_attr fixes
- don't raise exception if mods is used together with common or prefixed there is nothing in the logic preventing it - don't raise if columns is used together with common or prefixed, warn instead - minor code cleanup
1 parent ddbcfa4 commit 8422877

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

src/mudata/_core/mudata.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,8 +2242,6 @@ def _push_attr(
22422242
raise ValueError("All mods should be present in mdata.mod")
22432243
elif len(mods) == self.n_mod:
22442244
mods = None
2245-
for k, v in {"common": common, "prefixed": prefixed}.items():
2246-
assert v is None, f"Cannot use mods with {k}."
22472245

22482246
if only_drop:
22492247
drop = True
@@ -2252,15 +2250,22 @@ def _push_attr(
22522250

22532251
if columns is not None:
22542252
for k, v in {"common": common, "prefixed": prefixed}.items():
2255-
assert v is None, f"Cannot use columns with {k}."
2253+
if v:
2254+
warnings.warn(
2255+
f"Both columns and {k} given. Columns take precedence, {k} will be ignored",
2256+
RuntimeWarning,
2257+
stacklevel=2,
2258+
)
22562259

22572260
# - modname1:column -> [modname1:column]
22582261
# - column -> [modname1:column, modname2:column, ...]
2259-
cols = [col for col in cols if col["name"] in columns or col["derived_name"] in columns]
2260-
22612262
# preemptively drop columns from other modalities
2262-
if mods is not None:
2263-
cols = [col for col in cols if col["prefix"] in mods or col["prefix"] == ""]
2263+
cols = [
2264+
col
2265+
for col in cols
2266+
if (col["name"] in columns or col["derived_name"] in columns)
2267+
and (col["prefix"] == "" or mods is not None and col["prefix"] in mods)
2268+
]
22642269
else:
22652270
if common is None:
22662271
common = True

src/mudata/_core/utils.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,30 +122,17 @@ def _classify_prefixed_columns(
122122
res: list[dict[str, str]] = []
123123

124124
for name in names:
125-
name_common = {
126-
"name": name,
127-
"prefix": "",
128-
"derived_name": name,
129-
}
130-
name_split = name.split(":", 1)
131-
132-
if len(name_split) < 2:
133-
res.append(name_common)
125+
if len(name_split := name.split(":", 1)) < 2 or name_split[0] not in prefixes:
126+
res.append({"name": name, "prefix": "", "derived_name": name, "class": "common"})
134127
else:
135-
maybe_modname, derived_name = name_split
136-
137-
if maybe_modname in prefixes:
138-
name_prefixed = {
128+
res.append(
129+
{
139130
"name": name,
140-
"prefix": maybe_modname,
141-
"derived_name": derived_name,
131+
"prefix": name_split[0],
132+
"derived_name": name_split[1],
133+
"class": "prefixed",
142134
}
143-
res.append(name_prefixed)
144-
else:
145-
res.append(name_common)
146-
147-
for name_res in res:
148-
name_res["class"] = "common" if name_res["prefix"] == "" else "prefixed"
135+
)
149136

150137
return res
151138

0 commit comments

Comments
 (0)