mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-20 04:29:47 +00:00
[ty] Infer type for implicit self parameters in method bodies (#20922)
## Summary Infer a type of `Self` for unannotated `self` parameters in methods of classes. part of https://github.com/astral-sh/ty/issues/159 closes https://github.com/astral-sh/ty/issues/1081 ## Conformance tests changes ```diff +enums_member_values.py:85:9: error[invalid-assignment] Object of type `int` is not assignable to attribute `_value_` of type `str` ``` A true positive ✔️ ```diff -generics_self_advanced.py:35:9: error[type-assertion-failure] Argument does not have asserted type `Self@method2` -generics_self_basic.py:14:9: error[type-assertion-failure] Argument does not have asserted type `Self@set_scale ``` Two false positives going away ✔️ ```diff +generics_syntax_infer_variance.py:82:9: error[invalid-assignment] Cannot assign to final attribute `x` on type `Self@__init__` ``` This looks like a true positive to me, even if it's not marked with `# E` ✔️ ```diff +protocols_explicit.py:56:9: error[invalid-assignment] Object of type `tuple[int, int, str]` is not assignable to attribute `rgb` of type `tuple[int, int, int]` ``` True positive ✔️ ``` +protocols_explicit.py:85:9: error[invalid-attribute-access] Cannot assign to ClassVar `cm1` from an instance of type `Self@__init__` ``` This looks like a true positive to me, even if it's not marked with `# E`. But this is consistent with our understanding of `ClassVar`, I think. ✔️ ```py +qualifiers_final_annotation.py:52:9: error[invalid-assignment] Cannot assign to final attribute `ID4` on type `Self@__init__` +qualifiers_final_annotation.py:65:9: error[invalid-assignment] Cannot assign to final attribute `ID7` on type `Self@method1` ``` New true positives ✔️ ```py +qualifiers_final_annotation.py:52:9: error[invalid-assignment] Cannot assign to final attribute `ID4` on type `Self@__init__` +qualifiers_final_annotation.py:57:13: error[invalid-assignment] Cannot assign to final attribute `ID6` on type `Self@__init__` +qualifiers_final_annotation.py:59:13: error[invalid-assignment] Cannot assign to final attribute `ID6` on type `Self@__init__` ``` This is a new false positive, but that's a pre-existing issue on main (if you annotate with `Self`): https://play.ty.dev/3ee1c56d-7e13-43bb-811a-7a81e236e6ab ❌ => reported as https://github.com/astral-sh/ty/issues/1409 ## Ecosystem * There are 5931 new `unresolved-attribute` and 3292 new `possibly-missing-attribute` attribute errors, way too many to look at all of them. I randomly sampled 15 of these errors and found: * 13 instances where there was simply no such attribute that we could plausibly see. Sometimes [I didn't find it anywhere](8644d886c6/openlibrary/plugins/openlibrary/tests/test_listapi.py (L33)). Sometimes it was set externally on the object. Sometimes there was some [`setattr` dynamicness going on](a49f6b927d/setuptools/wheel.py (L88-L94)). I would consider all of them to be true positives. * 1 instance where [attribute was set on `obj` in `__new__`](9e87b44fd4/sympy/tensor/array/array_comprehension.py (L45C1-L45C36)), which we don't support yet * 1 instance [where the attribute was defined via `__slots__` ](e250ec0fc8/lib/spack/spack/vendor/pyrsistent/_pdeque.py (L48C5-L48C14)) * I see 44 instances [of the false positive above](https://github.com/astral-sh/ty/issues/1409) with `Final` instance attributes being set in `__init__`. I don't think this should block this PR. ## Test Plan New Markdown tests. --------- Co-authored-by: Shaygan Hooshyari <sh.hooshyari@gmail.com>
This commit is contained in:
parent
76a55314e4
commit
589e8ac0d9
16 changed files with 325 additions and 210 deletions
|
|
@ -206,7 +206,7 @@ enum ReduceResult<'db> {
|
|||
//
|
||||
// For now (until we solve https://github.com/astral-sh/ty/issues/957), keep this number
|
||||
// below 200, which is the salsa fixpoint iteration limit.
|
||||
const MAX_UNION_LITERALS: usize = 199;
|
||||
const MAX_UNION_LITERALS: usize = 190;
|
||||
|
||||
pub(crate) struct UnionBuilder<'db> {
|
||||
elements: Vec<UnionElement<'db>>,
|
||||
|
|
|
|||
|
|
@ -80,11 +80,11 @@ pub(crate) fn bind_typevar<'db>(
|
|||
/// Create a `typing.Self` type variable for a given class.
|
||||
pub(crate) fn typing_self<'db>(
|
||||
db: &'db dyn Db,
|
||||
scope_id: ScopeId,
|
||||
function_scope_id: ScopeId,
|
||||
typevar_binding_context: Option<Definition<'db>>,
|
||||
class: ClassLiteral<'db>,
|
||||
) -> Option<Type<'db>> {
|
||||
let index = semantic_index(db, scope_id.file(db));
|
||||
let index = semantic_index(db, function_scope_id.file(db));
|
||||
|
||||
let identity = TypeVarIdentity::new(
|
||||
db,
|
||||
|
|
@ -110,7 +110,7 @@ pub(crate) fn typing_self<'db>(
|
|||
bind_typevar(
|
||||
db,
|
||||
index,
|
||||
scope_id.file_scope_id(db),
|
||||
function_scope_id.file_scope_id(db),
|
||||
typevar_binding_context,
|
||||
typevar,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ use crate::types::function::{
|
|||
};
|
||||
use crate::types::generics::{
|
||||
GenericContext, InferableTypeVars, LegacyGenericBase, SpecializationBuilder, bind_typevar,
|
||||
enclosing_generic_contexts,
|
||||
enclosing_generic_contexts, typing_self,
|
||||
};
|
||||
use crate::types::infer::nearest_enclosing_function;
|
||||
use crate::types::instance::SliceLiteral;
|
||||
|
|
@ -2495,6 +2495,8 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
|||
} else {
|
||||
let ty = if let Some(default_ty) = default_ty {
|
||||
UnionType::from_elements(self.db(), [Type::unknown(), default_ty])
|
||||
} else if let Some(ty) = self.special_first_method_parameter_type(parameter) {
|
||||
ty
|
||||
} else {
|
||||
Type::unknown()
|
||||
};
|
||||
|
|
@ -2535,6 +2537,65 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Special case for unannotated `cls` and `self` arguments to class methods and instance methods.
|
||||
fn special_first_method_parameter_type(
|
||||
&mut self,
|
||||
parameter: &ast::Parameter,
|
||||
) -> Option<Type<'db>> {
|
||||
let db = self.db();
|
||||
let file = self.file();
|
||||
|
||||
let function_scope_id = self.scope();
|
||||
let function_scope = function_scope_id.scope(db);
|
||||
let function = function_scope.node().as_function()?;
|
||||
|
||||
let parent_file_scope_id = function_scope.parent()?;
|
||||
let mut parent_scope_id = parent_file_scope_id.to_scope_id(db, file);
|
||||
|
||||
// Skip type parameter scopes, if the method itself is generic.
|
||||
if parent_scope_id.is_annotation(db) {
|
||||
let parent_scope = parent_scope_id.scope(db);
|
||||
parent_scope_id = parent_scope.parent()?.to_scope_id(db, file);
|
||||
}
|
||||
|
||||
// Return early if this is not a method inside a class.
|
||||
let class = parent_scope_id.scope(db).node().as_class()?;
|
||||
|
||||
let method_definition = self.index.expect_single_definition(function);
|
||||
let DefinitionKind::Function(function_definition) = method_definition.kind(db) else {
|
||||
return None;
|
||||
};
|
||||
|
||||
if function_definition
|
||||
.node(self.module())
|
||||
.parameters
|
||||
.index(parameter.name())
|
||||
.is_none_or(|index| index != 0)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
|
||||
let method = infer_definition_types(db, method_definition)
|
||||
.declaration_type(method_definition)
|
||||
.inner_type()
|
||||
.as_function_literal()?;
|
||||
|
||||
if method.is_classmethod(db) {
|
||||
// TODO: set the type for `cls` argument
|
||||
return None;
|
||||
} else if method.is_staticmethod(db) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let class_definition = self.index.expect_single_definition(class);
|
||||
let class_literal = infer_definition_types(db, class_definition)
|
||||
.declaration_type(class_definition)
|
||||
.inner_type()
|
||||
.as_class_literal()?;
|
||||
|
||||
typing_self(db, self.scope(), Some(method_definition), class_literal)
|
||||
}
|
||||
|
||||
/// Set initial declared/inferred types for a `*args` variadic positional parameter.
|
||||
///
|
||||
/// The annotated type is implicitly wrapped in a string-keyed dictionary.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue