Skip to content

CardKit sequence example raises SyntaxError at module scope #161

Description

@dajiaohuang

Affected version

  • lark-oapi 1.7.3
  • Default branch commit: 0b9e6e48b74bb4b34462fc67b7e738b27e73e697
  • File: doc/channel/cardkit-streaming.md, recommended sequence pattern

Problem

The recommended CardKit sequence snippet declares seq at module scope and then uses nonlocal seq inside patch():

seq = 0

async def patch(text):
    nonlocal seq
    seq += 1
    await channel.update_card_element_content(card_id, "main", text, sequence=seq)

nonlocal can only reference a binding in an enclosing function scope, not a module-level binding, so copying or compiling the example raises:

SyntaxError: no binding for nonlocal 'seq' found

Reproduction

source = '''
seq = 0
async def patch(text):
    nonlocal seq
    seq += 1
'''
compile(source, "cardkit-example.py", "exec")

The repository-wide documentation check compiled 71 explicit Python code fences with ast.PyCF_ALLOW_TOP_LEVEL_AWAIT; this was the only fence that failed.

Expected behavior

The recommended pattern should be syntactically valid when copied into an application.

Suggested fix

Place both seq and the nested patch() helper inside an enclosing async function, keeping nonlocal seq meaningful, or use another state pattern that compiles at module scope. The lower-level complete example later in the same page already uses a valid direct increment pattern.

I searched open and closed issues, all pull request states, and repository history for nonlocal seq, CardKit streaming, and the document path. I found no existing report or competing fix.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions