Skip to content

Commit 792f9e3

Browse files
authored
[red-knot] Rename *_ty functions (#15617)
## Summary General rules: * Change the `_ty` suffix of all functions to `_type`. * `_type_and_qualifiers` suffixes seem too long, so we ignore the existence of qualifiers and still speak of "types" * Functions only have a `_type` suffix if they return either `Type`, `Option<Type>`, or `TypeAndQualifiers` Free functions: * `binding_ty` => `binding_type` * `declaration_ty` => `declaration_type` * `definition_expression_ty` => `definition_expression_type` Methods: * `CallDunderResult::return_ty` => `return_type` * `NotCallableError::return_ty` => `return_type` * `NotCallableError::called_ty` => `called_type` * `TypeAndQualifiers::inner_ty` => `inner_type` * `TypeAliasType::value_ty` => `value_type` * `TypeInference::expression_ty` => `expression_type` * `TypeInference::try_expression_ty` => `try_expression_type` * `TypeInference::binding_ty` => `binding_type` * `TypeInference::declaration_ty` => `declaration_type` * `TypeInferenceBuilder::expression_ty` => `expression_type` * `TypeInferenceBuilder::file_expression_ty` => `file_expression_type` * `TypeInferenceBuilder::module_ty_from_name` => `module_type_from_name` * `ClassBase::try_from_ty` => `try_from_type` * `Parameter::annotated_ty` => `annotated_type` * `Parameter::default_ty` => `default_type` * `CallOutcome::return_ty` => `return_type` * `CallOutcome::return_ty_result` => `return_type_result` * `CallBinding::from_return_ty` => `from_return_type` * `CallBinding::set_return_ty` => `set_return_type` * `CallBinding::return_ty` => `return_type` * `CallBinding::parameter_tys` => `parameter_types` * `CallBinding::one_parameter_ty` => `one_parameter_type` * `CallBinding::two_parameter_tys` => `two_parameter_types` * `Unpacker::tuple_ty_elements` => `tuple_type_elements` * `StringPartsCollector::ty` => `string_type` Traits * `HasTy` => `HasType` * `HasTy::ty` => `inferred_type` Test functions: * `assert_public_ty` => `assert_public_type` * `assert_scope_ty` => `assert_scope_type` closes #15569 ## Test Plan —
1 parent 6fe404a commit 792f9e3

File tree

15 files changed

+335
-329
lines changed

15 files changed

+335
-329
lines changed

crates/red_knot_project/tests/check.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::{anyhow, Context};
22
use red_knot_project::{ProjectDatabase, ProjectMetadata};
3-
use red_knot_python_semantic::{HasTy, SemanticModel};
3+
use red_knot_python_semantic::{HasType, SemanticModel};
44
use ruff_db::files::{system_path_to_file, File};
55
use ruff_db::parsed::parsed_module;
66
use ruff_db::system::{SystemPath, SystemPathBuf, TestSystem};
@@ -197,10 +197,10 @@ impl SourceOrderVisitor<'_> for PullTypesVisitor<'_> {
197197
fn visit_stmt(&mut self, stmt: &Stmt) {
198198
match stmt {
199199
Stmt::FunctionDef(function) => {
200-
let _ty = function.ty(&self.model);
200+
let _ty = function.inferred_type(&self.model);
201201
}
202202
Stmt::ClassDef(class) => {
203-
let _ty = class.ty(&self.model);
203+
let _ty = class.inferred_type(&self.model);
204204
}
205205
Stmt::Assign(assign) => {
206206
for target in &assign.targets {
@@ -243,25 +243,25 @@ impl SourceOrderVisitor<'_> for PullTypesVisitor<'_> {
243243
}
244244

245245
fn visit_expr(&mut self, expr: &Expr) {
246-
let _ty = expr.ty(&self.model);
246+
let _ty = expr.inferred_type(&self.model);
247247

248248
source_order::walk_expr(self, expr);
249249
}
250250

251251
fn visit_parameter(&mut self, parameter: &Parameter) {
252-
let _ty = parameter.ty(&self.model);
252+
let _ty = parameter.inferred_type(&self.model);
253253

254254
source_order::walk_parameter(self, parameter);
255255
}
256256

257257
fn visit_parameter_with_default(&mut self, parameter_with_default: &ParameterWithDefault) {
258-
let _ty = parameter_with_default.ty(&self.model);
258+
let _ty = parameter_with_default.inferred_type(&self.model);
259259

260260
source_order::walk_parameter_with_default(self, parameter_with_default);
261261
}
262262

263263
fn visit_alias(&mut self, alias: &Alias) {
264-
let _ty = alias.ty(&self.model);
264+
let _ty = alias.inferred_type(&self.model);
265265

266266
source_order::walk_alias(self, alias);
267267
}

crates/red_knot_python_semantic/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub use module_resolver::{resolve_module, system_module_search_paths, KnownModul
1010
pub use program::{Program, ProgramSettings, SearchPathSettings, SitePackages};
1111
pub use python_platform::PythonPlatform;
1212
pub use python_version::PythonVersion;
13-
pub use semantic_model::{HasTy, SemanticModel};
13+
pub use semantic_model::{HasType, SemanticModel};
1414

1515
pub mod ast_node_ref;
1616
mod db;

crates/red_knot_python_semantic/src/semantic_index/builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -603,8 +603,8 @@ impl<'db> SemanticIndexBuilder<'db> {
603603

604604
let definition = self.add_definition(symbol, parameter);
605605

606-
// Insert a mapping from the inner Parameter node to the same definition.
607-
// This ensures that calling `HasTy::ty` on the inner parameter returns
606+
// Insert a mapping from the inner Parameter node to the same definition. This
607+
// ensures that calling `HasType::inferred_type` on the inner parameter returns
608608
// a valid type (and doesn't panic)
609609
let existing_definition = self
610610
.definitions_by_node

crates/red_knot_python_semantic/src/semantic_model.rs

+87-87
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::module_name::ModuleName;
88
use crate::module_resolver::{resolve_module, Module};
99
use crate::semantic_index::ast_ids::HasScopedExpressionId;
1010
use crate::semantic_index::semantic_index;
11-
use crate::types::{binding_ty, infer_scope_types, Type};
11+
use crate::types::{binding_type, infer_scope_types, Type};
1212
use crate::Db;
1313

1414
pub struct SemanticModel<'db> {
@@ -40,117 +40,117 @@ impl<'db> SemanticModel<'db> {
4040
}
4141
}
4242

43-
pub trait HasTy {
43+
pub trait HasType {
4444
/// Returns the inferred type of `self`.
4545
///
4646
/// ## Panics
4747
/// May panic if `self` is from another file than `model`.
48-
fn ty<'db>(&self, model: &SemanticModel<'db>) -> Type<'db>;
48+
fn inferred_type<'db>(&self, model: &SemanticModel<'db>) -> Type<'db>;
4949
}
5050

51-
impl HasTy for ast::ExprRef<'_> {
52-
fn ty<'db>(&self, model: &SemanticModel<'db>) -> Type<'db> {
51+
impl HasType for ast::ExprRef<'_> {
52+
fn inferred_type<'db>(&self, model: &SemanticModel<'db>) -> Type<'db> {
5353
let index = semantic_index(model.db, model.file);
5454
let file_scope = index.expression_scope_id(*self);
5555
let scope = file_scope.to_scope_id(model.db, model.file);
5656

5757
let expression_id = self.scoped_expression_id(model.db, scope);
58-
infer_scope_types(model.db, scope).expression_ty(expression_id)
58+
infer_scope_types(model.db, scope).expression_type(expression_id)
5959
}
6060
}
6161

62-
macro_rules! impl_expression_has_ty {
62+
macro_rules! impl_expression_has_type {
6363
($ty: ty) => {
64-
impl HasTy for $ty {
64+
impl HasType for $ty {
6565
#[inline]
66-
fn ty<'db>(&self, model: &SemanticModel<'db>) -> Type<'db> {
66+
fn inferred_type<'db>(&self, model: &SemanticModel<'db>) -> Type<'db> {
6767
let expression_ref = ExprRef::from(self);
68-
expression_ref.ty(model)
68+
expression_ref.inferred_type(model)
6969
}
7070
}
7171
};
7272
}
7373

74-
impl_expression_has_ty!(ast::ExprBoolOp);
75-
impl_expression_has_ty!(ast::ExprNamed);
76-
impl_expression_has_ty!(ast::ExprBinOp);
77-
impl_expression_has_ty!(ast::ExprUnaryOp);
78-
impl_expression_has_ty!(ast::ExprLambda);
79-
impl_expression_has_ty!(ast::ExprIf);
80-
impl_expression_has_ty!(ast::ExprDict);
81-
impl_expression_has_ty!(ast::ExprSet);
82-
impl_expression_has_ty!(ast::ExprListComp);
83-
impl_expression_has_ty!(ast::ExprSetComp);
84-
impl_expression_has_ty!(ast::ExprDictComp);
85-
impl_expression_has_ty!(ast::ExprGenerator);
86-
impl_expression_has_ty!(ast::ExprAwait);
87-
impl_expression_has_ty!(ast::ExprYield);
88-
impl_expression_has_ty!(ast::ExprYieldFrom);
89-
impl_expression_has_ty!(ast::ExprCompare);
90-
impl_expression_has_ty!(ast::ExprCall);
91-
impl_expression_has_ty!(ast::ExprFString);
92-
impl_expression_has_ty!(ast::ExprStringLiteral);
93-
impl_expression_has_ty!(ast::ExprBytesLiteral);
94-
impl_expression_has_ty!(ast::ExprNumberLiteral);
95-
impl_expression_has_ty!(ast::ExprBooleanLiteral);
96-
impl_expression_has_ty!(ast::ExprNoneLiteral);
97-
impl_expression_has_ty!(ast::ExprEllipsisLiteral);
98-
impl_expression_has_ty!(ast::ExprAttribute);
99-
impl_expression_has_ty!(ast::ExprSubscript);
100-
impl_expression_has_ty!(ast::ExprStarred);
101-
impl_expression_has_ty!(ast::ExprName);
102-
impl_expression_has_ty!(ast::ExprList);
103-
impl_expression_has_ty!(ast::ExprTuple);
104-
impl_expression_has_ty!(ast::ExprSlice);
105-
impl_expression_has_ty!(ast::ExprIpyEscapeCommand);
106-
107-
impl HasTy for ast::Expr {
108-
fn ty<'db>(&self, model: &SemanticModel<'db>) -> Type<'db> {
74+
impl_expression_has_type!(ast::ExprBoolOp);
75+
impl_expression_has_type!(ast::ExprNamed);
76+
impl_expression_has_type!(ast::ExprBinOp);
77+
impl_expression_has_type!(ast::ExprUnaryOp);
78+
impl_expression_has_type!(ast::ExprLambda);
79+
impl_expression_has_type!(ast::ExprIf);
80+
impl_expression_has_type!(ast::ExprDict);
81+
impl_expression_has_type!(ast::ExprSet);
82+
impl_expression_has_type!(ast::ExprListComp);
83+
impl_expression_has_type!(ast::ExprSetComp);
84+
impl_expression_has_type!(ast::ExprDictComp);
85+
impl_expression_has_type!(ast::ExprGenerator);
86+
impl_expression_has_type!(ast::ExprAwait);
87+
impl_expression_has_type!(ast::ExprYield);
88+
impl_expression_has_type!(ast::ExprYieldFrom);
89+
impl_expression_has_type!(ast::ExprCompare);
90+
impl_expression_has_type!(ast::ExprCall);
91+
impl_expression_has_type!(ast::ExprFString);
92+
impl_expression_has_type!(ast::ExprStringLiteral);
93+
impl_expression_has_type!(ast::ExprBytesLiteral);
94+
impl_expression_has_type!(ast::ExprNumberLiteral);
95+
impl_expression_has_type!(ast::ExprBooleanLiteral);
96+
impl_expression_has_type!(ast::ExprNoneLiteral);
97+
impl_expression_has_type!(ast::ExprEllipsisLiteral);
98+
impl_expression_has_type!(ast::ExprAttribute);
99+
impl_expression_has_type!(ast::ExprSubscript);
100+
impl_expression_has_type!(ast::ExprStarred);
101+
impl_expression_has_type!(ast::ExprName);
102+
impl_expression_has_type!(ast::ExprList);
103+
impl_expression_has_type!(ast::ExprTuple);
104+
impl_expression_has_type!(ast::ExprSlice);
105+
impl_expression_has_type!(ast::ExprIpyEscapeCommand);
106+
107+
impl HasType for ast::Expr {
108+
fn inferred_type<'db>(&self, model: &SemanticModel<'db>) -> Type<'db> {
109109
match self {
110-
Expr::BoolOp(inner) => inner.ty(model),
111-
Expr::Named(inner) => inner.ty(model),
112-
Expr::BinOp(inner) => inner.ty(model),
113-
Expr::UnaryOp(inner) => inner.ty(model),
114-
Expr::Lambda(inner) => inner.ty(model),
115-
Expr::If(inner) => inner.ty(model),
116-
Expr::Dict(inner) => inner.ty(model),
117-
Expr::Set(inner) => inner.ty(model),
118-
Expr::ListComp(inner) => inner.ty(model),
119-
Expr::SetComp(inner) => inner.ty(model),
120-
Expr::DictComp(inner) => inner.ty(model),
121-
Expr::Generator(inner) => inner.ty(model),
122-
Expr::Await(inner) => inner.ty(model),
123-
Expr::Yield(inner) => inner.ty(model),
124-
Expr::YieldFrom(inner) => inner.ty(model),
125-
Expr::Compare(inner) => inner.ty(model),
126-
Expr::Call(inner) => inner.ty(model),
127-
Expr::FString(inner) => inner.ty(model),
128-
Expr::StringLiteral(inner) => inner.ty(model),
129-
Expr::BytesLiteral(inner) => inner.ty(model),
130-
Expr::NumberLiteral(inner) => inner.ty(model),
131-
Expr::BooleanLiteral(inner) => inner.ty(model),
132-
Expr::NoneLiteral(inner) => inner.ty(model),
133-
Expr::EllipsisLiteral(inner) => inner.ty(model),
134-
Expr::Attribute(inner) => inner.ty(model),
135-
Expr::Subscript(inner) => inner.ty(model),
136-
Expr::Starred(inner) => inner.ty(model),
137-
Expr::Name(inner) => inner.ty(model),
138-
Expr::List(inner) => inner.ty(model),
139-
Expr::Tuple(inner) => inner.ty(model),
140-
Expr::Slice(inner) => inner.ty(model),
141-
Expr::IpyEscapeCommand(inner) => inner.ty(model),
110+
Expr::BoolOp(inner) => inner.inferred_type(model),
111+
Expr::Named(inner) => inner.inferred_type(model),
112+
Expr::BinOp(inner) => inner.inferred_type(model),
113+
Expr::UnaryOp(inner) => inner.inferred_type(model),
114+
Expr::Lambda(inner) => inner.inferred_type(model),
115+
Expr::If(inner) => inner.inferred_type(model),
116+
Expr::Dict(inner) => inner.inferred_type(model),
117+
Expr::Set(inner) => inner.inferred_type(model),
118+
Expr::ListComp(inner) => inner.inferred_type(model),
119+
Expr::SetComp(inner) => inner.inferred_type(model),
120+
Expr::DictComp(inner) => inner.inferred_type(model),
121+
Expr::Generator(inner) => inner.inferred_type(model),
122+
Expr::Await(inner) => inner.inferred_type(model),
123+
Expr::Yield(inner) => inner.inferred_type(model),
124+
Expr::YieldFrom(inner) => inner.inferred_type(model),
125+
Expr::Compare(inner) => inner.inferred_type(model),
126+
Expr::Call(inner) => inner.inferred_type(model),
127+
Expr::FString(inner) => inner.inferred_type(model),
128+
Expr::StringLiteral(inner) => inner.inferred_type(model),
129+
Expr::BytesLiteral(inner) => inner.inferred_type(model),
130+
Expr::NumberLiteral(inner) => inner.inferred_type(model),
131+
Expr::BooleanLiteral(inner) => inner.inferred_type(model),
132+
Expr::NoneLiteral(inner) => inner.inferred_type(model),
133+
Expr::EllipsisLiteral(inner) => inner.inferred_type(model),
134+
Expr::Attribute(inner) => inner.inferred_type(model),
135+
Expr::Subscript(inner) => inner.inferred_type(model),
136+
Expr::Starred(inner) => inner.inferred_type(model),
137+
Expr::Name(inner) => inner.inferred_type(model),
138+
Expr::List(inner) => inner.inferred_type(model),
139+
Expr::Tuple(inner) => inner.inferred_type(model),
140+
Expr::Slice(inner) => inner.inferred_type(model),
141+
Expr::IpyEscapeCommand(inner) => inner.inferred_type(model),
142142
}
143143
}
144144
}
145145

146146
macro_rules! impl_binding_has_ty {
147147
($ty: ty) => {
148-
impl HasTy for $ty {
148+
impl HasType for $ty {
149149
#[inline]
150-
fn ty<'db>(&self, model: &SemanticModel<'db>) -> Type<'db> {
150+
fn inferred_type<'db>(&self, model: &SemanticModel<'db>) -> Type<'db> {
151151
let index = semantic_index(model.db, model.file);
152152
let binding = index.definition(self);
153-
binding_ty(model.db, binding)
153+
binding_type(model.db, binding)
154154
}
155155
}
156156
};
@@ -168,10 +168,10 @@ mod tests {
168168
use ruff_db::parsed::parsed_module;
169169

170170
use crate::db::tests::TestDbBuilder;
171-
use crate::{HasTy, SemanticModel};
171+
use crate::{HasType, SemanticModel};
172172

173173
#[test]
174-
fn function_ty() -> anyhow::Result<()> {
174+
fn function_type() -> anyhow::Result<()> {
175175
let db = TestDbBuilder::new()
176176
.with_file("/src/foo.py", "def test(): pass")
177177
.build()?;
@@ -182,15 +182,15 @@ mod tests {
182182

183183
let function = ast.suite()[0].as_function_def_stmt().unwrap();
184184
let model = SemanticModel::new(&db, foo);
185-
let ty = function.ty(&model);
185+
let ty = function.inferred_type(&model);
186186

187187
assert!(ty.is_function_literal());
188188

189189
Ok(())
190190
}
191191

192192
#[test]
193-
fn class_ty() -> anyhow::Result<()> {
193+
fn class_type() -> anyhow::Result<()> {
194194
let db = TestDbBuilder::new()
195195
.with_file("/src/foo.py", "class Test: pass")
196196
.build()?;
@@ -201,15 +201,15 @@ mod tests {
201201

202202
let class = ast.suite()[0].as_class_def_stmt().unwrap();
203203
let model = SemanticModel::new(&db, foo);
204-
let ty = class.ty(&model);
204+
let ty = class.inferred_type(&model);
205205

206206
assert!(ty.is_class_literal());
207207

208208
Ok(())
209209
}
210210

211211
#[test]
212-
fn alias_ty() -> anyhow::Result<()> {
212+
fn alias_type() -> anyhow::Result<()> {
213213
let db = TestDbBuilder::new()
214214
.with_file("/src/foo.py", "class Test: pass")
215215
.with_file("/src/bar.py", "from foo import Test")
@@ -222,7 +222,7 @@ mod tests {
222222
let import = ast.suite()[0].as_import_from_stmt().unwrap();
223223
let alias = &import.names[0];
224224
let model = SemanticModel::new(&db, bar);
225-
let ty = alias.ty(&model);
225+
let ty = alias.inferred_type(&model);
226226

227227
assert!(ty.is_class_literal());
228228

0 commit comments

Comments
 (0)