Typed Python in 2024: Well adopted, yet usability challenges persist

👁 270 بازدید ❤️ 3 واکنش 📤 3 فوروارد

Author: Aaron Pollack

Site: Engineering at Meta

Source: https://engineering.fb.com/2024/12/09/developer-tools/typed-python-2024-survey-meta

⚠️ Article content is partial (Telegram Instant View delivered in segments).

167_article_cover.jpg

167_article_photo_1.jpg

167_article_photo_2.jpg

✨ تایپ هینت‌ها توی پایتون در سال ۲۰۲۴: محبوب ولی هنوز چالش‌دار

🐍 فارغ از اینکه تکنولوژی اصلی‌مون برای توسعه‌ چی باشه، یا حتی عنوان شغلی‌مون توی صنعت نرم‌افزار چی باشه، بلد بودن پایتون خیلی کار راه بندازه! احتمالا همگی دلایل صحبتم رو می‌دونید یا شاید با کمی جستجو تایید کنید.
توی این مطلب به بهانه مرور نتایج یک نظرسنجی در مورد کاربرد تایپ‌هینت‌ها، خود تایپ‌هینت رو هم به اختصار توضیح می‌دم.

PEP 484 چیه؟
تایپ‌هینت توی زبان‌های داینامیک تایپ (Dynamic Typing) مثل پایتون یا جاوا اسکریپت، این امکان رو به توسعه‌دهنده‌ها می‌ده تا «نوع» داده‌ای ورودی‌ها و خروجی توابع و متغیرها رو مشخص کنن.
مثلا به جای:

def greet(name):
    return f"Hello, {name}!"

بنویسن:

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

سال ۲۰۱۴، Guido van Rossum (خالق پایتون) و Jukka Lehtosalo (خالق mypy) امکان تایپ هینت (Type Hint) رو در قالب PEP 484 معرفی و به پایتون اضافه کردن. تایپ هینت اجباری نیست، یعنی کد پایتون بدون اون هم اجرا میشه، اما وقتی ازش استفاده کنیم، می‌تونیم توی پیدا کردن باگ‌های احتمالی، بهتر شدن تکمیل خودکار کد (autocomplete) توی IDEها و مستند‌سازی، از مزایاش استفاده کنیم.

ایده اصلی PEP 484 این بود که پایتون همچنان یک زبون داینامیک بمونه، ولی اگه کسی خواست، بتونه از تایپ‌ها برای بهبود کیفیت کد استفاده کنه. با این قابلیت، ابزارهایی مثل Mypy و Pyright می‌تونن تایپ‌ها رو بررسی کنن و خطاهای احتمالی رو قبل از اجرای کد پیدا کنن.

یکی از دلایل اصلی محبوب شدن تایپ‌ها تو پایتون PEP 484 بوده و باعث شده ابزارها و کتابخونه‌های زیادی ازش پشتیبانی کنن. حالا ده سال بعد از معرفی PEP 484، یه نظرسنجی بزرگ توسط JetBrains، Meta و مایکروسافت انجام شده تا ببینن وضعیت استفاده از تایپ‌ها تو پایتون چجوریه. بیش از ۱۰۰۰ نفر تو این نظرسنجی شرکت کردن و نتایج جالبی به دست اومده. خلاصه‌اش اینه:

نکات مهم:

  • تقریبا ۸۸٪ برنامه‌نویسا یا همیشه یا اغلب از تایپ‌ها استفاده می‌کنن.
  • مزایای اصلی، بهبود IDEها، داکیومنت‌ها و پیدا کردن باگ‌ها.
  • مشکلات اصلی هم دشواری توی استفاده برای الگوهای پیچیده، کندی ابزارها و نبود تایپ تو کتابخونه‌های محبوب.
  • تفاوت توی نحوه پیاده‌سازی تایپ چکرها و سختی پیدا کردن مستندات، کارو برای جونیورها سخت می‌کنه.

📌 کجاها از تایپ‌ها استفاده میشه؟
کوتاه: خیلی جاها 😁
کمی دقیقتر: از اسکریپت‌نویسی و توسعه وب گرفته تا دیتا ساینس، دیتا انجینیرینگ، هوش مصنوعی و... حتی برای پروژه‌های شخصی هم ۶۶٪ از تایپ‌ها استفاده می‌کنن.

⚙️ ابزارها و تایپ چکرها

  • محبوب‌ترین محیط توسعه VS Code بوده.
  • تو تایپ چکرها، Mypy اول و Pyright دومه.
  • جالبه که Pydantic هم کلی استفاده میشه (۶۲٪)، حتی برای چک‌های زمان اجرا.

😍 چیزایی که دولوپرا دوست دارن:

  • تکمیل خودکار (autocomplete) قوی‌تر.
  • شفاف‌تر شدن کد.
  • پیدا کردن باگ‌های احتمالی قبل از اجرا.
  • ریفکتور راحت‌تر.

😤 مشکلاتی که اذیت می‌کنه:

  • پیچیدگی تایپ‌ها برای چیزای داینامیک.
  • سرعت پایین ابزارهایی مثل Mypy.
  • نبود تایپ تو بعضی از کتابخونه‌ها.
  • مستندات ناکافی، مخصوصاً برای موارد پیشرفته.

🧐 چرا بعضیا تایپ استفاده نمی‌کنن؟

  • ۲۹٪ گفتن نیازی به تایپ تو پروژه‌هاشون ندارن. جالب اینکه حتی بین این افراد، ۶۰٪ تایپ رو "همیشه" یا "اغلب" استفاده می‌کنن.

✍️ پیشنهادها برای بهبود:

  • استانداردسازی بهتر ابزارها.
  • پشتیبانی قوی‌تر برای الگوهای پیچیده و داینامیک.
  • بهبود مستندات، مخصوصاً برای تایپ‌های پیشرفته با مثال‌های عملی.
  • افزایش سرعت تایپ چکرها.

🔄 این نظرسنجی قراره سال ۲۰۲۵ دوباره انجام بشه تا ببینن وضعیت تغییر کرده یا نه.

🔗 لینک نتایج نظرسنجی از بلاگ مهندسی شرکت متا

نظر شما چیه؟ از تایپ‌ها استفاده می‌کنی یا ترجیحت پایتون‌نویسی به شیوه مردان شجاع و فارغ از تایپه؟ 😅

This summer, JetBrains, Meta, and Microsoft collaborated to conduct a comprehensive survey on the state of Python typing*. The survey aimed to understand how developers in the open source community are using type hints, the challenges they face, and the tools they rely on. Over 1,000 people took the survey and we are delighted to share the findings. Despite the positive typing sentiment, we received fantastic (even if a little biting at times) feedback about the type system. We’ll give a summary of the findings including usage statistics, overall sentiment and takeaways that can improve Python developer tooling.

Overall findings

  • 88% of respondents “Always” or “Often” use Types in their Python code.
  • IDE tooling, documentation, and catching bugs are drivers for the high adoption of types in survey responses,
  • The usability of types and ability to express complex patterns still are challenges that leave some code unchecked.
  • Latency in tooling and lack of types in popular libraries are limiting the effectiveness of type checkers.
  • Inconsistency in type check implementations and poor discoverability of the documentation create friction in onboarding types into a project and seeking help when using the tools.

Survey methodology

A survey about types is likely to attract a lot of typing enthusiasts, so we don’t take this to be an unbiased nor representative view of everyone in the community. We did our best to distribute to as many developers as possible and aimed for easy-to-understand questions for all skill levels. We created questions that would give a picture of developer profiles, tools, and overall sentiment towards typed Python. Beyond metrics, we wanted to get a sense of the current mood and are thankful for the detailed and candid feedback.

Developer cohorts

As a general purpose language, it was not surprising to see Python types used across many fields. Scripting/automation, web development, data analysis, AI/ML, devOps and teaching all had large representation. One surprising finding was the value Python types are demonstrating outside of collaborative environments. A significant portion of respondents use Python types in personal projects (66% of respondents who only use Python personally “Always” or “Often” use types, compared to 78% of only “Professional” developers) and without CI (29.6% respondents don’t have type checking in CI use types “Always” or “Often”).

167_article_photo_2.jpg

IDEs and type checkers

← بازگشت به همه پست‌ها