rayanht / paprika

Paprika is a python library that reduces boilerplate. Heavily inspired by Project Lombok.
MIT License
80 stars 3 forks source link

@to_string: decorated_class.__dict__[attr], "__call__")) KeyError: 'xxx' #28

Open lifefossil opened 2 years ago

lifefossil commented 2 years ago

When I decorated my class with @to_string decorator, I got a KeyError. I found that there is a problem with the source code after I debugged.

  attributes = [
      attr
      for attr in dir(self)
      if not attr.startswith("_") and not (
          hasattr(self.__dict__[attr], "__call__") if attr in self.__dict__ else hasattr(
              decorated_class.__dict__[attr], "__call__"))
  ]

After I print dir(self) and self.__dict__, the attr of dict(self) are more than self.__dict__, thus result in the self.__dict__[attr] KeyError

rayanht commented 2 years ago

Hey, thanks for flagging this! Could you provide the class that is being decorated so I can repro this locally?

adrian-herscu commented 3 months ago
def test_pack_unpack(self):
        @to_string
        class Point(Struct):
            """
            Semantic eq method is provided by Struct
            @attr.define -- does not play well with the super-class
            @dataclass -- does not play well with the super-class
            """
            _endianess = Endianess.LittleEndian
            x = FieldType.WORD
            y = FieldType.WORD
            name = FieldType.String[6]("NONAME")

        original_point = Point(x=1, y=2)
        packed_point = original_point.pack()
        assert_that(packed_point,
                    is_(b'\x01\x00\x02\x00NONAME'))

        unpacked_point = Point.unpack(packed_point)
        assert_that(unpacked_point,
                    is_(original_point))
        self.log().debug(unpacked_point)

where Struct is defined at https://github.com/mrdor44/stru/blob/master/stru/stru_struct.py#L7

This test fails on its last line with:

.0 = <list_iterator object at 0x7f04687c6c20>

    attributes = [
        attr
        for attr in dir(self)
        if not attr.startswith("_")
        and not (
            hasattr(self.__dict__[attr], "__call__")
            if attr in self.__dict__
>           else hasattr(decorated_class.__dict__[attr], "__call__")
        )
    ]
E   KeyError: 'pack'

tests/python/venv/lib/python3.10/site-packages/paprika/oo.py:21: KeyError