Skip to content

fix(typing): Message.topic also holds a DirectMessagesTopic - #368

Open
Danipulok wants to merge 1 commit into
KurimuzonAkuma:devfrom
Danipulok:fix/message-topic-type
Open

fix(typing): Message.topic also holds a DirectMessagesTopic#368
Danipulok wants to merge 1 commit into
KurimuzonAkuma:devfrom
Danipulok:fix/message-topic-type

Conversation

@Danipulok

Copy link
Copy Markdown
Contributor

What

Message.topic is annotated Optional["types.ForumTopic"] at message.py:655, and in a
channel direct-messages chat it holds a DirectMessagesTopic. message.py:1870, in the
branch guarded by chat.type == enums.ChatType.DIRECT:

parsed_message.topic = await client.get_direct_messages_topics_by_id(
    chat_id=parsed_message.chat.id,
    topic_ids=parsed_message.direct_messages_topic_id
)

The cache branch a few lines above it does the same. So reading message.topic.icon_color
after a ForumTopic-shaped check passes the type checker and raises at runtime in a direct
chat, and the honest code — checking which of the two it is — is reported as unreachable.

The change

The annotation, and the docstring entry that describes it:

topic: Optional[Union["types.ForumTopic", "types.DirectMessagesTopic"]] = None,

I took the assignment as correct and the annotation as the thing that was behind — the
direct-messages topic is a real, separate type with its own fetch method, so narrowing the
other way would mean deciding that message.py:1870 is the bug. If you would rather it went
that way, say so and I will redo it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant