ty vs mypy vs Pyright: ควรใช้ตัวตรวจสอบ Type ตัวไหนสำหรับ Python?

ty 1.0 พร้อมใช้งานระดับ production แล้ว เรารัน ty, mypy และ Pyright บน API client ตัวเดียวกัน เปรียบเทียบความเร็ว ฟีเจอร์ และการย้ายมาใช้งาน พร้อมชี้ให้เห็นช่องว่างที่ไม่มีตัวไหนปิดได้: นั่นคือ JSON ที่ API ของคุณคืนค่ามา

Ashley Innocent

Ashley Innocent

4 December 2026

ty vs mypy vs Pyright: ควรใช้ตัวตรวจสอบ Type ตัวไหนสำหรับ Python?

Apidog สำหรับองค์กร

การติดตั้งแบบ On-Premises

SSO & RBAC

รองรับมาตรฐาน SOC 2

สำรวจ Apidog Enterprise

ty vs mypy สรุปได้ว่าอยู่ที่ความเร็วและความเป็นผู้ใหญ่ของเครื่องมือ ty คือ type checker และ language server ภาษา Rust จาก Astral ซึ่งพร้อมใช้งาน production ตั้งแต่เปิดตัวเวอร์ชัน 1.0 เมื่อวันที่ 1 ธันวาคม 2026 ส่วน mypy เป็นตัว checker มาตรฐานอ้างอิงที่มีระบบ plugin ที่ลึกและหลากหลายที่สุด และ Pyright คือตัว checker ที่รวดเร็วของ Microsoft ซึ่งทำงานอยู่เบื้องหลัง Pylance ใน VS Code เลือก ty ถ้าต้องการความเร็วในโปรเจกต์ใหม่ เลือก mypy ถ้าโค้ดเบสของคุณพึ่งพา plugin เยอะ และเลือก Pyright ถ้าทีมของคุณทำงานอยู่ใน VS Code เป็นหลัก

ทั้งสามตัวจับบั๊กในชีวิตประจำวันได้เหมือนกันหมด แต่ไม่มีตัวไหนตรวจสอบ JSON ที่ API ของคุณส่งกลับมาจริง ๆ ได้ และสำหรับนักพัฒนา backend ช่องว่างตรงนี้สำคัญกว่าตัวเลข benchmark ใด ๆ

ty คืออะไร?

ty เป็น Python type checker และ language server แบบโอเพนซอร์สจาก Astral ทีมเบื้องหลัง uv และ Ruff ตัวนี้เขียนด้วย Rust ออกแบบมาบนรากฐานของ incremental checking เพื่อให้ editor อัปเดตได้รวดเร็ว และมาพร้อมทั้ง CLI (ty check) และ VS Code extension Astral ประกาศความพร้อมใช้งาน production กับ ty 1.0 เมื่อวันที่ 1 ธันวาคม 2026

คุณลองใช้ได้โดยไม่ต้องติดตั้งอะไรลงในโปรเจกต์เลย:

uvx ty check
# or install it as a tool
uv tool install ty@latest

ty 1.1.0 ตามมาบน PyPI ในวันที่ 3 ธันวาคม โดยประกาศ ty 1.0 ระบุว่าตอนนี้ ty เป็น type checker เริ่มต้นที่ OpenAI ซึ่งการย้ายมาใช้ช่วยให้เร็วขึ้นแบบ median 24 เท่า และใช้หน่วยความจำสูงสุดน้อยลง 73%

ty vs mypy vs Pyright แบบเห็นภาพรวมในตารางเดียว

ty mypy Pyright
ผู้ดูแล Astral ชุมชน Python (python/mypy) Microsoft
เขียนด้วย Rust Python (คอมไพล์ด้วย mypyc) TypeScript (รันบน Node.js)
เวอร์ชันล่าสุด (4 ธ.ค. 2026) 1.1.0 2.4.1 1.1.414
การติดตั้ง uv tool install ty pip install mypy npm i -g pyright หรือ pip install pyright
Language server มี ในตัวอยู่แล้ว ไม่มีอย่างเป็นทางการ (มี dmypy daemon เพื่อความเร็ว) มี และยังเป็นตัวขับเคลื่อน Pylance
ระบบ plugin ไม่มี แต่รองรับ Pydantic, Django, pytest, attrs ในตัว มี (Pydantic, django-stubs และอื่น ๆ) ไม่มี
Autofix ty check --fix ไม่มี ไม่มี (มีแค่ quick fixes ใน editor)
การนำมาใช้กับโค้ดเบสเดิม --add-ignore, --update-baseline ตั้งค่ารายโมดูล, # type: ignore กำหนด # pyright: basic หรือ strict รายไฟล์

ข้อมูลเวอร์ชันมาจาก PyPI และ npm ณ วันที่ 4 ธันวาคม 2026 ส่วนข้อมูลอื่น ๆ มาจากเอกสารของแต่ละโปรเจกต์: ty, mypy และ Pyright

ไฟล์เดียวกัน สาม type checker

นี่คือ API client ตัวเล็ก ๆ ที่มีบั๊กอยู่สองจุด คือพิมพ์ key ของ TypedDict ผิด และมี int + str โดยให้ทั้งสามตัวตรวจสอบไฟล์เดียวกันบนเครื่องเดียวกัน

from typing import TypedDict

import httpx


class User(TypedDict):
    id: int
    email: str


def get_user(base_url: str, user_id: int) -> User:
    resp = httpx.get(f"{base_url}/users/{user_id}", timeout=5)
    resp.raise_for_status()
    return resp.json()


def greeting(user: User) -> str:
    return "Hello " + user["emial"]


def total_users(users: list[User]) -> int:
    return len(users) + "0"

ty 1.1.0 (ty check client.py) จับบั๊กทั้งสองจุดได้ พร้อมเสนอวิธีแก้ typo:

error[invalid-typed-dict-key]: Unknown key "emial" for TypedDict `User`
  --> client.py:18:28
help: Replace with "email"
error[unsupported-operator]: Unsupported `+` operation
  --> client.py:22:12
Found 2 diagnostics

mypy 2.4.1 (mypy client.py) ก็จับได้เหมือนกันสองจุด พร้อมคำแนะนำเช่นกัน:

client.py:18: error: TypedDict "User" has no key "emial"  [typeddict-item]
client.py:18: note: Did you mean "email"?
client.py:22: error: Unsupported operand types for + ("int" and "str")  [operator]
Found 2 errors in 1 file (checked 1 source file)

Pyright 1.1.414 (pyright client.py) เห็นด้วย:

client.py:18:23 - error: Could not access item in TypedDict
    "emial" is not a defined key in "User" (reportGeneralTypeIssues)
client.py:22:12 - error: Operator "+" not supported for types "int" and "Literal['0']"
2 errors, 0 warnings, 0 informations

สังเกตสิ่งที่ทั้งสามตัวยอมผ่านในโหมด default: return resp.json() การเรียกนี้คืนค่าเป็น Any และ Any ผ่านได้ในตำแหน่งที่ต้องเป็น User มีแค่การตั้งค่าที่เข้มงวดขึ้นเท่านั้นที่จะจับจุดนี้ได้ mypy --strict จะรายงาน Returning Any from function declared to return "User" และ ty check --warn unsound-return จะรายงาน `Any` is not a subtype of `User` แต่แม้แต่อย่างนั้น checker ก็บอกได้แค่ว่าค่านี้ไม่ได้รับการตรวจสอบ มันบอกไม่ได้หรอกว่าเซิร์ฟเวอร์ส่ง email มาจริงหรือไม่

ความเร็วและความถูกต้อง: ตัวเลขบอกอะไร

Benchmark ของ Astral จัดให้ ty อยู่อันดับหนึ่งใน 16 จาก 18 โปรเจกต์โอเพนซอร์สสำหรับการตรวจสอบแบบ cold command-line โดยเร็วกว่า Pyrefly 1.46 เท่าและเร็วกว่า Zuban 3.56 เท่าตามค่าเฉลี่ยเรขาคณิต ในส่วนของ editor ty เป็น language server ที่เร็วที่สุดใน 14 จาก 17 โปรเจกต์ อย่างไรก็ตามนี่คือตัวเลขจาก Astral บนเครื่อง M3 Max ดังนั้นควรทดสอบกับ repo ของตัวเองก่อนนำตัวเลขเหล่านี้ไปอ้างอิงกับทีม

ในชุดทดสอบ conformance ของ Python typing ty 1.0 มี false positive 14 กรณีและ false negative 13 กรณี ลดลงจาก 224 และ 405 กรณีตอนยังเป็นเบต้า โพสต์ของ Astral ระบุว่า mypy มี false positive 190 กรณีและ false negative 109 กรณีในชุดทดสอบเดียวกัน แม้ conformance จะไม่ใช่ตัวชี้วัดที่สมบูรณ์แบบสำหรับโค้ดจริง แต่มันแสดงให้เห็นว่า ty ไม่ใช่ของเล่นอีกต่อไปแล้ว

ในการสำรวจของ Astral ในเดือนพฤศจิกายน ซึ่งดู package ที่ถูกดาวน์โหลดมากที่สุด 25,000 รายการบน PyPI พบว่า repository ที่มี type checker 11.6% ใช้ ty อยู่ในอันดับสามรองจาก mypy และ Pyright โดย FastAPI และ tox ใช้งานอยู่แล้ว

mypy vs Pyright vs ty: ควรใช้ตัวไหน?

โค้ดเบสของคุณเป็นตัวตัดสินมากกว่าตัวเลข benchmark เสียอีก

คุณไม่จำเป็นต้องเลือกครั้งเดียวตลอดไป การตั้งค่าที่พบบ่อยคือใช้ ty ใน editor เพื่อ feedback ทันที และคง checker ตัวเดิมใน CI ไว้จนกว่าผลลัพธ์ของ ty จะตรงกับสิ่งที่คุณเชื่อถือ ถ้าคุณรันบริการ FastAPI ควรใช้ทั้งสามตัวคู่กับการทดสอบจริง คู่มือunit testing FastAPI ของเราครอบคลุมเรื่องรันไทม์

สิ่งที่ไม่มี type checker ไหนมองเห็น: ขอบเขตของ API

type checker อ่านแค่ซอร์สโค้ดของคุณ มันไม่เคยเห็น HTTP response เลย เมื่อคุณกำกับว่า get_user() คืนค่าเป็น User คุณกำลังสัญญาเกี่ยวกับเซิร์ฟเวอร์ที่คุณควบคุมไม่ได้ และทั้ง ty, mypy และ Pyright ล้วนเชื่อคำสัญญานั้นโดยไม่ตั้งข้อสงสัย

Schema: ty, mypy and Pyright check your Python source and the TypedDict you wrote, but the JSON body the live API returns arrives at runtime; only a contract test against the API spec checks that response

ตรงนี้แหละที่บั๊กฝั่ง backend ซ่อนตัวจริง ๆ เซิร์ฟเวอร์เปลี่ยนชื่อ email เป็น email_address เปลี่ยน id ให้เป็น string หรือตัด field ออกในบาง code path โค้ดของคุณก็ยังผ่านการตรวจสอบ type อยู่ดี แต่กลับพังใน production ด้วย KeyError

มีสองนิสัยที่ช่วยปิดช่องว่างนี้:

  1. สร้าง TypedDict หรือโมเดล Pydantic จาก API spec แทนการเขียนด้วยมือ
  2. รัน contract test กับ endpoint จริง เพื่อให้ response ถูกตรวจสอบกับ spec ชุดเดียวกันตอนรันไทม์

ทั้งสองขั้นตอนนี้ถ้าทำด้วยมือจะน่าเบื่อมาก จึงเป็นงานที่เหมาะกับ coding agent อย่างยิ่ง

ให้ coding agent ของคุณตรวจสอบ contract ด้วย Apidog CLI

Apidog CLI เปิดให้ coding agent ของคุณ (Claude Code, Codex, OpenCode หรือ Cursor) เข้าถึงโปรเจกต์ Apidog ของคุณได้โดยตรง: endpoint, data schema, test case และ test run ทั้งหมดในรูปแบบ JSON ตัว agent จะอ่าน spec เขียน typed model ที่ตรงกับ spec รัน type checker ของคุณ แล้วรัน contract test คุณเขียนแค่ prompt เดียว

การตั้งค่า (ครั้งเดียวต่อเครื่อง):

npm install -g apidog-cli@latest
apidog auth login
apidog skill install   # teaches your agent the apidog commands

คู่มือการติดตั้งอธิบายเรื่อง token และ CI ถ้าคุณใช้ Claude Code ดูได้ที่Apidog CLI ใน Claude Code

Prompt:

Read the data schemas in Apidog project <projectId> with the apidog CLI.
Generate a TypedDict for each one in models.py and type the /objects
client against them. Run `ty check --warn unsound-return` and fix every
diagnostic. Then run the /objects test cases against environment <envId>
and tell me any field where the live response disagrees with the schema.

สิ่งที่ agent จะรัน:

apidog schema list --project <projectId>
apidog schema get <schemaId> --project <projectId>
ty check --warn unsound-return
apidog run --test-case <testCaseId> --project <projectId> -e <envId> -r cli,json
Apidog CLI listing the data schemas of a sandbox project and printing the ApiObject schema the agent turns into a TypedDict, before it runs the contract test

schema ที่ agent อ่านจะกลายเป็น TypedDict ที่ type checker ของคุณบังคับใช้ และ apidog run จะตรวจสอบ response จริงกับ schema ชุดเดียวกันนี้ เพิ่ม --upload-report เพื่อดูผลการรันในแอป Apidog หรือนำคำสั่งเดียวกันไปผูกเข้ากับ CI ด้วยข้อมูลอ้างอิง apidog run

Sequence: the coding agent reads the schema from Apidog CLI, writes models.py, runs ty check until it passes, runs the Apidog test cases against the live API, and reports any field that disagrees with the schema

เมื่อ agent ตัวเดียวทำสิ่งนี้ได้อย่างน่าเชื่อถือ ทีมก็มอบงานรันเหล่านี้ให้เป็น task ได้ Sharkly เป็นบอร์ดที่ให้คุณมอบงานแบบนี้ให้ coding agent ที่รันบนเครื่องของคุณเองด้วยแผน Claude Code หรือ Codex ของคุณเอง รันหลายตัวขนานกันได้ และตรวจทานงานที่ agent ส่งมอบก่อนที่จะ merge เริ่มใช้ได้ฟรี

วิธีย้ายจาก mypy หรือ Pyright มาใช้ ty

ty 1.0 เพิ่มสองฟีเจอร์ที่ทำให้การย้ายมาปลอดภัยบนโค้ดเบสจริง

  1. ติดตั้งและรันคู่ไปกับ checker ตัวปัจจุบันของคุณ uvx ty check ให้จำนวน error เบื้องต้นแก่คุณโดยไม่ต้องแตะ CI เลย
  2. ชี้ให้มันรู้จัก environment ของคุณ ส่ง --python .venv (หรือให้ uv ค้นหาเอง) เพื่อให้การ import ของ third-party resolve ได้
  3. ปิดเสียง error เดิมที่ค้างอยู่ รัน ty check --add-ignore เพื่อเพิ่มคอมเมนต์ suppression ในครั้งเดียว หรือ ty check --update-baseline เพื่อบันทึก diagnostics ที่มีอยู่ลงใน ty-baseline.json คอมมิต baseline ไว้เพื่อให้ CI แจ้งเตือนเฉพาะ error ใหม่
  4. เปิดกฎที่คุณเคยใช้ ถ้าคุณเคยรัน mypy --strict ให้เริ่มด้วย --warn unsound-return ใช้ --error <RULE> เพื่อให้กฎใดกฎหนึ่งทำให้ build ล้มเหลวได้
  5. ใช้ safe fix ty check --fix ลบสิ่งต่าง ๆ เช่น cast ที่ซ้ำซ้อนให้อัตโนมัติ
  6. สลับขั้นตอน CI เป็นอย่างสุดท้าย สำหรับ GitLab --output-format gitlab สร้าง Code Quality JSON ออกมา

คง mypy ไว้ใน CI สำหรับโมดูลใด ๆ ที่พึ่งพา mypy plugin จนกว่าคุณจะยืนยันได้ว่าการรองรับในตัวของ ty ครอบคลุมแล้ว

คำถามที่พบบ่อย

ty พร้อมใช้งาน production แล้วหรือยัง? พร้อมแล้ว ตามข้อมูลของ Astral ty 1.0 เปิดตัวเมื่อวันที่ 1 ธันวาคม 2026 พร้อมคำแนะนำให้ใช้ใน production และตอนนี้เป็น checker เริ่มต้นที่ OpenAI ควรรันคู่ไปกับ checker ตัวปัจจุบันของคุณสักหนึ่ง sprint ก่อนสลับ CI มาใช้

ty เร็วกว่า mypy จริงไหม? ตาม benchmark ของ Astral ใช่ และเร็วกว่ามาก โดย OpenAI รายงานว่าหลังย้ายมาใช้เร็วขึ้นแบบ median 24 เท่า อย่างไรก็ตามผลลัพธ์ขึ้นอยู่กับโค้ดเบสและ cache ของคุณ จึงควรจับเวลา ty check กับ repo ของตัวเองดู

ty จะแทนที่ Pyright และ Pylance ใน VS Code ได้ไหม? ทำได้ ty มาพร้อม language server ของตัวเองและ VS Code extension คุณจึงใช้มันแทน Pylance สำหรับ completions, navigation และ diagnostics ได้ แต่ควรปิด language server ตัวใดตัวหนึ่งเพื่อไม่ให้เห็น error ซ้ำซ้อน

ty รองรับ Pydantic และ Django ไหม? รองรับ ty 1.0 มีการรองรับ Pydantic, Django, pytest และ attrs ในตัว โดยไม่ต้องตั้งค่า plugin อย่างที่ mypy ต้องทำ

type checker ตรวจสอบ API response ได้ไหม? ไม่ได้ ty, mypy และ Pyright ตรวจสอบได้แค่ซอร์สโค้ด ควร validate response ตอนรันไทม์ด้วย Pydantic และตรวจสอบ API contract ด้วยการทดสอบ เช่น ให้ coding agent รัน Apidog test case ด้วย apidog run

ฝึกการออกแบบ API แบบ Design-first ใน Apidog

ค้นพบวิธีที่ง่ายขึ้นในการสร้างและใช้ API