mirror of
https://github.com/astral-sh/ruff.git
synced 2025-12-23 09:19:39 +00:00
[ty] Respect deferred values in keyword arguments et al for .pyi files (#22029)
Some checks are pending
CI / cargo clippy (push) Blocked by required conditions
CI / pre-commit (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / test scripts (push) Blocked by required conditions
CI / benchmarks instrumented (ruff) (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (${{ github.repository == 'astral-sh/ruff' && 'depot-windows-2022-16' || 'windows-latest' }}) (push) Blocked by required conditions
CI / cargo test (macos-latest) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / ty completion evaluation (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks walltime (medium|multithreaded) (push) Blocked by required conditions
CI / benchmarks walltime (small|large) (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
Some checks are pending
CI / cargo clippy (push) Blocked by required conditions
CI / pre-commit (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / test scripts (push) Blocked by required conditions
CI / benchmarks instrumented (ruff) (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (${{ github.repository == 'astral-sh/ruff' && 'depot-windows-2022-16' || 'windows-latest' }}) (push) Blocked by required conditions
CI / cargo test (macos-latest) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / ty completion evaluation (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks walltime (medium|multithreaded) (push) Blocked by required conditions
CI / benchmarks walltime (small|large) (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
## Summary Closes https://github.com/astral-sh/ty/issues/2019.
This commit is contained in:
parent
638f230910
commit
5bb9ee2a9d
2 changed files with 105 additions and 0 deletions
|
|
@ -205,3 +205,93 @@ class B:
|
|||
class A(B): ...
|
||||
class B: ...
|
||||
```
|
||||
|
||||
## Default argument values
|
||||
|
||||
### Not deferred in regular files
|
||||
|
||||
```py
|
||||
# error: [unresolved-reference]
|
||||
def f(mode: int = ParseMode.test):
|
||||
pass
|
||||
|
||||
class ParseMode:
|
||||
test = 1
|
||||
```
|
||||
|
||||
### Deferred in stub files
|
||||
|
||||
Forward references in default argument values are allowed in stub files.
|
||||
|
||||
```pyi
|
||||
def f(mode: int = ParseMode.test): ...
|
||||
|
||||
class ParseMode:
|
||||
test: int
|
||||
```
|
||||
|
||||
### Undefined names are still errors in stub files
|
||||
|
||||
```pyi
|
||||
# error: [unresolved-reference]
|
||||
def f(mode: int = NeverDefined.test): ...
|
||||
```
|
||||
|
||||
## Class keyword arguments
|
||||
|
||||
### Not deferred in regular files
|
||||
|
||||
```py
|
||||
# error: [unresolved-reference]
|
||||
class Foo(metaclass=SomeMeta):
|
||||
pass
|
||||
|
||||
class SomeMeta(type):
|
||||
pass
|
||||
```
|
||||
|
||||
### Deferred in stub files
|
||||
|
||||
Forward references in class keyword arguments are allowed in stub files.
|
||||
|
||||
```pyi
|
||||
class Foo(metaclass=SomeMeta): ...
|
||||
|
||||
class SomeMeta(type): ...
|
||||
```
|
||||
|
||||
### Undefined names are still errors in stub files
|
||||
|
||||
```pyi
|
||||
# error: [unresolved-reference]
|
||||
class Foo(metaclass=NeverDefined): ...
|
||||
```
|
||||
|
||||
## Lambda default argument values
|
||||
|
||||
### Not deferred in regular files
|
||||
|
||||
```py
|
||||
# error: [unresolved-reference]
|
||||
f = lambda x=Foo(): x
|
||||
|
||||
class Foo:
|
||||
pass
|
||||
```
|
||||
|
||||
### Deferred in stub files
|
||||
|
||||
Forward references in lambda default argument values are allowed in stub files.
|
||||
|
||||
```pyi
|
||||
f = lambda x=Foo(): x
|
||||
|
||||
class Foo: ...
|
||||
```
|
||||
|
||||
### Undefined names are still errors in stub files
|
||||
|
||||
```pyi
|
||||
# error: [unresolved-reference]
|
||||
f = lambda x=NeverDefined(): x
|
||||
```
|
||||
|
|
|
|||
|
|
@ -2358,12 +2358,16 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
|||
decorator_types_and_nodes.push((decorator_type, decorator));
|
||||
}
|
||||
|
||||
// In stub files, default values may reference names that are defined later in the file.
|
||||
let in_stub = self.in_stub();
|
||||
let previous_deferred_state = std::mem::replace(&mut self.deferred_state, in_stub.into());
|
||||
for default in parameters
|
||||
.iter_non_variadic_params()
|
||||
.filter_map(|param| param.default.as_deref())
|
||||
{
|
||||
self.infer_expression(default, TypeContext::default());
|
||||
}
|
||||
self.deferred_state = previous_deferred_state;
|
||||
|
||||
// If there are type params, parameters and returns are evaluated in that scope. Otherwise,
|
||||
// we always defer the inference of the parameters and returns. That ensures that we do not
|
||||
|
|
@ -2998,9 +3002,14 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
|||
// if there are type parameters, then the keywords and bases are within that scope
|
||||
// and we don't need to run inference here
|
||||
if type_params.is_none() {
|
||||
// In stub files, keyword values may reference names that are defined later in the file.
|
||||
let in_stub = self.in_stub();
|
||||
let previous_deferred_state =
|
||||
std::mem::replace(&mut self.deferred_state, in_stub.into());
|
||||
for keyword in class_node.keywords() {
|
||||
self.infer_expression(&keyword.value, TypeContext::default());
|
||||
}
|
||||
self.deferred_state = previous_deferred_state;
|
||||
|
||||
// Inference of bases deferred in stubs, or if any are string literals.
|
||||
if self.in_stub() || class_node.bases().iter().any(contains_string_literal) {
|
||||
|
|
@ -8322,6 +8331,10 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
|||
body: _,
|
||||
} = lambda_expression;
|
||||
|
||||
// In stub files, default values may reference names that are defined later in the file.
|
||||
let in_stub = self.in_stub();
|
||||
let previous_deferred_state = std::mem::replace(&mut self.deferred_state, in_stub.into());
|
||||
|
||||
let parameters = if let Some(parameters) = parameters {
|
||||
let positional_only = parameters
|
||||
.posonlyargs
|
||||
|
|
@ -8387,6 +8400,8 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
|||
Parameters::empty()
|
||||
};
|
||||
|
||||
self.deferred_state = previous_deferred_state;
|
||||
|
||||
// TODO: Useful inference of a lambda's return type will require a different approach,
|
||||
// which does the inference of the body expression based on arguments at each call site,
|
||||
// rather than eagerly computing a return type without knowing the argument types.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue