Skip to content

Remove return_obj from Entity parse/serialize/transform methods #18

@bworrell

Description

@bworrell

This would need to carry through to python-stix|cybox|maec. We don't need it except in places where we have extension points.

We have code that passes around a return_obj to handle type inheritance in to/from dict/obj methods.

class Foo(Entity):
    _binding_class = some.binding.classname

    def to_obj(self, return_obj=None, ns_info=None):

        if return_obj is None:
            return_obj = self._binding_class()

        return_obj.value = self.value
        return return_obj

class DerivedFoo(Foo):
    _binding_class = some.other.binding.classname

    def to_obj(self, return_obj=None, ns_info=None):
        if return_obj is None:
            return_obj = self._binding_class()

        # Apply super attributes to the return_obj we created here
        super(DerivedFoo, self).to_obj(return_obj=return_obj, ns_info=ns_info)

        # Apply our own attribtes and return
        return_obj.foo = "bar"
        return return_obj

But it could be replaced with code that makes better use of super().

class Foo(Entity):
    _binding_class = some.binding.classname

    def __init__(self):
        self.value = "value"

    def to_obj(self, ns_info=None):
        obj = self._binding_class()
        obj.value = self.value
        return obj


class DerivedFoo(Foo):
    _binding_class = some.other.binding.classname

    def __init__(self):
        super(DerivedFoo, self).__init__()
        self.bar = "bar"

    def to_obj(self, ns_info=None):
        obj = super(DerivedFoo, self).to_obj(ns_info=ns_info)
        obj.foo = self.foo
        return obj

Most of this would be in python-stix|cybox|maec, but the changes would start in mixbox.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions