2

Parent class has a property called 'deserialize' that is static and abstract with one argument. Each Child class implemented that method. Now I have a situation that Child class needs more than one argument. When I add options=None to Parent class, children classes complain that they have a different signature(warning). I have to add options=None to each class. That is a refactoring. I want to know if I can omit the warning and continue, or there is a better solution? Or do I have to refactor?

class Serializable:
    __metaclass__ = ABCMeta

    @staticmethod
    @abstractmethod
    def deserialize(json_obj, options=None):
        pass

class ChildWithNoExtraArguments(Serializable):

   # warning is here...
   @staticmethod        
   def deserialize(json_obj):
        # some implementation

class ChildWithExtraArgumnets(Serializable):

    @staticmethod
    def deserialize(json_obj, options):
        # some implementation, I need options
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Amir Shabani
  • 690
  • 2
  • 14
  • 36

1 Answers1

3

You need to decorate your child classes deserialize implementation with @staticmethod too. The exception you're seeing is because python is automatically adding self to each of the method calls. Decorating then with @staticmethod stops this behavior.

Additionally, you're second implementation needs to define options as a keyword argument. Keyword arguments have default values, for instance: options=None.

class Serializable:
    __metaclass__ = ABCMeta

    @staticmethod
    @abstractmethod
    def deserialize(json_obj, options=None):
        pass

class ChildWithNoExtraArguments(Serializable):

   # warning is here...        
    @staticmethod
    def deserialize(json_obj, options=None):
        # some implementation

class ChildWithExtraArgumnets(Serializable):

    @staticmethod
    def deserialize(json_obj, options=None):
        # some implementation, I need options
Matt Hardcastle
  • 638
  • 3
  • 9
  • Sorry, I added @staticmethod to subclasses. I updated my question. – Amir Shabani Feb 14 '18 at 18:24
  • We're almost there. I updated my response to address your other issue. Hopefully that helps. – Matt Hardcastle Feb 14 '18 at 18:57
  • Thanks, I know that I can do this. I have to refactor all of my methods. So maybe this is the only solution. – Amir Shabani Feb 15 '18 at 05:30
  • 1
    you could use the `deserialize(json_obj, **kwargs)` to suck up options, and any other keyword arguments. The `**` is a bit of syntax that tells Python to put any remaining keyword arguments into a dictionary named kwargs. – Matt Hardcastle Feb 15 '18 at 18:34