-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
Investigate schema improvements/autocast #158
Comments
I totally forgot about the recursive nature of auto_cast. Well, that was only a pseudocode I've though while writing the comment, it's more of an algorithm draft than actual code. Need to think more about this, but my initial feeling is that yes, auto_cast should be recursive. This would be set by the parent's OTOH, not sure how useful a recursive auto cast would be. I initially wanted it to make it easy to assign values to leaf keys, so I could write But in a recursion, we lose that information. In |
I agree that the scope for this is a bit fuzzy right now. Auto-casting could be just a minimal quality-of-life thing, or affect the entire hierarchy you're working with, or be some sort of automatic deduction and application of schemas. I'll wait until lower-level issues are fleshed out before messing around with it. |
Just a self-note: there are cases where I do not want auto-cast not only because of performance. I intend to intentionally set some keys to something else, like a custom class. for example Well, of course both |
I think its time I paste that pseudo-almost-actual-code from the "main" issue as a reference: def __init__(*args, *, auto_cast=False, **kwargs): # or __new__
self.auto_cast = auto_cast
@property
def auto_cast(self, value: bool) -> bool:
return self.autocast_setitem == super().__setitem__
@autocast.setter
def auto_cast(self, value: bool) -> None:
# should we recurse that too? slippery slope...
self.__setitem__ = self._autocast if value else super().__setitem__
def _autocast(self, key, value) -> None:
if not isinstance(value, Base) and key in self: # and isinstance(self[key], Base) too?
# Overwriting existing key with a non-tag value. Cast to old value type (if old is a tag)
super()[key] = self[key].__class__(value) # non-recursive version
return
super()[key] = value This would default to Or maybe File.load() should not even bother to turn it on, or at least not by default. Auto-cast is for users, so clients can manually set |
Speaking of this... I think this is right issue to tackle on a more profound subject: What is a tag afterall?
Not sure if there's any point in defining this, but sometimes I wonder what should be the "canonical" way of checking if something is a tag. Btw, currently I'm using |
The way I intended it is that something is a concrete tag if it inherits from |
You can always add a
That's a lot of trouble for just a nitpick, btw. |
Original conversation:
nbtlib 2.0
#156 (comment)nbtlib 2.0
#156 (comment)nbtlib 2.0
#156 (comment)The text was updated successfully, but these errors were encountered: