π The fast, Pythonic way to build MCP servers and clients.
switch from `pyright` to `ty` (#1545)
nate nowack77b425c47d29
diff --git a/.github/workflows/marvin.yml b/.github/workflows/marvin.ymlindex 972c5cc24..55141f071 100644--- a/.github/workflows/marvin.yml+++ b/.github/workflows/marvin.yml@@ -70,6 +70,6 @@ jobs:mode: tagtrigger_phrase: "/marvin"allowed_bots: "*"- allowed_tools: "WebSearch,WebFetch,Bash(uv:*),Bash(pre-commit:*),Bash(pytest:*),Bash(ruff:*),Bash(pyright:*),Bash(git:*),Bash(gh:*),mcp__github__add_issue_comment,mcp__github__create_issue,mcp__github__get_issue,mcp__github__list_issues,mcp__github__search_issues,mcp__github__update_issue,mcp__github__update_issue_comment,mcp__github__create_pull_request,mcp__github__get_pull_request,mcp__github__get_pull_request_comments,mcp__github__get_pull_request_files,mcp__github__get_pull_request_reviews,mcp__github__get_pull_request_status,mcp__github__list_pull_requests,mcp__github__update_pull_request,mcp__github__update_pull_request_branch,mcp__github__update_pull_request_comment,mcp__github__merge_pull_request"+ allowed_tools: "WebSearch,WebFetch,Bash(uv:*),Bash(pre-commit:*),Bash(pytest:*),Bash(ruff:*),Bash(ty:*),Bash(git:*),Bash(gh:*),mcp__github__add_issue_comment,mcp__github__create_issue,mcp__github__get_issue,mcp__github__list_issues,mcp__github__search_issues,mcp__github__update_issue,mcp__github__update_issue_comment,mcp__github__create_pull_request,mcp__github__get_pull_request,mcp__github__get_pull_request_comments,mcp__github__get_pull_request_files,mcp__github__get_pull_request_reviews,mcp__github__get_pull_request_status,mcp__github__list_pull_requests,mcp__github__update_pull_request,mcp__github__update_pull_request_branch,mcp__github__update_pull_request_comment,mcp__github__merge_pull_request"additional_permissions: |actions: readdiff --git a/.github/workflows/run-static.yml b/.github/workflows/run-static.ymlindex e46a75cdb..0be52ff60 100644--- a/.github/workflows/run-static.yml+++ b/.github/workflows/run-static.yml@@ -36,12 +36,9 @@ jobs:with:enable-cache: truecache-dependency-glob: "uv.lock"- - name: Set up Python- uses: actions/setup-python@v5- with:- python-version: "3.12"+- name: Install dependencies- run: uv sync --dev+ run: uv sync- name: Check lockfile is up to daterun: |if ! uv lock --check; then@@ -51,6 +48,6 @@ jobs:fiecho "β Lockfile is up to date"- name: Run pre-commit- uses: pre-commit/[email protected]+ run: uv run pre-commit run --all-filesenv:SKIP: no-commit-to-branchdiff --git a/.pre-commit-config.yaml b/.pre-commit-config.yamlindex bca0329bc..0af2b47eb 100644--- a/.pre-commit-config.yaml+++ b/.pre-commit-config.yaml@@ -22,11 +22,16 @@ repos:# Run the formatter.- id: ruff-format- - repo: https://github.com/northisup/pyright-pretty- rev: v0.1.0+ - repo: localhooks:- - id: pyright-pretty+ - id: ty+ name: type check+ entry: ty check+ language: system+ types: [python]files: ^src/|^tests/+ pass_filenames: false+ require_serial: true- repo: https://github.com/pre-commit/pre-commit-hooksrev: v4.3.0diff --git a/AGENTS.md b/AGENTS.mdindex e6bbb48fc..6f3ebbad4 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -10,7 +10,7 @@ FastMCP is a comprehensive Python framework (Python β₯3.10) for building Model```bashuv sync # Install dependencies-uv run pre-commit run --all-files # Ruff + Prettier + Pyright+uv run pre-commit run --all-files # Ruff + Prettier + tyuv run pytest # Run full test suite```@@ -222,7 +222,7 @@ uv sync # Installs all deps including dev tools### Validation Commands (Run Frequently)- **Linting**: `uv run ruff check` (or with `--fix`)-- **Type Checking**: `uv run pyright`+- **Type Checking**: `uv run ty check`- **All Checks**: `uv run pre-commit run --all-files`### Testing@@ -247,5 +247,5 @@ uv sync # Installs all deps including dev tools1. **Dependencies**: Always `uv sync` first2. **Pre-commit fails**: Run `uv run pre-commit run --all-files` to see failures-3. **Type errors**: Use `uv run pyright` directly, check `pyproject.toml` config+3. **Type errors**: Use `uv run ty check` directly, check `pyproject.toml` config4. **Test timeouts**: Default 3s - optimize or mark as integration testsdiff --git a/justfile b/justfileindex cfb9f1a3e..74b845c6f 100644--- a/justfile+++ b/justfile@@ -6,9 +6,9 @@ build:test: builduv run --frozen pytest -xvs tests-# Run pyright on all files+# Run ty type checker on all filestypecheck:- uv run --frozen pyright+ uv run --frozen ty check# Serve documentation locallydocs:diff --git a/pyproject.toml b/pyproject.tomlindex 23d979528..90dbaad02 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -50,7 +50,7 @@ dev = ["psutil","pyinstrument>=5.0.2","pyperclip>=1.9.0",- "pyright>=1.1.389",+ "ty>=0.0.1a19","pytest>=8.3.3","pytest-asyncio>=0.23.5","pytest-cov>=6.1.1",@@ -114,18 +114,23 @@ python_files = ["test_*.py", "*_test.py"]python_classes = ["Test*"]python_functions = ["test_*"]-[tool.pyright]+[tool.ty.src]include = ["src", "tests"]exclude = ["**/node_modules", "**/__pycache__", ".venv", ".git", "dist"]-pythonVersion = "3.10"-pythonPlatform = "Darwin"-typeCheckingMode = "basic"-reportMissingImports = true-reportMissingTypeStubs = false-useLibraryCodeForTypes = true-venvPath = "."-venv = ".venv"-strict = ["src/fastmcp/server/server.py"]++[tool.ty.environment]+python-version = "3.10"++[tool.ty.rules]+# Rules with too many errors to fix right now (40+ each)+invalid-argument-type = "ignore" # 40 errors+no-matching-overload = "ignore" # 126 errors+unknown-argument = "ignore" # 61 errors+unresolved-attribute = "ignore" # 60 errors++# Rules with moderate errors that need more investigation+call-non-callable = "ignore" # 7 errors+missing-argument = "ignore" # 23 errors[tool.ruff.lint]extend-select = ["I", "UP"]diff --git a/src/fastmcp/cli/run.py b/src/fastmcp/cli/run.pyindex 53a2120ea..a78f8193e 100644--- a/src/fastmcp/cli/run.py+++ b/src/fastmcp/cli/run.py@@ -107,7 +107,7 @@ async def import_server(file: Path, server_or_factory: str | None = None) -> Anysys.exit(1)# Handle module:object syntax- if ":" in server_or_factory:+ if server_or_factory and ":" in server_or_factory:module_name, object_name = server_or_factory.split(":", 1)try:server_module = importlib.import_module(module_name)diff --git a/src/fastmcp/experimental/utilities/openapi/parser.py b/src/fastmcp/experimental/utilities/openapi/parser.pyindex 285b62309..799601a7e 100644--- a/src/fastmcp/experimental/utilities/openapi/parser.py+++ b/src/fastmcp/experimental/utilities/openapi/parser.py@@ -549,7 +549,11 @@ class OpenAPIParser(return# Add this schema and recursively find its dependencies- if schema_name not in collected and schema_name in all_schemas:+ if (+ collected is not None+ and schema_name not in collected+ and schema_name in all_schemas+ ):collected.add(schema_name)# Recursively find dependencies of this schemafind_refs(all_schemas[schema_name])diff --git a/src/fastmcp/server/context.py b/src/fastmcp/server/context.pyindex 7d7c2f48e..165997571 100644--- a/src/fastmcp/server/context.py+++ b/src/fastmcp/server/context.py@@ -44,7 +44,7 @@ from fastmcp.utilities.types import get_cached_typeadapterlogger = get_logger(__name__)T = TypeVar("T")-_current_context: ContextVar[Context | None] = ContextVar("context", default=None)+_current_context: ContextVar[Context | None] = ContextVar("context", default=None) # type: ignore[assignment]_flush_lock = asyncio.Lock()diff --git a/src/fastmcp/server/http.py b/src/fastmcp/server/http.pyindex ad779b157..549a4642e 100644--- a/src/fastmcp/server/http.py+++ b/src/fastmcp/server/http.py@@ -64,7 +64,7 @@ class StreamableHTTPASGIApp:raise-_current_http_request: ContextVar[Request | None] = ContextVar(+_current_http_request: ContextVar[Request | None] = ContextVar( # type: ignore[assignment]"http_request",default=None,)diff --git a/src/fastmcp/tools/tool_transform.py b/src/fastmcp/tools/tool_transform.pyindex e4f867ab2..d492432ad 100644--- a/src/fastmcp/tools/tool_transform.py+++ b/src/fastmcp/tools/tool_transform.py@@ -29,7 +29,7 @@ logger = get_logger(__name__)# Context variable to store current transformed tool-_current_tool: ContextVar[TransformedTool | None] = ContextVar(+_current_tool: ContextVar[TransformedTool | None] = ContextVar( # type: ignore[assignment]"_current_tool", default=None)diff --git a/tests/client/test_elicitation.py b/tests/client/test_elicitation.pyindex d65b4a6bf..51b6b23db 100644--- a/tests/client/test_elicitation.py+++ b/tests/client/test_elicitation.py@@ -547,6 +547,8 @@ class TestPatternMatching:return "You declined"case CancelledElicitation():return "Cancelled"+ case _:+ return "Unknown result"async def elicitation_handler(message, response_type, params, ctx):return ElicitResult(action="accept", content={"value": "Alice"})@@ -570,6 +572,8 @@ class TestPatternMatching:return "You declined"case CancelledElicitation():return "Cancelled"+ case _:+ return "Unknown result"async def elicitation_handler(message, response_type, params, ctx):return ElicitResult(action="decline")@@ -593,6 +597,8 @@ class TestPatternMatching:return "You declined"case CancelledElicitation():return "Cancelled"+ case _:+ return "Unknown result"async def elicitation_handler(message, response_type, params, ctx):return ElicitResult(action="cancel")diff --git a/tests/integration_tests/auth/test_github_provider_integration.py b/tests/integration_tests/auth/test_github_provider_integration.pyindex ad49d5e7c..909900119 100644--- a/tests/integration_tests/auth/test_github_provider_integration.py+++ b/tests/integration_tests/auth/test_github_provider_integration.py@@ -116,7 +116,7 @@ def create_github_server_with_mock_callback(separator = "&" if "?" in str(params.redirect_uri) else "?"return f"{params.redirect_uri}{separator}{urlencode(callback_params)}"- auth.authorize = mock_authorize+ auth.authorize = mock_authorize # type: ignore[assignment]# Mock the token verifier to accept our fake tokensoriginal_verify_token = auth._token_validator.verify_token@@ -137,7 +137,7 @@ def create_github_server_with_mock_callback(# Fall back to original verification for other tokensreturn await original_verify_token(token)- auth._token_validator.verify_token = mock_verify_token+ auth._token_validator.verify_token = mock_verify_token # type: ignore[assignment]# Create FastMCP server with mocked GitHub authenticationserver = FastMCP("GitHub OAuth Integration Test Server (Mock)", auth=auth)diff --git a/uv.lock b/uv.lockindex 482bcf5c8..ee126d08c 100644--- a/uv.lock+++ b/uv.lock@@ -558,7 +558,6 @@ dev = [{ name = "psutil" },{ name = "pyinstrument" },{ name = "pyperclip" },- { name = "pyright" },{ name = "pytest" },{ name = "pytest-asyncio" },{ name = "pytest-cov" },@@ -569,6 +568,7 @@ dev = [{ name = "pytest-timeout" },{ name = "pytest-xdist" },{ name = "ruff" },+ { name = "ty" },][package.metadata]@@ -599,7 +599,6 @@ dev = [{ name = "psutil" },{ name = "pyinstrument", specifier = ">=5.0.2" },{ name = "pyperclip", specifier = ">=1.9.0" },- { name = "pyright", specifier = ">=1.1.389" },{ name = "pytest", specifier = ">=8.3.3" },{ name = "pytest-asyncio", specifier = ">=0.23.5" },{ name = "pytest-cov", specifier = ">=6.1.1" },@@ -610,6 +609,7 @@ dev = [{ name = "pytest-timeout", specifier = ">=2.4.0" },{ name = "pytest-xdist", specifier = ">=3.6.1" },{ name = "ruff" },+ { name = "ty", specifier = ">=0.0.1a19" },][[package]]@@ -1422,19 +1422,6 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/bd/a5/ce97a778f096aaa27cfcb7ad09f1198cf73277dcab6c68a4b8f332d91e48/pyrepl-0.11.4-py3-none-any.whl", hash = "sha256:ac30d6340267a21c39e1b1934f92bca6b8735017d14b17e40f903b2d1563541d", size = 55596, upload-time = "2025-07-17T22:56:24.537Z" },]-[[package]]-name = "pyright"-version = "1.1.403"-source = { registry = "https://pypi.org/simple" }-dependencies = [- { name = "nodeenv" },- { name = "typing-extensions" },-]-sdist = { url = "https://files.pythonhosted.org/packages/fe/f6/35f885264ff08c960b23d1542038d8da86971c5d8c955cfab195a4f672d7/pyright-1.1.403.tar.gz", hash = "sha256:3ab69b9f41c67fb5bbb4d7a36243256f0d549ed3608678d381d5f51863921104", size = 3913526, upload-time = "2025-07-09T07:15:52.882Z" }-wheels = [- { url = "https://files.pythonhosted.org/packages/49/b6/b04e5c2f41a5ccad74a1a4759da41adb20b4bc9d59a5e08d29ba60084d07/pyright-1.1.403-py3-none-any.whl", hash = "sha256:c0eeca5aa76cbef3fcc271259bbd785753c7ad7bcac99a9162b4c4c7daed23b3", size = 5684504, upload-time = "2025-07-09T07:15:50.958Z" },-]-[[package]]name = "pytest"version = "8.4.1"@@ -2101,6 +2088,31 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/00/c0/8f5d070730d7836adc9c9b6408dec68c6ced86b304a9b26a14df072a6e8c/traitlets-5.14.3-py3-none-any.whl", hash = "sha256:b74e89e397b1ed28cc831db7aea759ba6640cb3de13090ca145426688ff1ac4f", size = 85359, upload-time = "2024-04-19T11:11:46.763Z" },]+[[package]]+name = "ty"+version = "0.0.1a19"+source = { registry = "https://pypi.org/simple" }+sdist = { url = "https://files.pythonhosted.org/packages/c0/04/281c1a3c9c53dae5826b9d01a3412de653e3caf1ca50ce1265da66e06d73/ty-0.0.1a19.tar.gz", hash = "sha256:894f6a13a43989c8ef891ae079b3b60a0c0eae00244abbfbbe498a3840a235ac", size = 4098412, upload-time = "2025-08-19T13:29:58.559Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/3e/65/a61cfcc7248b0257a3110bf98d3d910a4729c1063abdbfdcd1cad9012323/ty-0.0.1a19-py3-none-linux_armv6l.whl", hash = "sha256:e0e7762f040f4bab1b37c57cb1b43cc3bc5afb703fa5d916dfcafa2ef885190e", size = 8143744, upload-time = "2025-08-19T13:29:13.88Z" },+ { url = "https://files.pythonhosted.org/packages/02/d9/232afef97d9afa2274d23a4c49a3ad690282ca9696e1b6bbb6e4e9a1b072/ty-0.0.1a19-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:cd0a67ac875f49f34d9a0b42dcabf4724194558a5dd36867209d5695c67768f7", size = 8305799, upload-time = "2025-08-19T13:29:17.322Z" },+ { url = "https://files.pythonhosted.org/packages/20/14/099d268da7a9cccc6ba38dfc124f6742a1d669bc91f2c61a3465672b4f71/ty-0.0.1a19-py3-none-macosx_11_0_arm64.whl", hash = "sha256:ff8b1c0b85137333c39eccd96c42603af8ba7234d6e2ed0877f66a4a26750dd4", size = 7901431, upload-time = "2025-08-19T13:29:21.635Z" },+ { url = "https://files.pythonhosted.org/packages/c2/cd/3f1ca6e1d7f77cc4d08910a3fc4826313c031c0aae72286ae859e737670c/ty-0.0.1a19-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4fef34a29f4b97d78aa30e60adbbb12137cf52b8b2b0f1a408dd0feb0466908a", size = 8051501, upload-time = "2025-08-19T13:29:23.741Z" },+ { url = "https://files.pythonhosted.org/packages/47/72/ddbec39f48ce3f5f6a3fa1f905c8fff2873e59d2030f738814032bd783e3/ty-0.0.1a19-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:b0f219cb43c0c50fc1091f8ebd5548d3ef31ee57866517b9521d5174978af9fd", size = 7981234, upload-time = "2025-08-19T13:29:25.839Z" },+ { url = "https://files.pythonhosted.org/packages/f2/0f/58e76b8d4634df066c790d362e8e73b25852279cd6f817f099b42a555a66/ty-0.0.1a19-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:22abb6c1f14c65c1a2fafd38e25dd3c87994b3ab88cb0b323235b51dbad082d9", size = 8916394, upload-time = "2025-08-19T13:29:27.932Z" },+ { url = "https://files.pythonhosted.org/packages/70/30/01bfd93ccde11540b503e2539e55f6a1fc6e12433a229191e248946eb753/ty-0.0.1a19-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:5b49225c349a3866e38dd297cb023a92d084aec0e895ed30ca124704bff600e6", size = 9412024, upload-time = "2025-08-19T13:29:30.942Z" },+ { url = "https://files.pythonhosted.org/packages/a8/a2/2216d752f5f22c5c0995f9b13f18337301220f2a7d952c972b33e6a63583/ty-0.0.1a19-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:88f41728b3b07402e0861e3c34412ca963268e55f6ab1690208f25d37cb9d63c", size = 9032657, upload-time = "2025-08-19T13:29:33.933Z" },+ { url = "https://files.pythonhosted.org/packages/24/c7/e6650b0569be1b69a03869503d07420c9fb3e90c9109b09726c44366ce63/ty-0.0.1a19-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:33814a1197ec3e930fcfba6fb80969fe7353957087b42b88059f27a173f7510b", size = 8812775, upload-time = "2025-08-19T13:29:36.505Z" },+ { url = "https://files.pythonhosted.org/packages/35/c6/b8a20e06b97fe8203059d56d8f91cec4f9633e7ba65f413d80f16aa0be04/ty-0.0.1a19-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d71b7f2b674a287258f628acafeecd87691b169522945ff6192cd8a69af15857", size = 8631417, upload-time = "2025-08-19T13:29:38.837Z" },+ { url = "https://files.pythonhosted.org/packages/be/99/821ca1581dcf3d58ffb7bbe1cde7e1644dbdf53db34603a16a459a0b302c/ty-0.0.1a19-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:3a7f8ef9ac4c38e8651c18c7380649c5a3fa9adb1a6012c721c11f4bbdc0ce24", size = 7928900, upload-time = "2025-08-19T13:29:41.08Z" },+ { url = "https://files.pythonhosted.org/packages/08/cb/59f74a0522e57565fef99e2287b2bc803ee47ff7dac250af26960636939f/ty-0.0.1a19-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:60f40e72f0fbf4e54aa83d9a6cb1959f551f83de73af96abbb94711c1546bd60", size = 8003310, upload-time = "2025-08-19T13:29:43.165Z" },+ { url = "https://files.pythonhosted.org/packages/4c/b3/1209b9acb5af00a2755114042e48fb0f71decc20d9d77a987bf5b3d1a102/ty-0.0.1a19-py3-none-musllinux_1_2_i686.whl", hash = "sha256:64971e4d3e3f83dc79deb606cc438255146cab1ab74f783f7507f49f9346d89d", size = 8496463, upload-time = "2025-08-19T13:29:46.136Z" },+ { url = "https://files.pythonhosted.org/packages/a2/d6/a4b6ba552d347a08196d83a4d60cb23460404a053dd3596e23a922bce544/ty-0.0.1a19-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:9aadbff487e2e1486e83543b4f4c2165557f17432369f419be9ba48dc47625ca", size = 8700633, upload-time = "2025-08-19T13:29:49.351Z" },+ { url = "https://files.pythonhosted.org/packages/96/c5/258f318d68b95685c8d98fb654a38882c9d01ce5d9426bed06124f690f04/ty-0.0.1a19-py3-none-win32.whl", hash = "sha256:00b75b446357ee22bcdeb837cb019dc3bc1dc5e5013ff0f46a22dfe6ce498fe2", size = 7811441, upload-time = "2025-08-19T13:29:52.077Z" },+ { url = "https://files.pythonhosted.org/packages/fb/bb/039227eee3c0c0cddc25f45031eea0f7f10440713f12d333f2f29cf8e934/ty-0.0.1a19-py3-none-win_amd64.whl", hash = "sha256:aaef76b2f44f6379c47adfe58286f0c56041cb2e374fd8462ae8368788634469", size = 8441186, upload-time = "2025-08-19T13:29:54.53Z" },+ { url = "https://files.pythonhosted.org/packages/74/5f/bceb29009670ae6f759340f9cb434121bc5ed84ad0f07bdc6179eaaa3204/ty-0.0.1a19-py3-none-win_arm64.whl", hash = "sha256:893755bb35f30653deb28865707e3b16907375c830546def2741f6ff9a764710", size = 8000810, upload-time = "2025-08-19T13:29:56.796Z" },+]+[[package]]name = "typer"version = "0.16.0"