Recent writings

Modify iterables while iterating in Python

Safely modify lists, sets, and dictionaries while iterating using list comprehensions, filters, or copying to avoid skipping elements.

Github action template for Python based projects

Production-ready GitHub Actions workflow for Python with multi-OS testing, dependency caching, automated updates, and daily scheduled runs.

Self type in Python

Use Python's Self type from PEP 673 to annotate methods returning class instances, eliminating complex forward references and TypeVars.

Patching test dependencies via pytest fixture & unittest mock

Combine pytest fixtures with unittest.mock.patch for clean, reusable test mocking patterns that integrate seamlessly with pytest's ecosystem.

Narrowing types with TypeGuard in Python

Use Python's TypeGuard to create custom type narrowing functions that help static type checkers understand runtime type checks and validations.

Why 'NoReturn' type exists in Python

Use Python's NoReturn type to annotate functions that never return normally, helping type checkers understand exception-raising and infinite loop code.

Add extra attributes to enum members in Python

Master adding multiple attributes to Python enum members using __new__ method. Avoid hardcoded indexes and create more maintainable enums.

Peeking into the internals of Python's 'functools.wraps' decorator

Explore how functools.wraps preserves function identity by copying metadata from wrapped functions using update_wrapper and partial application.

Limit concurrency with semaphore in Python asyncio

Control concurrent async requests with Python asyncio.Semaphore to respect rate limits and prevent overwhelming APIs or services.

Amphibian decorators in Python

Build Python decorators that work seamlessly with both sync and async functions using inspect.iscoroutinefunction for maximum flexibility.

Go Rusty with exception handling in Python

Implement Rust-style Result types for type-safe exception handling in Python using generic Ok and Err types inspired by Black formatter.

Variance of generic types in Python

Understand covariance, contravariance, and invariance in Python generics with practical examples of type relationships and subtyping rules.

Create a sub dictionary with O(K) complexity in Python

Optimize Python dictionary slicing from O(DK) to O(K) complexity using direct key lookups instead of iterating through all items.

Gotchas of early-bound function argument defaults in Python

Avoid Python function default argument pitfalls caused by early binding, where mutable defaults and function calls bind at definition time.

Use 'assertIs' to check literal booleans in Python unittest

Test literal booleans correctly in Python unittest using assertIs instead of assertTrue/assertFalse to avoid truthy/falsy confusion.