Skip to content

Convert _id_attrs to a class variable #5249

@harshil21

Description

@harshil21

What kind of feature are you missing? Where do you notice a shortcoming of PTB?

One thing I noticed is that while digging around for potential improvements is that we currently define the _id_attrs in the __init__ of every class, but the information it represents is static and doesn't change with every instance* (see exceptions below)

I propose representing that information as a class variable with strings, which will serve as forward references.

This mainly has 2 advantages:

  1. When switching to dataclasses (insert future issue number), we will not need a __post_init__ method to define what attributes to use for equality comparison.
  2. Memory savings. This would be tiny of course (saving one 8 byte slot pointer every instance + the pointers to the data in the tuple, so at least 48B)

Describe the solution you'd like

This change would move the evaluation of _id_attrs from init time to equality checking time. Inside __eq__, we can iterate through the _id_attrs, and call getattr to fully resolve it. Of course this incurs a performance overhead, but I don't believe most users are ever comparing TelegramObjects that often..

Exceptions

  1. PassportData._id_attrs is doing some dynamic assignments. I'm not sure why it was done that way, that is probably not needed and can be just static.
  2. InlineKeyboardButton._set_id_attrs can be deleted since __eq__ will dynamically fetch the current callback data, so there's nothing to update in the tuple itself.

Describe alternatives you've considered

No response

Additional context

There is the concern of typos in the strings typed out, but since we have equality tests for every class, they should be caught.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Priority

    Low

    Effort

    High

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions