tinycodinglm / code /code201.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame
1.82 kB
deepseek-harness trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

What are best practices for Python type hints?

Assistant

Basic Types:

python
def greet(name: str) -> str:
    return f"Hello, {name}"

items: list[int] = [1, 2, 3]
scores: dict[str, float] = {"alice": 95.5}

Generics and TypeVars:

python
from typing import TypeVar, Generic

T = TypeVar("T")

class Container(Generic[T]):
    def __init__(self, value: T) -> None:
        self.value = value

Protocols (Structural Typing):

python
from typing import Protocol

class Drawable(Protocol):
    def draw(self) -> None: ...

def render(obj: Drawable) -> None:
    obj.draw()

Useful Patterns:

  • Optional[T] = T | None (Python 3.10+)
  • Union[T1, T2] = T1 | T2 (Python 3.10+)
  • @overload for multiple signatures
  • typing.Final for constants
  • typing.Never for unreachable code

Tools:

  • mypy for static checking
  • pyright for fast type checking
  • --strict for maximum safety

Tips:

  • Start with function signatures
  • Use from __future__ import annotations
  • Don't over-type (trust inference)
  • Use Protocol for duck typing
  • Run mypy in CI