Skip to content

Commit e7024ae

Browse files
committed
revert unnecessary changes for the purpose of this PR
1 parent 6419669 commit e7024ae

File tree

17 files changed

+327
-690
lines changed

17 files changed

+327
-690
lines changed

crates/ty_python_semantic/resources/corpus/divergent.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ def unwrap(value):
5757
else:
5858
raise TypeError()
5959

60+
# TODO: If this is commented out, that is, if `infer_scope_types` is called before `infer_return_type`, it will panic.
61+
reveal_type(unwrap(Foo()))
62+
6063
def descent(x: int, y: int):
6164
if x > y:
6265
y, x = descent(y, x)
@@ -67,3 +70,6 @@ def descent(x: int, y: int):
6770
return (0, 1)
6871
else:
6972
return descent(x-1, y-1)
73+
74+
# TODO: If this is commented out, that is, if `infer_scope_types` is called before `infer_return_type`, it will panic.
75+
reveal_type(descent(5, 3))

crates/ty_python_semantic/resources/mdtest/attributes.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,24 +2281,6 @@ class B:
22812281

22822282
reveal_type(B().x) # revealed: Unknown | Literal[1]
22832283
reveal_type(A().x) # revealed: Unknown | Literal[1]
2284-
2285-
class Base:
2286-
def flip(self) -> "Sub":
2287-
return Sub()
2288-
2289-
class Sub(Base):
2290-
# TODO invalid override error
2291-
def flip(self) -> "Base":
2292-
return Base()
2293-
2294-
class C2:
2295-
def __init__(self, x: Sub):
2296-
self.x = x
2297-
2298-
def replace_with(self, other: "C2"):
2299-
self.x = other.x.flip()
2300-
2301-
reveal_type(C2(Sub()).x) # revealed: Unknown | Base
23022284
```
23032285

23042286
This case additionally tests our union/intersection simplification logic:

crates/ty_python_semantic/resources/mdtest/cycle.md

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,3 @@ p = Point()
3131
reveal_type(p.x) # revealed: Unknown | int
3232
reveal_type(p.y) # revealed: Unknown | int
3333
```
34-
35-
## Self-referential bare type alias
36-
37-
```py
38-
A = list["A" | None]
39-
40-
def f(x: A):
41-
# TODO: should be `list[A | None]`?
42-
reveal_type(x) # revealed: list[Divergent]
43-
# TODO: should be `A | None`?
44-
reveal_type(x[0]) # revealed: Divergent
45-
```
46-
47-
## Self-referential type variables
48-
49-
```py
50-
from typing import Generic, TypeVar
51-
52-
B = TypeVar("B", bound="Base")
53-
54-
# TODO: no error
55-
# error: [invalid-argument-type] "`typing.TypeVar | typing.TypeVar` is not a valid argument to `Generic`"
56-
class Base(Generic[B]):
57-
pass
58-
```

crates/ty_python_semantic/src/dunder_all.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use ruff_python_ast::name::Name;
66
use ruff_python_ast::statement_visitor::{StatementVisitor, walk_stmt};
77
use ruff_python_ast::{self as ast};
88

9-
use crate::semantic_index::global_scope;
10-
use crate::types::{Truthiness, Type, infer_scope_expression_type};
9+
use crate::semantic_index::{SemanticIndex, semantic_index};
10+
use crate::types::{Truthiness, Type, TypeContext, infer_expression_types};
1111
use crate::{Db, ModuleName, resolve_module};
1212

1313
#[allow(clippy::ref_option)]
@@ -31,7 +31,8 @@ pub(crate) fn dunder_all_names(db: &dyn Db, file: File) -> Option<FxHashSet<Name
3131
let _span = tracing::trace_span!("dunder_all_names", file=?file.path(db)).entered();
3232

3333
let module = parsed_module(db, file).load(db);
34-
let mut collector = DunderAllNamesCollector::new(db, file);
34+
let index = semantic_index(db, file);
35+
let mut collector = DunderAllNamesCollector::new(db, file, index);
3536
collector.visit_body(module.suite());
3637
collector.into_names()
3738
}
@@ -41,6 +42,9 @@ struct DunderAllNamesCollector<'db> {
4142
db: &'db dyn Db,
4243
file: File,
4344

45+
/// The semantic index for the module.
46+
index: &'db SemanticIndex<'db>,
47+
4448
/// The origin of the `__all__` variable in the current module, [`None`] if it is not defined.
4549
origin: Option<DunderAllOrigin>,
4650

@@ -53,10 +57,11 @@ struct DunderAllNamesCollector<'db> {
5357
}
5458

5559
impl<'db> DunderAllNamesCollector<'db> {
56-
fn new(db: &'db dyn Db, file: File) -> Self {
60+
fn new(db: &'db dyn Db, file: File, index: &'db SemanticIndex<'db>) -> Self {
5761
Self {
5862
db,
5963
file,
64+
index,
6065
origin: None,
6166
invalid: false,
6267
names: FxHashSet::default(),
@@ -177,7 +182,8 @@ impl<'db> DunderAllNamesCollector<'db> {
177182
///
178183
/// This function panics if `expr` was not marked as a standalone expression during semantic indexing.
179184
fn standalone_expression_type(&self, expr: &ast::Expr) -> Type<'db> {
180-
infer_scope_expression_type(self.db, global_scope(self.db, self.file), expr)
185+
infer_expression_types(self.db, self.index.expression(expr), TypeContext::default())
186+
.expression_type(expr)
181187
}
182188

183189
/// Evaluate the given expression and return its truthiness.

crates/ty_python_semantic/src/semantic_model.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::semantic_index::definition::Definition;
1212
use crate::semantic_index::scope::FileScopeId;
1313
use crate::semantic_index::semantic_index;
1414
use crate::types::ide_support::all_declarations_and_bindings;
15-
use crate::types::{Type, binding_type, infer_scope_expression_type};
15+
use crate::types::{Type, binding_type, infer_scope_types};
1616

1717
pub struct SemanticModel<'db> {
1818
db: &'db dyn Db,
@@ -363,7 +363,7 @@ impl HasType for ast::ExprRef<'_> {
363363
let file_scope = index.expression_scope_id(self);
364364
let scope = file_scope.to_scope_id(model.db, model.file);
365365

366-
infer_scope_expression_type(model.db, scope, *self)
366+
infer_scope_types(model.db, scope).expression_type(*self)
367367
}
368368
}
369369

0 commit comments

Comments
 (0)