Skip to content
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

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

Open
lifefossil opened this issue Jun 27, 2022 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@lifefossil
Copy link

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 rayanht added the bug Something isn't working label Jun 28, 2022
@rayanht rayanht self-assigned this Jun 28, 2022
@rayanht
Copy link
Owner

rayanht commented Jun 28, 2022

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

@adrian-herscu
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants