alexander-danilenko/cortex-ai-skills

python

Apply these opinionated Python 3.11+ conventions when writing Python in this codebase: type hints with mypy, async/await, pytest fixtures, dataclasses, Poetry packaging, production patterns.

소스 보기

사용 방법

  1. 설치 명령을 복사하고 자신의 에이전트 환경에 설치하기 전에 소스 패키지를 확인하세요.
  2. 필요한 도구, 인증 정보, 지원 환경은 원본 문서에서 확인하세요.
  3. 입력과 원하는 산출물을 설명하고 출처와 결과를 대조하세요.
npx skills add https://github.com/alexander-danilenko/cortex-ai-skills --skill python

시작 전 확인

  • 설치에는 모델 구독, API 한도, 파일 접근 권한이 포함되지 않습니다.
  • 별점 수와 설치 수는 활동 지표이며 독립적인 품질·보안 검증이 아닙니다.
원본 Skill 문서

원본 저장소의 제목, 예시, 코드, 표, 링크, 이미지를 유지해 표시합니다.

Python

House conventions for Python 3.11+. Apply them to code you are writing or changing — don't refactor untouched files to match unless asked.

Conventions

  • Type every public signature and keep mypy --strict green. Types on internals are optional; types at the boundary are what stop a caller passing the wrong thing.
  • Built-in generics and `X | None` — list[str], dict[str, int], str | None. typing.List and Optional[str] are the pre-3.10 spelling and only cost an import.
  • `Protocol` over ABC inheritance. Structural typing lets any correctly-shaped object satisfy the contract — including a test double — with no inheritance tree to maintain.
  • Dataclasses for data, slots=True and frozen=True where they fit. They generate __init__, __repr__, and __eq__ correctly; a hand-written __init__ is where field drift starts. Pydantic is for validation and (de)serialisation at a boundary, not for plain records.
  • `pathlib`, not `os.path`. Operator joins can't silently produce a wrong path from a stray separator.
  • Never a mutable default argument. def f(items=[]) shares one list across every call — a bug that only appears on the second call. Default to None and build inside.
  • `asyncio.TaskGroup` over bare `gather` (3.11+): it cancels siblings on failure and reports via ExceptionGroup, so a crashed task can't leave the rest running detached. Use async with asyncio.timeout(n) for deadlines.
  • Hold a reference to every `create_task`. The event loop only keeps a weak reference, so a fire-and-forget task can be garbage-collected mid-execution and simply vanish — no error, no result. Keep them in a set and discard on completion:
python
  _tasks: set[asyncio.Task[None]] = set()

  def spawn(coro: Coroutine[None, None, None]) -> None:
      task = asyncio.create_task(coro)
      _tasks.add(task)
      task.add_done_callback(_tasks.discard)
  • Never a bare `except:` — it swallows KeyboardInterrupt and SystemExit. Catch what you can actually handle.
  • Google-style docstrings on public functions and classes, carrying intent rather than a restatement of the signature. The documentation skill has the full rule.
  • pytest: fixtures for setup, `parametrize` for cases. A loop inside one test reports a single failure and hides which case broke; parametrize names each one.
  • Poetry and `pyproject.toml` for packaging, ruff for lint and format, and a py.typed marker on any package whose types consumers should see. Use a src/ layout — it stops tests from importing the working directory instead of the installed package, which is how a broken package still passes its own suite.

Tooling baseline

strict = true covers most of it; these add the checks that catch real bugs rather than style. --strict-markers matters more than it looks: without it a typo'd @pytest.mark.integraton silently does nothing and the test runs where you thought it was excluded.

toml
[tool.mypy]
strict = true
warn_unreachable = true
warn_redundant_casts = true
warn_unused_ignores = true

[[tool.mypy.overrides]]
module = "untyped_dep.*"
ignore_missing_imports = true

[tool.pytest.ini_options]
addopts = ["-ra", "--strict-markers", "--strict-config"]

관련 작업

같은 저장소의 Skills

더 많은 Skills

모든 Skills