ruff/crates/ruff_python_ast/src
Dylan a95c73d5d0
Implement deferred annotations for Python 3.14 (#17658)
This PR updates the semantic model for Python 3.14 by essentially
equating "run using Python 3.14" with "uses `from __future__ import
annotations`".

While this is not technically correct under the hood, it appears to be
correct for the purposes of our semantic model. That is: from the point
of view of deciding when to parse, bind, etc. annotations, these two
contexts behave the same. More generally these contexts behave the same
unless you are performing some kind of introspection like the following:


Without future import:
```pycon
>>> from annotationlib import get_annotations,Format
>>> def foo()->Bar:...
...
>>> get_annotations(foo,format=Format.FORWARDREF)
{'return': ForwardRef('Bar')}
>>> get_annotations(foo,format=Format.STRING)
{'return': 'Bar'}
>>> get_annotations(foo,format=Format.VALUE)
Traceback (most recent call last):
[...]
NameError: name 'Bar' is not defined
>>> get_annotations(foo)
Traceback (most recent call last):
[...]
NameError: name 'Bar' is not defined
```

With future import:
```
>>> from __future__ import annotations
>>> from annotationlib import get_annotations,Format
>>> def foo()->Bar:...
...
>>> get_annotations(foo,format=Format.FORWARDREF)
{'return': 'Bar'}
>>> get_annotations(foo,format=Format.STRING)
{'return': 'Bar'}
>>> get_annotations(foo,format=Format.VALUE)
{'return': 'Bar'}
>>> get_annotations(foo)
{'return': 'Bar'}
```

(Note: the result of the last call to `get_annotations` in these
examples relies on the fact that, as of this writing, the default value
for `format` is `Format.VALUE`).

If one day we support lint rules targeting code that introspects using
the new `annotationlib`, then it is possible we will need to revisit our
approximation.

Closes #15100
2025-05-05 06:40:36 -05:00
..
visitor Upgrade to Rust 1.86 and bump MSRV to 1.84 (#17171) 2025-04-03 15:59:44 +00:00
comparable.rs Use #[expect(lint)] over #[allow(lint)] where possible (#17822) 2025-05-03 21:20:31 +02:00
docstrings.rs Move Python whitespace utilities into new ruff_python_whitespace crate (#4993) 2023-06-10 00:59:57 +00:00
expression.rs Remove customizable reference enum names (#15647) 2025-01-21 13:46:31 -05:00
generated.rs Auto generate visit_source_order (#17180) 2025-04-17 08:59:57 -04:00
helpers.rs [syntax-errors] Duplicate type parameter names (#16858) 2025-03-21 15:06:22 -04:00
identifier.rs Upgrade to Rust 1.86 and bump MSRV to 1.84 (#17171) 2025-04-03 15:59:44 +00:00
int.rs [ruff] Unnecessary cast to int (RUF046) (#14697) 2024-12-05 10:30:06 +01:00
lib.rs Use ast::PythonVersion internally in the formatter and linter (#16170) 2025-02-18 12:03:13 -05:00
name.rs Auto generate ast expression nodes (#16285) 2025-03-05 08:25:55 -05:00
node.rs Auto generate visit_source_order (#17180) 2025-04-17 08:59:57 -04:00
nodes.rs Use #[expect(lint)] over #[allow(lint)] where possible (#17822) 2025-05-03 21:20:31 +02:00
operator_precedence.rs Use the common OperatorPrecedence for the parser (#16747) 2025-03-21 09:40:37 +05:30
parenthesize.rs Remove customizable reference enum names (#15647) 2025-01-21 13:46:31 -05:00
python_version.rs Implement deferred annotations for Python 3.14 (#17658) 2025-05-05 06:40:36 -05:00
relocate.rs Auto generate ast expression nodes (#16285) 2025-03-05 08:25:55 -05:00
script.rs bump MSRV to 1.83 (#16294) 2025-02-26 06:12:43 -08:00
statement_visitor.rs Remove Stmt::TryStar (#6566) 2023-08-14 13:39:44 -04:00
stmt_if.rs Misc. small tweaks from perusing modules (#9383) 2024-01-03 12:30:25 -05:00
str.rs Preserve triple quotes and prefixes for strings (#15818) 2025-02-04 08:41:06 -05:00
str_prefix.rs Add text_len() methods to more *Prefix enums in ruff_python_ast (#16254) 2025-02-19 14:47:07 +00:00
traversal.rs Use referencial equality in traversal helper methods (#13895) 2024-10-24 11:30:22 +02:00
types.rs Remove RefEquality (#6393) 2023-08-07 16:04:50 +00:00
visitor.rs Upgrade to Rust 1.86 and bump MSRV to 1.84 (#17171) 2025-04-03 15:59:44 +00:00
whitespace.rs Extract LineIndex independent methods from Locator (#13938) 2024-10-28 07:53:41 +00:00