From 69f918203359f834e0ca76764a502f3421b2c2c7 Mon Sep 17 00:00:00 2001 From: David Peter Date: Fri, 10 Oct 2025 12:05:03 +0200 Subject: [PATCH] [ty] Annotations are deferred by default for 3.14+ (#20799) ## Summary Type annotations are deferred by default starting with Python 3.14. No `from __future__ import annotations` import is necessary. ## Test Plan New Markdown test --- .../resources/mdtest/assignment/annotations.md | 16 ++++++++++++++++ .../src/types/infer/builder.rs | 7 +++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/crates/ty_python_semantic/resources/mdtest/assignment/annotations.md b/crates/ty_python_semantic/resources/mdtest/assignment/annotations.md index 8d44cd7195..5caefda4e1 100644 --- a/crates/ty_python_semantic/resources/mdtest/assignment/annotations.md +++ b/crates/ty_python_semantic/resources/mdtest/assignment/annotations.md @@ -308,6 +308,22 @@ x = Foo() reveal_type(x) # revealed: Foo ``` +## Annotations are deferred by default in Python 3.14 and later + +```toml +[environment] +python-version = "3.14" +``` + +```py +x: Foo + +class Foo: ... + +x = Foo() +reveal_type(x) # revealed: Foo +``` + ## Annotated assignments in stub files are inferred correctly ```pyi diff --git a/crates/ty_python_semantic/src/types/infer/builder.rs b/crates/ty_python_semantic/src/types/infer/builder.rs index 2ab009025e..c6b817c789 100644 --- a/crates/ty_python_semantic/src/types/infer/builder.rs +++ b/crates/ty_python_semantic/src/types/infer/builder.rs @@ -374,9 +374,12 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> { } /// Are we currently inferring types in file with deferred types? - /// This is true for stub files and files with `__future__.annotations` + /// This is true for stub files, for files with `__future__.annotations`, and + /// by default for all source files in Python 3.14 and later. fn defer_annotations(&self) -> bool { - self.index.has_future_annotations() || self.in_stub() + self.index.has_future_annotations() + || self.in_stub() + || Program::get(self.db()).python_version(self.db()) >= PythonVersion::PY314 } /// Are we currently in a context where name resolution should be deferred