Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 1, 2025

Eliminates Python anti-pattern B006 where mutable objects ([], {}) used as default parameter values cause shared state across function calls.

Changes

Fixed 6 instances across 4 files by replacing mutable defaults with None and initializing inside the function:

  • astrbot/core/star/filter/command.py - CommandFilter.__init__()
  • astrbot/core/platform/astr_message_event.py - request_llm() (2 parameters)
  • astrbot/core/provider/sources/dashscope_source.py - text_chat()
  • astrbot/core/utils/dify_api_client.py - chat_messages() and workflow_run()

Pattern Applied

# Before - shared state bug
def request_llm(self, image_urls: list[str] = [], contexts: list = []):
    return ProviderRequest(image_urls=image_urls, contexts=contexts)

# After - isolated state
def request_llm(self, image_urls: list[str] | None = None, contexts: list | None = None):
    if image_urls is None:
        image_urls = []
    if contexts is None:
        contexts = []
    return ProviderRequest(image_urls=image_urls, contexts=contexts)

No functional changes - behavior remains identical.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.soulter.top
    • Triggering command: /home/REDACTED/work/AstrBot/AstrBot/.venv/bin/python /home/REDACTED/work/AstrBot/AstrBot/.venv/bin/pytest tests/ -v (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

搜索项目内是否存在构造函数参数默认值为可变对象的情况,如果有,请修正。


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: LIghtJUNction <106986785+LIghtJUNction@users.noreply.github.com>
@LIghtJUNction
Copy link
Contributor

默认值不可以用可变对象,比如list
比较详细的原因分析:【【Python】我精心设计的默认参数,怎么就出问题了呢?-哔哩哔哩】 https://b23.tv/l1j7soM

@LIghtJUNction LIghtJUNction marked this pull request as ready for review November 1, 2025 18:35
Copilot AI changed the title [WIP] Fix default values for constructor parameters of mutable objects Fix mutable default arguments in constructors and methods Nov 1, 2025
Copilot AI requested a review from LIghtJUNction November 1, 2025 18:36
Copilot finished work on behalf of LIghtJUNction November 1, 2025 18:36
@LIghtJUNction LIghtJUNction requested a review from Copilot November 1, 2025 18:39
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors default parameter handling to follow Python best practices by replacing mutable default arguments (empty lists [] and [""]) with None and initializing them within the function body.

  • Replaced mutable default arguments with None across multiple files
  • Added initialization checks at the start of affected functions
  • Applied the pattern consistently to files, image_urls, contexts, and parent_command_names parameters

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
astrbot/core/utils/dify_api_client.py Changed files parameter defaults from [] to None in chat_messages and workflow_run methods
astrbot/core/star/filter/command.py Changed parent_command_names parameter default from [""] to None in CommandFilter.__init__
astrbot/core/provider/sources/dashscope_source.py Changed image_urls parameter default from [] to None in text_chat method
astrbot/core/platform/astr_message_event.py Changed image_urls and contexts parameter defaults from [] to None in request_llm method

@LIghtJUNction LIghtJUNction merged commit 92abc43 into master Nov 2, 2025
13 checks passed
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.

2 participants