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

Preferences can't be unset if they are the only one on a given node. #312

Open
nicolasap-dm opened this issue Jul 14, 2022 · 2 comments
Open

Comments

@nicolasap-dm
Copy link

From the IPreference documentation, it's unclear to me whether I should use preferences.clear(key) or preferences.remove(key) to un-set a preference.

However, it looks as though neither works.

Is it possible to remove a preference altogether? And if so, should one of these two methods work to that end?

Expected

For preferences that are stored to file, if there is a preference at some key (namespace.key), it should be possible to remove that preference altogether.

Consider this initial situation:

>>> def cat(fname):
...     with open(fname, "r") as f:
...         print(f.read())
... 
>>> preferences = Preferences(filename="example.ini")
>>> preferences.set("namespace.field", "value")
>>> preferences.flush()
>>> cat("example.ini")
[namespace]
field = value

I would expect that

preferences.remove("namespace.field")
preferences.flush()

would result in a empty example.ini file.

Observed

Neither approach works:

>>> preferences.remove("namespace.field")
>>> preferences.flush()
>>> cat("example.ini")
[namespace]
field = value

>>> preferences.clear("namespace.field")
>>> preferences.flush()
>>> cat("example.ini")
[namespace]
field = value
@nicolasap-dm
Copy link
Author

nicolasap-dm commented Jul 14, 2022

Note: I have just observed that only the last remaining preference of a namespace can't be removed.

If there is an existing other field in the same namespace, then the other field can be removed.

>>> preferences.set("namespace.field", "value")
>>> preferences.set("namespace.other_field", "other_value")
>>> preferences.flush()
>>> cat("example.ini")
[namespace]
field = value
other_field = other_value

>>> preferences.remove("namespace.field")
>>> preferences.flush()
>>> cat("example.ini")
[namespace]
other_field = other_value

I'm not sure if this limitation is expected, but it doesn't seem to be documented in the apidocs.

The general docs don't cover removing/un-setting preferences.


I'm updating the issue name.

@nicolasap-dm nicolasap-dm changed the title Preferences can't be unset Preferences can't be unset if they are the last one on a given node. Jul 14, 2022
@nicolasap-dm
Copy link
Author

nicolasap-dm commented Jul 14, 2022

The behavior is actually particularly odd if, on the same preferences object, another field is added after the attempted removal of the initially only field.

At first, removing the only field doesn't work. But then, if another field is added, the previous remove operation takes effect.

>>> preferences = Preferences(filename="example.ini")
>>> preferences.set("namespace.field", "value")
>>> preferences.flush()
>>> cat("example.ini")
[namespace]
field = value

>>> preferences.remove("namespace.field")
>>> cat("example.ini")
[namespace]
field = value

>>> preferences.set("namespace.other_field", "other_value")
>>> preferences.flush()
>>> cat("example.ini")
[namespace]
other_field = other_value

>>> # NOTE: this operation appears to have both added a field and
>>> # removed another field from the file (which was not done before).

To summarize: it seems like the Preferences don't want to have empty namespaces on file (not sure if that's a .ini limitation), so they won't remove the last remaining field from a namespace.

However, the object will have registered that removal (unbekownst to the user) and may still "apply" it on a subsequent flush() if other conditions have changed.

Both the discrepancy between the state of the file and the state of the object, and the undocumented limitation, should be addressed. In general, key removal should be documented in the main Preferences docs.

@nicolasap-dm nicolasap-dm changed the title Preferences can't be unset if they are the last one on a given node. Preferences can't be unset if they are the only one on a given node. Jul 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants