[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
This commit is contained in:
David Peter 2025-10-10 12:05:03 +02:00 committed by GitHub
parent 949a4f1c42
commit 69f9182033
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 2 deletions

View File

@ -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

View File

@ -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