-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
(1 of 2) Items to discuss: metadata support #1467
Comments
Nice find @JohnAD ! I'm not a fan of what #1051 did as well. It basically delays the time when you learn about an invalid kwarg till runtime. I wish all the additional data you might want to store on a field was encapsulated by say a That said, I think I'd rather fix it in a major release (and document a breaking change) than start adding more and more "checkboxes" to MongoEngine's configuration. Let's just agree on the best way to do it moving forward and stick to that. Otherwise, this library is going to turn into a bunch of ifs and elses with spaghetti code trying to enable and disable features as anybody sees fit (with maintainers having to support all of the combinations). |
Unfortunately, use of MongoEngine itself turns application code into spaghetti if trees. Something I'm sad I missed was the unfortunate practice of wholly overriding Edited to add: very notably, the use of |
Writing a book or making a video is a great way to flush out ones knowledge of a subject; and it has the benefit of also discovering shortcomings of the product or library.
Yesterday, while filming a YouTube video covering fields in MongoEngine, two issues came up pretty strongly. One of them, ironically, was actually captured as part of the film. I'm listing them separately to help with discussion.
BTW, if curious, the video itself: MongoEngine Fields Ep 1: Overview and String Fields.
metadata
Allowing open-ended parameters to a function or class makes it much easier to accidentally write bugs that might not be caught. Or worse: caught long after the code was in production and data has been subtly corrupted.
In this particular case, I had written:
Which executed without errors. The problem was that it didn't work as expected. The proper argument should have been
choices
notchoice
. The reason I did not get aTypeError
is that awhile back support was added for arbitrary fields, metadata, in pull request #1051 .While I do see the benefit of supporting meta data, I'm not sure why that particular way of doing was chosen. I can think of a few way this could have been done with had less impact but perhaps there are complications I'm not aware of.
proposed solution
Add a field to the 'meta' dictionary in Document called
metadata
. It would default tometadata=True
to keep legacy behavior consistent.metadata=False
would turn off metadata support. Specifically, any entry found in**kwargs
insideBaseField
generates aTypeError
.Children of this class might need to be modified to handle the stricter rules. But most will not. For example
StringField(BaseField)
will not need to be changed.Libraries flask-mongoengine and django-mongoengine should be checked against this change, just in case. Ironically, these libraries could use 'metadata=False' also if they 'absorb' their own arguments prior to calling
super
.going further
(with other possibilities in the future)
Even better, add a global setting. Such as:
Any
meta
entry could have a default applied to all documents unless specifically overridden.The text was updated successfully, but these errors were encountered: