python-suitcase icon indicating copy to clipboard operation
python-suitcase copied to clipboard

BaseField instantiate=True seems to fail

Open djmoreho opened this issue 10 years ago • 3 comments

Creating a instance of a field will make it look like it fails because BaseField prints None, however it doesn't (I just spent a while seeing why it was 'failing').

Script that demonstrates the 'fail'

from suitcase.fields import UBInt8

# create our placeholder
u = UBInt8(instantiate=False)
print u # correct result

# create our actual object
u = UBInt8(instantiate=True)
print u # doesn't return a instance..... or does it

Fix would be simply changing the BaseField repr to,

def __repr__(self):
    return "Field(_value="+repr(self.getval())+")"

and we get a much better result that doesn't seem to fail, though having just the normal repr would give better information (i.e what field).

djmoreho avatar Jan 12 '16 10:01 djmoreho

@djmoreho What is your use case? There is some hackery with FieldPlaceholder and the like that were what I came up with at some point for supporting the primary use case (use of fields within the context of structures). Knowing your use case might help an opportunity for a suggestion on a better way to solve your problem or provide input for improving the design of suitcase for your use case.

posborne avatar Jan 13 '16 07:01 posborne

My use case was simply exploring the instantiate=True argument. When I was creating it at a python interactive prompt I ran into the repr None 'issue'. I mainly brought up the issue so if others run into it there is documented information. It isn't an actual issue (as the code works) but more a design decision.

djmoreho avatar Jan 13 '16 14:01 djmoreho

I think part of the problem is that the fields' values default to None. I just submitted a PR in #37 to address that. (Though, it wasn't specifically because of this issue.)

Taken on their own, it isn't clear that these fields are wrapping the value their __repr__ returns, but I'd prefer to leave the __repr__ as it is. It makes it much nicer to look at a complete structure.

rcfox avatar Jan 22 '16 23:01 rcfox