Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 38 additions & 35 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3713,43 +3713,45 @@ impl<'db> TypeInferenceBuilder<'db> {
.unwrap_with_diagnostic(|lookup_error| match lookup_error {
LookupError::Unbound => {
let bound_on_instance = match value_type {
Type::ClassLiteral(class) => {
!class.class().instance_member(db, attr).0.is_unbound()
}
Type::SubclassOf(subclass_of @ SubclassOfType { .. }) => {
match subclass_of.subclass_of() {
ClassBase::Class(class) => {
!class.instance_member(db, attr).0.is_unbound()
Type::ClassLiteral(class) => {
!class.class().instance_member(db, attr).0.is_unbound()
}
Type::SubclassOf(subclass_of @ SubclassOfType { .. }) => {
match subclass_of.subclass_of() {
ClassBase::Class(class) => {
!class.instance_member(db, attr).0.is_unbound()
}
ClassBase::Dynamic(_) => unreachable!(
"Attribute lookup on a dynamic `SubclassOf` type should always return a bound symbol"
),
}
ClassBase::Dynamic(_) => unreachable!("Attribute lookup on a dynamic `SubclassOf` type should always return a bound symbol"),
}
}
_ => false,
};
_ => false,
};

if bound_on_instance {
self.context.report_lint(
&UNRESOLVED_ATTRIBUTE,
attribute,
format_args!(
"Attribute `{}` can only be accessed on instances, not on the class object `{}` itself.",
attr.id,
value_type.display(db)
),
);
} else {
self.context.report_lint(
&UNRESOLVED_ATTRIBUTE,
attribute,
format_args!(
"Type `{}` has no attribute `{}`",
value_type.display(db),
attr.id
),
);
}
if bound_on_instance {
self.context.report_lint(
&UNRESOLVED_ATTRIBUTE,
attribute,
format_args!(
"Attribute `{}` can only be accessed on instances, not on the class object `{}` itself.",
attr.id,
value_type.display(db)
),
);
} else {
self.context.report_lint(
&UNRESOLVED_ATTRIBUTE,
attribute,
format_args!(
"Type `{}` has no attribute `{}`",
value_type.display(db),
attr.id
),
);
}

Type::unknown()
Type::unknown()
}
LookupError::PossiblyUnbound(type_when_bound) => {
self.context.report_lint(
Expand Down Expand Up @@ -3799,7 +3801,7 @@ impl<'db> TypeInferenceBuilder<'db> {
let class_member = value_ty.member(self.db(), attr);

if class_member.is_unbound() {
if let Some(class) = match value_ty {
let class = match value_ty {
Type::ClassLiteral(class) => Some(class.class()),
Type::SubclassOf(subclass_of @ SubclassOfType { .. }) => {
match subclass_of.subclass_of() {
Expand All @@ -3808,7 +3810,8 @@ impl<'db> TypeInferenceBuilder<'db> {
}
}
_ => None,
} {
};
if let Some(class) = class {
let instance_member = class.instance_member(self.db(), attr);

// Attribute is declared or bound on instance. Forbid access from the class object
Expand Down
Loading