mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:24:57 +00:00
Display generic function signature properly (#19544)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
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 / test scripts (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 / python package (push) Waiting to run
CI / pre-commit (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 (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
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 / test scripts (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 / python package (push) Waiting to run
CI / pre-commit (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 (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
## Summary Resolves https://github.com/astral-sh/ty/issues/817 ## Test Plan Update mdtest --------- Co-authored-by: Carl Meyer <carl@astral.sh>
This commit is contained in:
parent
5bfffe1aa7
commit
18ad2848e3
7 changed files with 87 additions and 37 deletions
|
@ -5402,7 +5402,7 @@ impl<'db> Type<'db> {
|
|||
Some(TypeVarBoundOrConstraints::UpperBound(instance)),
|
||||
TypeVarVariance::Invariant,
|
||||
None,
|
||||
TypeVarKind::Legacy,
|
||||
TypeVarKind::Implicit,
|
||||
)))
|
||||
}
|
||||
SpecialFormType::TypeAlias => Ok(Type::Dynamic(DynamicType::TodoTypeAlias)),
|
||||
|
@ -5820,7 +5820,7 @@ impl<'db> Type<'db> {
|
|||
) {
|
||||
match self {
|
||||
Type::TypeVar(typevar) => {
|
||||
if typevar.is_legacy(db) {
|
||||
if typevar.is_legacy(db) || typevar.is_implicit(db) {
|
||||
typevars.insert(typevar);
|
||||
}
|
||||
}
|
||||
|
@ -6694,11 +6694,16 @@ impl<'db> FieldInstance<'db> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Whether this typecar was created via the legacy `TypeVar` constructor, or using PEP 695 syntax.
|
||||
/// Whether this typevar was created via the legacy `TypeVar` constructor, using PEP 695 syntax,
|
||||
/// or an implicit typevar like `Self` was used.
|
||||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
||||
pub enum TypeVarKind {
|
||||
/// `T = TypeVar("T")`
|
||||
Legacy,
|
||||
/// `def foo[T](x: T) -> T: ...`
|
||||
Pep695,
|
||||
/// `typing.Self`
|
||||
Implicit,
|
||||
}
|
||||
|
||||
/// Data regarding a single type variable.
|
||||
|
@ -6803,6 +6808,10 @@ impl<'db> TypeVarInstance<'db> {
|
|||
matches!(self.kind(db), TypeVarKind::Legacy)
|
||||
}
|
||||
|
||||
pub(crate) fn is_implicit(self, db: &'db dyn Db) -> bool {
|
||||
matches!(self.kind(db), TypeVarKind::Implicit)
|
||||
}
|
||||
|
||||
pub(crate) fn upper_bound(self, db: &'db dyn Db) -> Option<Type<'db>> {
|
||||
if let Some(TypeVarBoundOrConstraints::UpperBound(ty)) = self.bound_or_constraints(db) {
|
||||
Some(ty)
|
||||
|
|
|
@ -124,16 +124,19 @@ impl Display for DisplayRepresentation<'_> {
|
|||
Type::BoundMethod(bound_method) => {
|
||||
let function = bound_method.function(self.db);
|
||||
|
||||
// TODO: use the specialization from the method. Similar to the comment above
|
||||
// about the function specialization,
|
||||
|
||||
match function.signature(self.db).overloads.as_slice() {
|
||||
[signature] => {
|
||||
let type_parameters = DisplayOptionalGenericContext {
|
||||
generic_context: signature.generic_context.as_ref(),
|
||||
db: self.db,
|
||||
};
|
||||
|
||||
write!(
|
||||
f,
|
||||
"bound method {instance}.{method}{signature}",
|
||||
"bound method {instance}.{method}{type_parameters}{signature}",
|
||||
method = function.name(self.db),
|
||||
instance = bound_method.self_instance(self.db).display(self.db),
|
||||
type_parameters = type_parameters,
|
||||
signature = signature.bind_self().display(self.db)
|
||||
)
|
||||
}
|
||||
|
@ -318,10 +321,16 @@ pub(crate) struct DisplayOverloadLiteral<'db> {
|
|||
impl Display for DisplayOverloadLiteral<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
let signature = self.literal.signature(self.db, None);
|
||||
let type_parameters = DisplayOptionalGenericContext {
|
||||
generic_context: signature.generic_context.as_ref(),
|
||||
db: self.db,
|
||||
};
|
||||
|
||||
write!(
|
||||
f,
|
||||
"def {name}{signature}",
|
||||
"def {name}{type_parameters}{signature}",
|
||||
name = self.literal.name(self.db),
|
||||
type_parameters = type_parameters,
|
||||
signature = signature.display(self.db)
|
||||
)
|
||||
}
|
||||
|
@ -342,15 +351,18 @@ impl Display for DisplayFunctionType<'_> {
|
|||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
let signature = self.ty.signature(self.db);
|
||||
|
||||
// TODO: We should consider adding the type parameters to the signature of a generic
|
||||
// function, i.e. `def foo[T](x: T) -> T`.
|
||||
|
||||
match signature.overloads.as_slice() {
|
||||
[signature] => {
|
||||
let type_parameters = DisplayOptionalGenericContext {
|
||||
generic_context: signature.generic_context.as_ref(),
|
||||
db: self.db,
|
||||
};
|
||||
|
||||
write!(
|
||||
f,
|
||||
"def {name}{signature}",
|
||||
"def {name}{type_parameters}{signature}",
|
||||
name = self.ty.name(self.db),
|
||||
type_parameters = type_parameters,
|
||||
signature = signature.display(self.db)
|
||||
)
|
||||
}
|
||||
|
@ -404,21 +416,51 @@ impl Display for DisplayGenericAlias<'_> {
|
|||
impl<'db> GenericContext<'db> {
|
||||
pub fn display(&'db self, db: &'db dyn Db) -> DisplayGenericContext<'db> {
|
||||
DisplayGenericContext {
|
||||
typevars: self.variables(db),
|
||||
generic_context: self,
|
||||
db,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct DisplayOptionalGenericContext<'db> {
|
||||
generic_context: Option<&'db GenericContext<'db>>,
|
||||
db: &'db dyn Db,
|
||||
}
|
||||
|
||||
impl Display for DisplayOptionalGenericContext<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
if let Some(generic_context) = self.generic_context {
|
||||
DisplayGenericContext {
|
||||
generic_context,
|
||||
db: self.db,
|
||||
}
|
||||
.fmt(f)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DisplayGenericContext<'db> {
|
||||
typevars: &'db FxOrderSet<TypeVarInstance<'db>>,
|
||||
generic_context: &'db GenericContext<'db>,
|
||||
db: &'db dyn Db,
|
||||
}
|
||||
|
||||
impl Display for DisplayGenericContext<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
let variables = self.generic_context.variables(self.db);
|
||||
|
||||
let non_implicit_variables: Vec<_> = variables
|
||||
.iter()
|
||||
.filter(|var| !var.is_implicit(self.db))
|
||||
.collect();
|
||||
|
||||
if non_implicit_variables.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
f.write_char('[')?;
|
||||
for (idx, var) in self.typevars.iter().enumerate() {
|
||||
for (idx, var) in non_implicit_variables.iter().enumerate() {
|
||||
if idx > 0 {
|
||||
f.write_str(", ")?;
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ use crate::types::signatures::{Parameter, Parameters, Signature};
|
|||
use crate::types::tuple::{TupleSpec, TupleType};
|
||||
use crate::types::{
|
||||
KnownInstanceType, Type, TypeMapping, TypeRelation, TypeTransformer, TypeVarBoundOrConstraints,
|
||||
TypeVarInstance, TypeVarKind, TypeVarVariance, UnionType, binding_type, declaration_type,
|
||||
TypeVarInstance, TypeVarVariance, UnionType, binding_type, declaration_type,
|
||||
};
|
||||
use crate::{Db, FxOrderSet};
|
||||
|
||||
|
@ -259,13 +259,12 @@ impl<'db> GenericContext<'db> {
|
|||
db: &'db dyn Db,
|
||||
typevar: TypeVarInstance<'db>,
|
||||
) -> Option<TypeVarInstance<'db>> {
|
||||
assert!(typevar.kind(db) == TypeVarKind::Legacy);
|
||||
assert!(typevar.is_legacy(db));
|
||||
let typevar_def = typevar.definition(db);
|
||||
self.variables(db)
|
||||
.iter()
|
||||
.find(|self_typevar| {
|
||||
self_typevar.kind(db) == TypeVarKind::Legacy
|
||||
&& self_typevar.definition(db) == typevar_def
|
||||
self_typevar.is_legacy(db) && self_typevar.definition(db) == typevar_def
|
||||
})
|
||||
.copied()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue