2026 में Python 3.12+ के साथ Python का टाइप सिस्टम काफी परिपक्व हो गया है। पीईपी 695 (टाइप पैरामीटर सिंटैक्स), पीईपी 696 (टाइपवार डिफॉल्ट्स), और नयाtypeकथन पायथन जेनेरिक को अधिक पठनीय बनाता है। यह गहरा गोता उन्नत प्रकार के पैटर्न को कवर करता है जिसे अनुभवी पायथन डेवलपर्स को जानना चाहिए।
📋 Table of Contents
नया सिंटैक्स: स्टेटमेंट टाइप करें (पायथन 3.12+)
# Old way (still works)
from typing import TypeAlias, TypeVar
Vector = list[float]
T = TypeVar("T")
# New syntax (Python 3.12+, much cleaner)
type Vector = list[float]
type Matrix = list[Vector]
type JSON = str | int | float | bool | None | list["JSON"] | dict[str, "JSON"]
# Generic type alias
type Stack[T] = list[T]
type Pair[T, U] = tuple[T, U]
type Callback[T] = callable[[T], None]
# Use them
def push[T](stack: Stack[T], item: T) -> Stack[T]:
return [*stack, item]
numbers: Stack[int] = [1, 2, 3]
result = push(numbers, 4)
सामान्य वर्ग और कार्य (पायथन 3.12+)
# Old TypeVar syntax (still valid)
from typing import TypeVar
T = TypeVar("T")
def first(items: list[T]) -> T | None:
return items[0] if items else None
# New syntax (PEP 695)
def first[T](items: list[T]) -> T | None:
return items[0] if items else None
def map_list[T, U](items: list[T], fn: callable[[T], U]) -> list[U]:
return [fn(item) for item in items]
class Stack[T]:
def __init__(self) -> None:
self._items: list[T] = []
def push(self, item: T) -> None:
self._items.append(item)
def pop(self) -> T:
if not self._items:
raise IndexError("Stack is empty")
return self._items.pop()
def peek(self) -> T | None:
return self._items[-1] if self._items else None
def __len__(self) -> int:
return len(self._items)
# Usage
stack: Stack[str] = Stack()
stack.push("hello")
stack.push("world")
print(stack.pop()) # "world"
# Constrained TypeVar
def sort_items[T: (int, str, float)](items: list[T]) -> list[T]:
return sorted(items)
डिफ़ॉल्ट के साथ टाइपवार (पीईपी 696 – पायथन 3.13+)
from typing import TypeVar
# TypeVar with default
T = TypeVar("T", default=str)
class Container[T = str]:
def __init__(self, value: T) -> None:
self.value = value
def get(self) -> T:
return self.value
# T defaults to str if not specified
c1: Container = Container("hello") # T = str (default)
c2: Container[int] = Container(42) # T = int (explicit)
प्रोटोकॉल – संरचनात्मक उपप्रकार
from typing import Protocol, runtime_checkable
from collections.abc import Sequence
@runtime_checkable
class Sizeable(Protocol):
def __len__(self) -> int: ...
@runtime_checkable
class Comparable[T](Protocol):
def __lt__(self, other: T) -> bool: ...
def __le__(self, other: T) -> bool: ...
def __gt__(self, other: T) -> bool: ...
def __ge__(self, other: T) -> bool: ...
# Any class with __len__ satisfies Sizeable — no inheritance needed
def count_items(collection: Sizeable) -> int:
return len(collection)
print(count_items([1, 2, 3])) # works: list has __len__
print(count_items({"a": 1})) # works: dict has __len__
print(count_items("hello")) # works: str has __len__
# isinstance check (requires @runtime_checkable)
print(isinstance([1, 2], Sizeable)) # True
# Protocol with methods
class Serializable(Protocol):
def to_json(self) -> str: ...
def to_dict(self) -> dict: ...
@classmethod
def from_json(cls, json_str: str) -> "Serializable": ...
अधिभार – एकाधिक हस्ताक्षर
from typing import overload
@overload
def process(x: int) -> int: ...
@overload
def process(x: str) -> str: ...
@overload
def process(x: list[int]) -> list[int]: ...
def process(x):
if isinstance(x, int):
return x * 2
elif isinstance(x, str):
return x.upper()
elif isinstance(x, list):
return [item * 2 for item in x]
# Type checker knows the return type based on input type
result_int: int = process(5) # type checker: int
result_str: str = process("hi") # type checker: str
result_list: list[int] = process([1, 2, 3]) # type checker: list[int]
टाइप्डडिक्ट उन्नत पैटर्न
from typing import TypedDict, Required, NotRequired
# Partial TypedDict — some required, some optional
class UserCreate(TypedDict):
name: Required[str]
email: Required[str]
password: Required[str]
role: NotRequired[str] # optional
# Inheritance
class UserUpdate(TypedDict, total=False):
name: str
email: str
role: str
# Nested TypedDict
class Address(TypedDict):
street: str
city: str
country: str
zip_code: NotRequired[str]
class UserWithAddress(TypedDict):
id: int
name: str
address: Address
# Use in function signatures
def create_user(user: UserCreate) -> int:
return db.insert("users", user)
def update_user(user_id: int, updates: UserUpdate) -> bool:
return db.update("users", user_id, updates)
परमस्पेक – टाइप-सेफ डेकोरेटर्स
from typing import ParamSpec, TypeVar, Callable
import functools
P = ParamSpec("P")
R = TypeVar("R")
def retry(max_attempts: int = 3):
def decorator(func: Callable[P, R]) -> Callable[P, R]:
@functools.wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
for attempt in range(max_attempts):
try:
return func(*args, **kwargs)
except Exception as e:
if attempt == max_attempts - 1:
raise
raise RuntimeError("unreachable")
return wrapper
return decorator
@retry(max_attempts=3)
def fetch_data(url: str, timeout: int = 30) -> dict:
return requests.get(url, timeout=timeout).json()
# Type checker knows: fetch_data(url: str, timeout: int = 30) -> dict
# The decorator is fully transparent to the type system
2026 में नए पीईपी 695 सिंटैक्स के साथ पायथन का टाइप सिस्टम, टाइपवार-आधारित सिंटैक्स की तुलना में काफी अधिक पठनीय है। मुख्य पैटर्न: संरचनात्मक टाइपिंग के लिए प्रोटोकॉल का उपयोग करें, टाइप-सुरक्षित डेकोरेटर्स के लिए पैरास्पेक, टाइप किए गए शब्दकोशों के लिए टाइप्डडिक्ट, एकाधिक हस्ताक्षरों के लिए @ओवरलोड, और स्वच्छ उपनामों के लिए नए प्रकार के स्टेटमेंट का उपयोग करें। इन पैटर्न को लागू करने के लिए pyright –strict या mypy –strict चलाएँ।
🔗 Share this article
✍️ Leave a Comment