-
Notifications
You must be signed in to change notification settings - Fork 10
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
How to do equality comparision of objects when properties are lazily defined #43
Comments
Ok, so going through documentation, one solution for me would be to use However, the problem is that in my usecase these fields really are lazily initialised and may not really exist in the object. I have a bunch of enrichers which add these fields as the object passes through a pipeline. Until then I really want them to be None or Empty.
I realize that this may be a very custom usecase - so I need a proper eq maybe? |
Ah, I guess defaulting to None was the problem :) Not using that solves my problem. Sorry for creating unnecessary issue. One reason why I was defaulting to None. My class is something like:
I guess now I have to do a try except Btw, One clean way which would solve my issue is adding a |
Thanks @devashishshankar for digging on this ! Let's backtrack a little bit on your initial need, not on the workarounds that you have found so far. If I understand your problem correctly,
So based on the above, your need should be solved by Except that you seem to be talking about validation. Here comes the |
Thank you! Optional works perfectly for me. For me nullable and Optional mean the same thing, I don't see why nullable would be useful if typing already has support for Optional Autoclass and Autodict look great! Will definitely use it in future. In this case, though, pyfields works better for me since I am inheriting from a superclass (which is in a common lib I don't want to change right now). Don't know if autoclass is compatible with inheritance. |
indeed they do, that's why many of us asked for a rename ( The open issue #44 is mostly for compatibility with versions of python that do not have Concerning autoclass (or its subtasks autodict, autohash, etc.), it sources its attribute list today from the constructor signature. Tomorrow I will have it detect pyfields and therefore source the list from the list of fields. It shoud support inheritance, yes. Shall we close this ticket then ? |
Yeah, correct. Optional can refer to fields with default values (which need not be passed while constructing the object). I guess here I've made my fields both Nonable and Optional (by setting default to None) - which serves my case quite well. I don't want to differentiate between field not being set, and and field set to None, so I set them to None by default. A side effect of this is also a simpler
Okay, I guess
Ah okay. How will it be different from just using pyfield's
Yes, closing it. |
Exactly. Using
Indeed ! You can consider Thanks again for all the feedback ! I'll work on fixing all this today and tomorrow |
For information: version |
Great! Like that it infers it is nonable if default=None |
I'm facing an issue while using pyfields, it has to do with the fact that properties are lazily initialized. Some of my tests require doing an equality comparison of two objects.
Let's say I have the following class:
Now,
Here, a1 !=a2 since calling a1.field would set a
_field
variable inside the instance.This is not exactly a bug, but can you think of any clean way of handling this with pyfields?
The text was updated successfully, but these errors were encountered: