ruff/crates
Simon 1639488082
[red-knot] support fstring expressions (#13511)
<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

Implement inference for `f-string`, contributes to #12701.

### First Implementation

When looking at the way `mypy` handles things, I noticed the following:
- No variables (e.g. `f"hello"`) ⇒ `LiteralString`
- Any variable (e.g. `f"number {1}"`) ⇒ `str`

My first commit (1ba5d0f13fdf70ed8b2b1a41433b32fc9085add2) implements
exactly this logic, except that we deal with string literals just like
`infer_string_literal_expression` (if below `MAX_STRING_LITERAL_SIZE`,
show `Literal["exact string"]`)

### Second Implementation

My second commit (90326ce9af5549af7b4efae89cd074ddf68ada14) pushes
things a bit further to handle cases where the expression within the
`f-string` are all literal values (string representation known at static
time).

Here's an example of when this could happen in code:
```python
BASE_URL = "https://httpbin.org"
VERSION = "v1"
endpoint = f"{BASE_URL}/{VERSION}/post"  # Literal["https://httpbin.org/v1/post"]
```
As this can be sightly more costly (additional allocations), I don't
know if we want this feature.

## Test Plan

- Added a test `fstring_expression` covering all cases I can think of

---------

Co-authored-by: Carl Meyer <carl@astral.sh>
2024-09-27 10:29:21 -07:00
..
red_knot Use an empty vendored file system in Ruff (#13436) 2024-09-21 16:31:42 +00:00
red_knot_python_semantic [red-knot] support fstring expressions (#13511) 2024-09-27 10:29:21 -07:00
red_knot_server [red-knot] Support untitled files in the server (#13044) 2024-08-23 12:47:35 +05:30
red_knot_wasm Use an empty vendored file system in Ruff (#13436) 2024-09-21 16:31:42 +00:00
red_knot_workspace Use an empty vendored file system in Ruff (#13436) 2024-09-21 16:31:42 +00:00
ruff Remove jemalloc crate when building on AIX (#13529) 2024-09-26 13:20:54 -04:00
ruff_benchmark [red-knot] use declared types in inference/checking (#13335) 2024-09-17 08:11:06 -07:00
ruff_cache Fix cache key collisions for paths with separators (#12159) 2024-07-03 07:36:46 -05:00
ruff_db Use an empty vendored file system in Ruff (#13436) 2024-09-21 16:31:42 +00:00
ruff_dev Fix parentheses around return type annotations (#13381) 2024-09-20 09:23:53 +02:00
ruff_diagnostics [Minor typo] Fix article in "an fix" (#12797) 2024-08-10 21:22:00 -04:00
ruff_formatter Upgrade to Rust 1.80 (#12586) 2024-07-30 19:18:08 +00:00
ruff_graph Don't skip over imports and other nodes containing nested statements in import collector (#13521) 2024-09-26 11:57:05 +00:00
ruff_index [red-knot] small efficiency improvements and bugfixes to use-def map building (#12373) 2024-07-18 09:24:58 -07:00
ruff_linter Do not offer an invalid fix for PLR1716 when the comparisons contain parenthesis (#13527) 2024-09-26 19:01:06 +00:00
ruff_macros Drop deprecated nursery rule group (#10172) 2024-06-27 13:44:11 +02:00
ruff_notebook Fallback to kernelspec to check if it's a Python notebook (#12875) 2024-08-14 12:36:09 +05:30
ruff_python_ast [internal] ComparableExpr (f)strings and bytes made invariant under concatenation (#13301) 2024-09-25 16:58:57 +02:00
ruff_python_ast_integration_tests [internal] ComparableExpr (f)strings and bytes made invariant under concatenation (#13301) 2024-09-25 16:58:57 +02:00
ruff_python_codegen Implement iter(), len() and is_empty() for all display-literal AST nodes (#12807) 2024-08-12 10:39:28 +00:00
ruff_python_formatter refactor: Simplify quote selection logic (#13536) 2024-09-27 14:40:28 +02:00
ruff_python_index Enable token-based rules on source with syntax errors (#11950) 2024-07-02 08:57:46 +00:00
ruff_python_literal
ruff_python_parser Expose internal types as public access (#13509) 2024-09-26 17:34:30 +02:00
ruff_python_resolver
ruff_python_semantic Detect tuples bound to variadic positional arguments i.e. *args (#13512) 2024-09-25 10:03:25 -05:00
ruff_python_stdlib Remove allocation from ruff_python_stdlib::builtins::python_builtins (#13317) 2024-09-10 16:34:24 -04:00
ruff_python_trivia Fix placement of inline parameter comments (#13379) 2024-09-18 08:26:06 +02:00
ruff_python_trivia_integration_tests
ruff_server Add support for extensionless Python files for server (#13326) 2024-09-12 00:35:26 +05:30
ruff_source_file Fix off-by one error in the LineIndex::offset calculation (#13407) 2024-09-19 11:58:45 +00:00
ruff_text_size Upgrade to Rust 1.79 (#11875) 2024-06-17 07:15:10 +01:00
ruff_vendored Use an empty vendored file system in Ruff (#13436) 2024-09-21 16:31:42 +00:00
ruff_wasm Bump version to 0.6.8 (#13522) 2024-09-26 14:09:03 +02:00
ruff_workspace Fix formatting for analyze direction values (#13476) 2024-09-23 09:18:28 +02:00