agentleFS
FullFranFullFran/Agent-skills-POC/.agents/skills/python-async-patterns/SKILL.md

python-async-patterns

Skill1 starsChanged 10 months agoMITView on GitHub

Best practices for async Python code, avoiding common pitfalls like await precedence bugs and sync-in-async anti-patterns.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 2 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use this skill when:

    line 12
  2. Very specific:

    Rule: Never use await inside a tuple literal.

    line 38
  3. Very specific:

    Rule: If using a sync library in async code, wrap calls with asyncio.to_thread().

    line 62
  4. Very specific:

    Rule: Always verify return type matches the actual return value.

    line 82
  5. Distinctive:

    Rule: Create domain-specific exceptions that capture relevant context.

    line 105

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Python Async Patterns Skill

Expert guidance on writing correct and performant async Python code. These patterns prevent common bugs discovered in code reviews.

Activation

Use this skill when:

  • Writing async def functions
  • Working with asyncio event loops
  • Integrating sync libraries in async code
  • Defining type hints for async functions

Critical Patterns

1. Await Precedence Bug

Problem: The comma operator has higher precedence than await.

# ❌ WRONG - Returns (coroutine, value) instead of (result, value)
async def search(query: str) -> tuple[list, str]:
    return await repository.search(query), query  # BUG!

# ✅ CORRECT - Assign result first, then create tuple
async def search(query: str) -> tuple[list, str]:
    results = await repository.search(query)
    return results, query

Rule: Never use await inside a tuple literal. Always assign the awaited result to a variable first.


2. Sync-in-Async Anti-Pattern

Problem: Calling synchronous I/O inside async functions blocks the event loop.

# ❌ WRONG - Blocks event loop
async def save_document(doc: Document) -> str:
    result = sync_client.insert(doc)  # BLOCKING!
    return result.id

# ✅ CORRECT - Wrap with asyncio.to_thread()
import asyncio

async def save_document(doc: Document) -> str:
    result = await asyncio.to_thread(
        lambda: sync_client.insert(doc)
    )
    return result.id

Rule: If using a sync library in async code, wrap calls with asyncio.to_thread().


3. Type Hints for Async Functions

Problem: Return type doesn't match what the function actually returns.

# ❌ WRONG - Says it returns List but actually returns tuple
async def search(query: str) -> List[Match]:
    results = await repo.search(query)
    return results, query  # Actually tuple!

# ✅ CORRECT - Accurate type hint
async def search(query: str) -> tuple[List[Match], str]:
    results = await repo.search(query)
    return results, query

Rule: Always verify return type matches the actual return value. Use tuple[T1, T2] for tuples.


4. Domain Exceptions Over Generic Exceptions

Problem: Generic exceptions lack context for debugging.

# ❌ WRONG - No context
if not result.data:
    raise Exception("Failed to save")

# ✅ CORRECT - Rich context
from src.core.exceptions import DocumentSaveError

if not result.data:
    raise DocumentSaveError(
        cause="No data returned from insert",
        document_id=doc.id
    )

Rule: Create domain-specific exceptions that capture relevant context.


Quick Reference

Anti-PatternFix
return await foo(), barAssign first: x = await foo(); return x, bar
sync_call() in asyncWrap: await asyncio.to_thread(sync_call)
-> List[T] returns tupleFix: -> tuple[List[T], str]
raise Exception(msg)Use: raise DomainError(context)

Related Files

  • exceptions.py - Domain exceptions
  • supabase_repository.py - Example async wrapper usage

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.