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

Signals not always emitted on model changes #223

Open
mrvisscher opened this issue Dec 20, 2024 · 3 comments
Open

Signals not always emitted on model changes #223

mrvisscher opened this issue Dec 20, 2024 · 3 comments

Comments

@mrvisscher
Copy link

For some reason the on_database_metadata_change is not sent when you delete a database from the databases metadata. signal=False is hardcode instead of it using the signal parameter that defaults to True. I think the expected behavior would be for the signal to fire on database deletion as well.

def __delitem__(self, name: str, signal: bool = True):
from bw2data import Database
if name not in self:
raise KeyError
try:
Database(name).delete(warn=False, signal=False)
except:
warnings.warn(
"""
Deletion unsuccessful due to database error.
Metadata state is unchanged, but database state is unknown.
"""
)
return
super(Databases, self).__delitem__(name=name, signal=False)
if signal:
on_database_delete.send(name=name)

@mrvisscher
Copy link
Author

Same goes for parameterized exchanges:

ActivityParameter.recalculate_exchanges(group, signal=False)

Exchanges are recalculated, but the signal is hardcoded to False. The signal is not emitted meaning models dependent on that signal will be out of sync.

@mrvisscher
Copy link
Author

mrvisscher commented Jan 29, 2025

Similarly, it would be very useful to get a signal for exchanges being modified when parameters are updated, but signals default back to false:

for obj in ParameterizedExchange.select().where(ParameterizedExchange.group == group):
exc = ExchangeDataset.get(id=obj.exchange)
exc.data["amount"] = interpreter(obj.formula)
exc.save(signal=False)

@mrvisscher
Copy link
Author

mrvisscher commented Feb 5, 2025

Similarly, on database write, the default is not True but defined by projects.dataset.is_sourced

if signal is None:
signal = projects.dataset.is_sourced
def merger(d1: dict, d2: dict) -> dict:

No problem when calling .write directly as we can set signal to true ourselves. But when calling .copy, .write is called without defining signal=True, which means we don't get database write signals for copied databases.

new_database.write(data, searchable=databases[name].get("searchable"))

On a related note: what is the sourced dataset functionality exactly? I can find many references to it in your code, but can find no documentation on it. I assume it has something to do with multi-user support?

@mrvisscher mrvisscher changed the title No on_database_metadata_change signal sent when deleting database Signals not always emitted on model changes Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant