mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 22:01:18 +00:00
[ty] Use typing.Self
for the first parameter of instance methods (#20517)
## Summary Modify the (external) signature of instance methods such that the first parameter uses `Self` unless it is explicitly annotated. This allows us to correctly type-check more code, and allows us to infer correct return types for many functions that return `Self`. For example: ```py from pathlib import Path from datetime import datetime, timedelta reveal_type(Path(".config") / ".ty") # now Path, previously Unknown def _(dt: datetime, delta: timedelta): reveal_type(dt - delta) # now datetime, previously Unknown ``` part of https://github.com/astral-sh/ty/issues/159 ## Performance I ran benchmarks locally on `attrs`, `freqtrade` and `colour`, the projects with the largest regressions on CodSpeed. I see much smaller effects locally, but can definitely reproduce the regression on `attrs`. From looking at the profiling results (on Codspeed), it seems that we simply do more type inference work, which seems plausible, given that we now understand much more return types (of many stdlib functions). In particular, whenever a function uses an implicit `self` and returns `Self` (without mentioning `Self` anywhere else in its signature), we will now infer the correct type, whereas we would previously return `Unknown`. This also means that we need to invoke the generics solver in more cases. Comparing half a million lines of log output on attrs, I can see that we do 5% more "work" (number of lines in the log), and have a lot more `apply_specialization` events (7108 vs 4304). On freqtrade, I see similar numbers for `apply_specialization` (11360 vs 5138 calls). Given these results, I'm not sure if it's generally worth doing more performance work, especially since none of the code modifications themselves seem to be likely candidates for regressions. | Command | Mean [ms] | Min [ms] | Max [ms] | Relative | |:---|---:|---:|---:|---:| | `./ty_main check /home/shark/ecosystem/attrs` | 92.6 ± 3.6 | 85.9 | 102.6 | 1.00 | | `./ty_self check /home/shark/ecosystem/attrs` | 101.7 ± 3.5 | 96.9 | 113.8 | 1.10 ± 0.06 | | Command | Mean [ms] | Min [ms] | Max [ms] | Relative | |:---|---:|---:|---:|---:| | `./ty_main check /home/shark/ecosystem/freqtrade` | 599.0 ± 20.2 | 568.2 | 627.5 | 1.00 | | `./ty_self check /home/shark/ecosystem/freqtrade` | 607.9 ± 11.5 | 594.9 | 626.4 | 1.01 ± 0.04 | | Command | Mean [ms] | Min [ms] | Max [ms] | Relative | |:---|---:|---:|---:|---:| | `./ty_main check /home/shark/ecosystem/colour` | 423.9 ± 17.9 | 394.6 | 447.4 | 1.00 | | `./ty_self check /home/shark/ecosystem/colour` | 426.9 ± 24.9 | 373.8 | 456.6 | 1.01 ± 0.07 | ## Test Plan New Markdown tests ## Ecosystem report * apprise: ~300 new diagnostics related to problematic stubs in apprise 😩 * attrs: a new true positive, since [this function](4e2c89c823/tests/test_make.py (L2135)
) is missing a `@staticmethod`? * Some legitimate true positives * sympy: lots of new `invalid-operator` false positives in [matrix multiplication](cf9f4b6805/sympy/matrices/matrixbase.py (L3267-L3269)
) due to our limited understanding of [generic `Callable[[Callable[[T1, T2], T3]], Callable[[T1, T2], T3]]` "identity" types](cf9f4b6805/sympy/core/decorators.py (L83-L84)
) of decorators. This is not related to type-of-self. ## Typing conformance results The changes are all correct, except for ```diff +generics_self_usage.py:50:5: error[invalid-assignment] Object of type `def foo(self) -> int` is not assignable to `(typing.Self, /) -> int` ``` which is related to an assignability problem involving type variables on both sides: ```py class CallableAttribute: def foo(self) -> int: return 0 bar: Callable[[Self], int] = foo # <- we currently error on this assignment ``` --------- Co-authored-by: Shaygan Hooshyari <sh.hooshyari@gmail.com>
This commit is contained in:
parent
1d3e4a9153
commit
0092794302
23 changed files with 555 additions and 131 deletions
|
@ -52,7 +52,8 @@ use crate::types::function::{
|
|||
DataclassTransformerParams, FunctionSpans, FunctionType, KnownFunction,
|
||||
};
|
||||
use crate::types::generics::{
|
||||
GenericContext, PartialSpecialization, Specialization, bind_typevar, walk_generic_context,
|
||||
GenericContext, PartialSpecialization, Specialization, bind_typevar, typing_self,
|
||||
walk_generic_context,
|
||||
};
|
||||
pub use crate::types::ide_support::{
|
||||
CallSignatureDetails, Member, MemberWithDefinition, all_members, call_signature_details,
|
||||
|
@ -5355,12 +5356,10 @@ impl<'db> Type<'db> {
|
|||
Some(generic_context) => (
|
||||
Some(class),
|
||||
Some(generic_context),
|
||||
Type::from(class.apply_specialization(db, |_| {
|
||||
// It is important that identity_specialization specializes the class with
|
||||
// _inferable_ typevars, so that our specialization inference logic will
|
||||
// try to find a specialization for them.
|
||||
generic_context.identity_specialization(db)
|
||||
})),
|
||||
// It is important that identity_specialization specializes the class with
|
||||
// _inferable_ typevars, so that our specialization inference logic will
|
||||
// try to find a specialization for them.
|
||||
Type::from(class.identity_specialization(db, &Type::TypeVar)),
|
||||
),
|
||||
_ => (None, None, self),
|
||||
},
|
||||
|
@ -5717,39 +5716,13 @@ impl<'db> Type<'db> {
|
|||
});
|
||||
};
|
||||
|
||||
let upper_bound = Type::instance(
|
||||
Ok(typing_self(
|
||||
db,
|
||||
class.apply_specialization(db, |generic_context| {
|
||||
let types = generic_context
|
||||
.variables(db)
|
||||
.iter()
|
||||
.map(|typevar| Type::NonInferableTypeVar(*typevar));
|
||||
|
||||
generic_context.specialize(db, types.collect())
|
||||
}),
|
||||
);
|
||||
|
||||
let class_definition = class.definition(db);
|
||||
let typevar = TypeVarInstance::new(
|
||||
db,
|
||||
ast::name::Name::new_static("Self"),
|
||||
Some(class_definition),
|
||||
Some(TypeVarBoundOrConstraints::UpperBound(upper_bound).into()),
|
||||
// According to the [spec], we can consider `Self`
|
||||
// equivalent to an invariant type variable
|
||||
// [spec]: https://typing.python.org/en/latest/spec/generics.html#self
|
||||
Some(TypeVarVariance::Invariant),
|
||||
None,
|
||||
TypeVarKind::TypingSelf,
|
||||
);
|
||||
Ok(bind_typevar(
|
||||
db,
|
||||
index,
|
||||
scope_id.file_scope_id(db),
|
||||
scope_id,
|
||||
typevar_binding_context,
|
||||
typevar,
|
||||
class,
|
||||
&Type::NonInferableTypeVar,
|
||||
)
|
||||
.map(Type::NonInferableTypeVar)
|
||||
.unwrap_or(*self))
|
||||
}
|
||||
SpecialFormType::TypeAlias => Ok(Type::Dynamic(DynamicType::TodoTypeAlias)),
|
||||
|
|
|
@ -376,8 +376,8 @@ pub enum ClassType<'db> {
|
|||
|
||||
#[salsa::tracked]
|
||||
impl<'db> ClassType<'db> {
|
||||
pub(super) const fn is_not_generic(self) -> bool {
|
||||
matches!(self, Self::NonGeneric(_))
|
||||
pub(super) const fn is_generic(self) -> bool {
|
||||
matches!(self, Self::Generic(_))
|
||||
}
|
||||
|
||||
pub(super) const fn into_generic_alias(self) -> Option<GenericAlias<'db>> {
|
||||
|
@ -1527,6 +1527,18 @@ impl<'db> ClassLiteral<'db> {
|
|||
})
|
||||
}
|
||||
|
||||
/// Returns a specialization of this class where each typevar is mapped to itself. The second
|
||||
/// parameter can be `Type::TypeVar` or `Type::NonInferableTypeVar`, depending on the use case.
|
||||
pub(crate) fn identity_specialization(
|
||||
self,
|
||||
db: &'db dyn Db,
|
||||
typevar_to_type: &impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>,
|
||||
) -> ClassType<'db> {
|
||||
self.apply_specialization(db, |generic_context| {
|
||||
generic_context.identity_specialization(db, typevar_to_type)
|
||||
})
|
||||
}
|
||||
|
||||
/// Return an iterator over the inferred types of this class's *explicit* bases.
|
||||
///
|
||||
/// Note that any class (except for `object`) that has no explicit
|
||||
|
@ -2625,7 +2637,14 @@ impl<'db> ClassLiteral<'db> {
|
|||
.map_type(|ty|
|
||||
ty.apply_type_mapping(
|
||||
db,
|
||||
&TypeMapping::ReplaceSelf {new_upper_bound: determine_upper_bound(db, self, specialization, ClassBase::is_typed_dict) }
|
||||
&TypeMapping::ReplaceSelf {
|
||||
new_upper_bound: determine_upper_bound(
|
||||
db,
|
||||
self,
|
||||
specialization,
|
||||
ClassBase::is_typed_dict
|
||||
)
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
@ -4174,6 +4193,91 @@ impl KnownClass {
|
|||
}
|
||||
}
|
||||
|
||||
/// Return `true` if this class is a typeshed fallback class which is used to provide attributes and
|
||||
/// methods for another type (e.g. `NamedTupleFallback` for actual `NamedTuple`s). These fallback
|
||||
/// classes need special treatment in some places. For example, implicit usages of `Self` should not
|
||||
/// be eagerly replaced with the fallback class itself. Instead, `Self` should eventually be treated
|
||||
/// as referring to the destination type (e.g. the actual `NamedTuple`).
|
||||
pub(crate) const fn is_fallback_class(self) -> bool {
|
||||
match self {
|
||||
KnownClass::Bool
|
||||
| KnownClass::Object
|
||||
| KnownClass::Bytes
|
||||
| KnownClass::Bytearray
|
||||
| KnownClass::Type
|
||||
| KnownClass::Int
|
||||
| KnownClass::Float
|
||||
| KnownClass::Complex
|
||||
| KnownClass::Str
|
||||
| KnownClass::List
|
||||
| KnownClass::Tuple
|
||||
| KnownClass::Set
|
||||
| KnownClass::FrozenSet
|
||||
| KnownClass::Dict
|
||||
| KnownClass::Slice
|
||||
| KnownClass::Property
|
||||
| KnownClass::BaseException
|
||||
| KnownClass::Exception
|
||||
| KnownClass::BaseExceptionGroup
|
||||
| KnownClass::ExceptionGroup
|
||||
| KnownClass::Staticmethod
|
||||
| KnownClass::Classmethod
|
||||
| KnownClass::Super
|
||||
| KnownClass::Enum
|
||||
| KnownClass::EnumType
|
||||
| KnownClass::Auto
|
||||
| KnownClass::Member
|
||||
| KnownClass::Nonmember
|
||||
| KnownClass::StrEnum
|
||||
| KnownClass::ABCMeta
|
||||
| KnownClass::GenericAlias
|
||||
| KnownClass::ModuleType
|
||||
| KnownClass::FunctionType
|
||||
| KnownClass::MethodType
|
||||
| KnownClass::MethodWrapperType
|
||||
| KnownClass::WrapperDescriptorType
|
||||
| KnownClass::UnionType
|
||||
| KnownClass::GeneratorType
|
||||
| KnownClass::AsyncGeneratorType
|
||||
| KnownClass::CoroutineType
|
||||
| KnownClass::NotImplementedType
|
||||
| KnownClass::BuiltinFunctionType
|
||||
| KnownClass::EllipsisType
|
||||
| KnownClass::NoneType
|
||||
| KnownClass::Awaitable
|
||||
| KnownClass::Generator
|
||||
| KnownClass::Deprecated
|
||||
| KnownClass::StdlibAlias
|
||||
| KnownClass::SpecialForm
|
||||
| KnownClass::TypeVar
|
||||
| KnownClass::ParamSpec
|
||||
| KnownClass::ParamSpecArgs
|
||||
| KnownClass::ParamSpecKwargs
|
||||
| KnownClass::ProtocolMeta
|
||||
| KnownClass::TypeVarTuple
|
||||
| KnownClass::TypeAliasType
|
||||
| KnownClass::NoDefaultType
|
||||
| KnownClass::NewType
|
||||
| KnownClass::SupportsIndex
|
||||
| KnownClass::Iterable
|
||||
| KnownClass::Iterator
|
||||
| KnownClass::ChainMap
|
||||
| KnownClass::Counter
|
||||
| KnownClass::DefaultDict
|
||||
| KnownClass::Deque
|
||||
| KnownClass::OrderedDict
|
||||
| KnownClass::VersionInfo
|
||||
| KnownClass::Field
|
||||
| KnownClass::KwOnly
|
||||
| KnownClass::NamedTupleLike
|
||||
| KnownClass::Template
|
||||
| KnownClass::Path
|
||||
| KnownClass::ConstraintSet
|
||||
| KnownClass::InitVar => false,
|
||||
KnownClass::NamedTupleFallback | KnownClass::TypedDictFallback => true,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn name(self, db: &dyn Db) -> &'static str {
|
||||
match self {
|
||||
Self::Bool => "bool",
|
||||
|
|
|
@ -1209,11 +1209,13 @@ impl Display for DisplayParameter<'_> {
|
|||
if let Some(name) = self.param.display_name() {
|
||||
f.write_str(&name)?;
|
||||
if let Some(annotated_type) = self.param.annotated_type() {
|
||||
write!(
|
||||
f,
|
||||
": {}",
|
||||
annotated_type.display_with(self.db, self.settings.clone())
|
||||
)?;
|
||||
if self.param.should_annotation_be_displayed() {
|
||||
write!(
|
||||
f,
|
||||
": {}",
|
||||
annotated_type.display_with(self.db, self.settings.clone())
|
||||
)?;
|
||||
}
|
||||
}
|
||||
// Default value can only be specified if `name` is given.
|
||||
if let Some(default_ty) = self.param.default_type() {
|
||||
|
|
|
@ -4,9 +4,9 @@ use itertools::Itertools;
|
|||
use ruff_python_ast as ast;
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use crate::semantic_index::SemanticIndex;
|
||||
use crate::semantic_index::definition::Definition;
|
||||
use crate::semantic_index::scope::{FileScopeId, NodeWithScopeKind};
|
||||
use crate::semantic_index::scope::{FileScopeId, NodeWithScopeKind, ScopeId};
|
||||
use crate::semantic_index::{SemanticIndex, semantic_index};
|
||||
use crate::types::class::ClassType;
|
||||
use crate::types::class_base::ClassBase;
|
||||
use crate::types::infer::infer_definition_types;
|
||||
|
@ -14,10 +14,10 @@ use crate::types::instance::{Protocol, ProtocolInstanceType};
|
|||
use crate::types::signatures::{Parameter, Parameters, Signature};
|
||||
use crate::types::tuple::{TupleSpec, TupleType, walk_tuple_type};
|
||||
use crate::types::{
|
||||
ApplyTypeMappingVisitor, BoundTypeVarInstance, FindLegacyTypeVarsVisitor, HasRelationToVisitor,
|
||||
IsEquivalentVisitor, KnownClass, KnownInstanceType, MaterializationKind, NormalizedVisitor,
|
||||
Type, TypeMapping, TypeRelation, TypeVarBoundOrConstraints, TypeVarInstance, TypeVarKind,
|
||||
TypeVarVariance, UnionType, binding_type, declaration_type,
|
||||
ApplyTypeMappingVisitor, BoundTypeVarInstance, ClassLiteral, FindLegacyTypeVarsVisitor,
|
||||
HasRelationToVisitor, IsEquivalentVisitor, KnownClass, KnownInstanceType, MaterializationKind,
|
||||
NormalizedVisitor, Type, TypeMapping, TypeRelation, TypeVarBoundOrConstraints, TypeVarInstance,
|
||||
TypeVarKind, TypeVarVariance, UnionType, binding_type, declaration_type,
|
||||
};
|
||||
use crate::{Db, FxOrderSet};
|
||||
|
||||
|
@ -98,6 +98,45 @@ 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,
|
||||
typevar_binding_context: Option<Definition<'db>>,
|
||||
class: ClassLiteral<'db>,
|
||||
typevar_to_type: &impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>,
|
||||
) -> Option<Type<'db>> {
|
||||
let index = semantic_index(db, scope_id.file(db));
|
||||
|
||||
let typevar = TypeVarInstance::new(
|
||||
db,
|
||||
ast::name::Name::new_static("Self"),
|
||||
Some(class.definition(db)),
|
||||
Some(
|
||||
TypeVarBoundOrConstraints::UpperBound(Type::instance(
|
||||
db,
|
||||
class.identity_specialization(db, typevar_to_type),
|
||||
))
|
||||
.into(),
|
||||
),
|
||||
// According to the [spec], we can consider `Self`
|
||||
// equivalent to an invariant type variable
|
||||
// [spec]: https://typing.python.org/en/latest/spec/generics.html#self
|
||||
Some(TypeVarVariance::Invariant),
|
||||
None,
|
||||
TypeVarKind::TypingSelf,
|
||||
);
|
||||
|
||||
bind_typevar(
|
||||
db,
|
||||
index,
|
||||
scope_id.file_scope_id(db),
|
||||
typevar_binding_context,
|
||||
typevar,
|
||||
)
|
||||
.map(typevar_to_type)
|
||||
}
|
||||
|
||||
/// A list of formal type variables for a generic function, class, or type alias.
|
||||
///
|
||||
/// TODO: Handle nested generic contexts better, with actual parent links to the lexically
|
||||
|
@ -286,14 +325,17 @@ impl<'db> GenericContext<'db> {
|
|||
}
|
||||
|
||||
/// Returns a specialization of this generic context where each typevar is mapped to itself.
|
||||
/// (And in particular, to an _inferable_ version of itself, since this will be used in our
|
||||
/// class constructor invocation machinery to infer a specialization for the class from the
|
||||
/// arguments passed to its constructor.)
|
||||
pub(crate) fn identity_specialization(self, db: &'db dyn Db) -> Specialization<'db> {
|
||||
/// The second parameter can be `Type::TypeVar` or `Type::NonInferableTypeVar`, depending on
|
||||
/// the use case.
|
||||
pub(crate) fn identity_specialization(
|
||||
self,
|
||||
db: &'db dyn Db,
|
||||
typevar_to_type: &impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>,
|
||||
) -> Specialization<'db> {
|
||||
let types = self
|
||||
.variables(db)
|
||||
.iter()
|
||||
.map(|typevar| Type::TypeVar(*typevar))
|
||||
.map(|typevar| typevar_to_type(*typevar))
|
||||
.collect();
|
||||
self.specialize(db, types)
|
||||
}
|
||||
|
|
|
@ -5950,7 +5950,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
|||
// but constructor calls to `tuple[int]`, `tuple[int, ...]`, `tuple[int, *tuple[str, ...]]` (etc.)
|
||||
// are handled by the default constructor-call logic (we synthesize a `__new__` method for them
|
||||
// in `ClassType::own_class_member()`).
|
||||
class.is_known(self.db(), KnownClass::Tuple) && class.is_not_generic()
|
||||
class.is_known(self.db(), KnownClass::Tuple) && !class.is_generic()
|
||||
);
|
||||
|
||||
// temporary special-casing for all subclasses of `enum.Enum`
|
||||
|
|
|
@ -13,20 +13,58 @@
|
|||
use std::{collections::HashMap, slice::Iter};
|
||||
|
||||
use itertools::{EitherOrBoth, Itertools};
|
||||
use ruff_python_ast::ParameterWithDefault;
|
||||
use smallvec::{SmallVec, smallvec_inline};
|
||||
|
||||
use super::{DynamicType, Type, TypeVarVariance, definition_expression_type};
|
||||
use super::{
|
||||
DynamicType, Type, TypeVarVariance, definition_expression_type, infer_definition_types,
|
||||
semantic_index,
|
||||
};
|
||||
use crate::semantic_index::definition::Definition;
|
||||
use crate::types::constraints::{ConstraintSet, IteratorConstraintsExtension};
|
||||
use crate::types::generics::{GenericContext, walk_generic_context};
|
||||
use crate::types::function::FunctionType;
|
||||
use crate::types::generics::{GenericContext, typing_self, walk_generic_context};
|
||||
use crate::types::infer::nearest_enclosing_class;
|
||||
use crate::types::{
|
||||
ApplyTypeMappingVisitor, BindingContext, BoundTypeVarInstance, FindLegacyTypeVarsVisitor,
|
||||
HasRelationToVisitor, IsEquivalentVisitor, KnownClass, MaterializationKind, NormalizedVisitor,
|
||||
TypeMapping, TypeRelation, VarianceInferable, todo_type,
|
||||
ApplyTypeMappingVisitor, BindingContext, BoundTypeVarInstance, ClassType,
|
||||
FindLegacyTypeVarsVisitor, HasRelationToVisitor, IsEquivalentVisitor, KnownClass,
|
||||
MaterializationKind, NormalizedVisitor, TypeMapping, TypeRelation, VarianceInferable,
|
||||
todo_type,
|
||||
};
|
||||
use crate::{Db, FxOrderSet};
|
||||
use ruff_python_ast::{self as ast, name::Name};
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct MethodInformation<'db> {
|
||||
method: FunctionType<'db>,
|
||||
class: ClassType<'db>,
|
||||
}
|
||||
|
||||
fn infer_method_information<'db>(
|
||||
db: &'db dyn Db,
|
||||
definition: Definition<'db>,
|
||||
) -> Option<MethodInformation<'db>> {
|
||||
let class_scope_id = definition.scope(db);
|
||||
let file = class_scope_id.file(db);
|
||||
let index = semantic_index(db, file);
|
||||
|
||||
let class_scope = index.scope(class_scope_id.file_scope_id(db));
|
||||
let class_node = class_scope.node().as_class()?;
|
||||
|
||||
let method = infer_definition_types(db, definition)
|
||||
.declaration_type(definition)
|
||||
.inner_type()
|
||||
.into_function_literal()?;
|
||||
|
||||
let class_def = index.expect_single_definition(class_node);
|
||||
let class_literal = infer_definition_types(db, class_def)
|
||||
.declaration_type(class_def)
|
||||
.inner_type();
|
||||
let class = class_literal.to_class_type(db)?;
|
||||
|
||||
Some(MethodInformation { method, class })
|
||||
}
|
||||
|
||||
/// The signature of a single callable. If the callable is overloaded, there is a separate
|
||||
/// [`Signature`] for each overload.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, salsa::Update, get_size2::GetSize)]
|
||||
|
@ -1185,16 +1223,72 @@ impl<'db> Parameters<'db> {
|
|||
.map(|default| definition_expression_type(db, definition, default))
|
||||
};
|
||||
|
||||
let method_info = infer_method_information(db, definition);
|
||||
let is_static_or_classmethod = method_info
|
||||
.is_some_and(|f| f.method.is_staticmethod(db) || f.method.is_classmethod(db));
|
||||
|
||||
let inferred_annotation = |arg: &ParameterWithDefault| {
|
||||
if let Some(MethodInformation { method, class }) = method_info
|
||||
&& !is_static_or_classmethod
|
||||
&& arg.parameter.annotation().is_none()
|
||||
&& parameters.index(arg.name().id()) == Some(0)
|
||||
{
|
||||
let method_has_self_in_generic_context =
|
||||
method.signature(db).overloads.iter().any(|s| {
|
||||
s.generic_context.is_some_and(|context| {
|
||||
context
|
||||
.variables(db)
|
||||
.iter()
|
||||
.any(|v| v.typevar(db).is_self(db))
|
||||
})
|
||||
});
|
||||
|
||||
if method_has_self_in_generic_context
|
||||
|| class.is_generic()
|
||||
|| class.known(db).is_some_and(KnownClass::is_fallback_class)
|
||||
{
|
||||
let scope_id = definition.scope(db);
|
||||
let typevar_binding_context = Some(definition);
|
||||
let index = semantic_index(db, scope_id.file(db));
|
||||
let class = nearest_enclosing_class(db, index, scope_id).unwrap();
|
||||
|
||||
Some(
|
||||
typing_self(db, scope_id, typevar_binding_context, class, &Type::TypeVar)
|
||||
.expect("We should always find the surrounding class for an implicit self: Self annotation"),
|
||||
)
|
||||
} else {
|
||||
// For methods of non-generic classes that are not otherwise generic (e.g. return `Self` or
|
||||
// have additional type parameters), the implicit `Self` type of the `self` parameter would
|
||||
// be the only type variable, so we can just use the class directly.
|
||||
Some(Type::instance(db, class))
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
let pos_only_param = |param: &ast::ParameterWithDefault| {
|
||||
Parameter::from_node_and_kind(
|
||||
db,
|
||||
definition,
|
||||
¶m.parameter,
|
||||
ParameterKind::PositionalOnly {
|
||||
name: Some(param.parameter.name.id.clone()),
|
||||
default_type: default_type(param),
|
||||
},
|
||||
)
|
||||
if let Some(inferred_annotation_type) = inferred_annotation(param) {
|
||||
Parameter {
|
||||
annotated_type: Some(inferred_annotation_type),
|
||||
inferred_annotation: true,
|
||||
kind: ParameterKind::PositionalOnly {
|
||||
name: Some(param.parameter.name.id.clone()),
|
||||
default_type: default_type(param),
|
||||
},
|
||||
form: ParameterForm::Value,
|
||||
}
|
||||
} else {
|
||||
Parameter::from_node_and_kind(
|
||||
db,
|
||||
definition,
|
||||
¶m.parameter,
|
||||
ParameterKind::PositionalOnly {
|
||||
name: Some(param.parameter.name.id.clone()),
|
||||
default_type: default_type(param),
|
||||
},
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let mut positional_only: Vec<Parameter> = posonlyargs.iter().map(pos_only_param).collect();
|
||||
|
@ -1218,15 +1312,27 @@ impl<'db> Parameters<'db> {
|
|||
}
|
||||
|
||||
let positional_or_keyword = pos_or_keyword_iter.map(|arg| {
|
||||
Parameter::from_node_and_kind(
|
||||
db,
|
||||
definition,
|
||||
&arg.parameter,
|
||||
ParameterKind::PositionalOrKeyword {
|
||||
name: arg.parameter.name.id.clone(),
|
||||
default_type: default_type(arg),
|
||||
},
|
||||
)
|
||||
if let Some(inferred_annotation_type) = inferred_annotation(arg) {
|
||||
Parameter {
|
||||
annotated_type: Some(inferred_annotation_type),
|
||||
inferred_annotation: true,
|
||||
kind: ParameterKind::PositionalOrKeyword {
|
||||
name: arg.parameter.name.id.clone(),
|
||||
default_type: default_type(arg),
|
||||
},
|
||||
form: ParameterForm::Value,
|
||||
}
|
||||
} else {
|
||||
Parameter::from_node_and_kind(
|
||||
db,
|
||||
definition,
|
||||
&arg.parameter,
|
||||
ParameterKind::PositionalOrKeyword {
|
||||
name: arg.parameter.name.id.clone(),
|
||||
default_type: default_type(arg),
|
||||
},
|
||||
)
|
||||
}
|
||||
});
|
||||
|
||||
let variadic = vararg.as_ref().map(|arg| {
|
||||
|
@ -1399,6 +1505,10 @@ pub(crate) struct Parameter<'db> {
|
|||
/// Annotated type of the parameter.
|
||||
annotated_type: Option<Type<'db>>,
|
||||
|
||||
/// Does the type of this parameter come from an explicit annotation, or was it inferred from
|
||||
/// the context, like `Self` for the `self` parameter of instance methods.
|
||||
inferred_annotation: bool,
|
||||
|
||||
kind: ParameterKind<'db>,
|
||||
pub(crate) form: ParameterForm,
|
||||
}
|
||||
|
@ -1407,6 +1517,7 @@ impl<'db> Parameter<'db> {
|
|||
pub(crate) fn positional_only(name: Option<Name>) -> Self {
|
||||
Self {
|
||||
annotated_type: None,
|
||||
inferred_annotation: false,
|
||||
kind: ParameterKind::PositionalOnly {
|
||||
name,
|
||||
default_type: None,
|
||||
|
@ -1418,6 +1529,7 @@ impl<'db> Parameter<'db> {
|
|||
pub(crate) fn positional_or_keyword(name: Name) -> Self {
|
||||
Self {
|
||||
annotated_type: None,
|
||||
inferred_annotation: false,
|
||||
kind: ParameterKind::PositionalOrKeyword {
|
||||
name,
|
||||
default_type: None,
|
||||
|
@ -1429,6 +1541,7 @@ impl<'db> Parameter<'db> {
|
|||
pub(crate) fn variadic(name: Name) -> Self {
|
||||
Self {
|
||||
annotated_type: None,
|
||||
inferred_annotation: false,
|
||||
kind: ParameterKind::Variadic { name },
|
||||
form: ParameterForm::Value,
|
||||
}
|
||||
|
@ -1437,6 +1550,7 @@ impl<'db> Parameter<'db> {
|
|||
pub(crate) fn keyword_only(name: Name) -> Self {
|
||||
Self {
|
||||
annotated_type: None,
|
||||
inferred_annotation: false,
|
||||
kind: ParameterKind::KeywordOnly {
|
||||
name,
|
||||
default_type: None,
|
||||
|
@ -1448,6 +1562,7 @@ impl<'db> Parameter<'db> {
|
|||
pub(crate) fn keyword_variadic(name: Name) -> Self {
|
||||
Self {
|
||||
annotated_type: None,
|
||||
inferred_annotation: false,
|
||||
kind: ParameterKind::KeywordVariadic { name },
|
||||
form: ParameterForm::Value,
|
||||
}
|
||||
|
@ -1486,6 +1601,7 @@ impl<'db> Parameter<'db> {
|
|||
.annotated_type
|
||||
.map(|ty| ty.apply_type_mapping_impl(db, type_mapping, visitor)),
|
||||
kind: self.kind.apply_type_mapping_impl(db, type_mapping, visitor),
|
||||
inferred_annotation: self.inferred_annotation,
|
||||
form: self.form,
|
||||
}
|
||||
}
|
||||
|
@ -1501,6 +1617,7 @@ impl<'db> Parameter<'db> {
|
|||
) -> Self {
|
||||
let Parameter {
|
||||
annotated_type,
|
||||
inferred_annotation,
|
||||
kind,
|
||||
form,
|
||||
} = self;
|
||||
|
@ -1544,6 +1661,7 @@ impl<'db> Parameter<'db> {
|
|||
|
||||
Self {
|
||||
annotated_type: Some(annotated_type),
|
||||
inferred_annotation: *inferred_annotation,
|
||||
kind,
|
||||
form: *form,
|
||||
}
|
||||
|
@ -1566,6 +1684,7 @@ impl<'db> Parameter<'db> {
|
|||
}),
|
||||
kind,
|
||||
form: ParameterForm::Value,
|
||||
inferred_annotation: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1620,6 +1739,11 @@ impl<'db> Parameter<'db> {
|
|||
&self.kind
|
||||
}
|
||||
|
||||
/// Whether or not the type of this parameter should be displayed.
|
||||
pub(crate) fn should_annotation_be_displayed(&self) -> bool {
|
||||
!self.inferred_annotation
|
||||
}
|
||||
|
||||
/// Name of the parameter (if it has one).
|
||||
pub(crate) fn name(&self) -> Option<&ast::name::Name> {
|
||||
match &self.kind {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue