mirror of
https://github.com/astral-sh/ruff.git
synced 2026-12-04 02:03:58 +08:00
[ty] Defer subtype checks for enum diagnostics until they can apply
This commit is contained in:
@@ -268,6 +268,21 @@ reveal_type(Pet6.species) # revealed: str
|
||||
reveal_type(Pet6.CAT.species) # revealed: str
|
||||
```
|
||||
|
||||
An annotation in an enum method declares a local variable. A nested enum still has its own members.
|
||||
|
||||
```py
|
||||
class Direction(Enum):
|
||||
UP = 1
|
||||
|
||||
def label(self) -> str:
|
||||
text: str = "up" # ok
|
||||
|
||||
class Style(Enum):
|
||||
SHORT: int = 1 # error: [invalid-enum-member-annotation]
|
||||
|
||||
return text
|
||||
```
|
||||
|
||||
### Pure declarations in stubs
|
||||
|
||||
In stubs, these should still be treated as non-member attributes rather than enum members:
|
||||
|
||||
@@ -5158,30 +5158,23 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
||||
&& !matches!(name_expr.id.as_str(), "_ignore_" | "_value_" | "_name_")
|
||||
// Bare Final is allowed on enum members.
|
||||
&& !declared.is_bare_final()
|
||||
&& (
|
||||
// Value type would be an enum member at runtime (exclude callables,
|
||||
// which are never members)
|
||||
!inferred_ty.is_subtype_of(db, env, Type::Callable(CallableType::top(db)))
|
||||
)
|
||||
&& self.index.scope(self.scope().file_scope_id(db)).kind() == ScopeKind::Class
|
||||
&& let Some(class) = nearest_enclosing_class(db, self.index, self.scope())
|
||||
&& is_enum_class_by_inheritance(db, env, class)
|
||||
&& !enum_ignored_names(db, self.scope()).contains(&name_expr.id)
|
||||
// Only check for callable values once we know this could be an enum member.
|
||||
&& !inferred_ty.is_subtype_of(db, env, Type::Callable(CallableType::top(db)))
|
||||
&& let Some(builder) = self
|
||||
.context
|
||||
.report_lint(&INVALID_ENUM_MEMBER_ANNOTATION, annotation)
|
||||
{
|
||||
let current_scope_id = self.scope().file_scope_id(self.db());
|
||||
let current_scope = self.index.scope(current_scope_id);
|
||||
if current_scope.kind() == ScopeKind::Class
|
||||
&& let Some(class) = nearest_enclosing_class(db, self.index, self.scope())
|
||||
&& is_enum_class_by_inheritance(db, env, class)
|
||||
&& !enum_ignored_names(self.db(), self.scope()).contains(&name_expr.id)
|
||||
&& let Some(builder) = self
|
||||
.context
|
||||
.report_lint(&INVALID_ENUM_MEMBER_ANNOTATION, annotation)
|
||||
{
|
||||
let mut diag = builder.into_diagnostic(format_args!(
|
||||
"Type annotation on enum member `{}` is not allowed",
|
||||
name_expr.id
|
||||
));
|
||||
diag.info(
|
||||
"See: https://typing.python.org/en/latest/spec/enums.html#enum-members",
|
||||
);
|
||||
}
|
||||
let mut diag = builder.into_diagnostic(format_args!(
|
||||
"Type annotation on enum member `{}` is not allowed",
|
||||
name_expr.id
|
||||
));
|
||||
diag.info(
|
||||
"See: https://typing.python.org/en/latest/spec/enums.html#enum-members",
|
||||
);
|
||||
}
|
||||
|
||||
self.add_declaration_with_binding(
|
||||
|
||||
@@ -170,8 +170,8 @@ pub(crate) fn check_static_class_definitions<'db>(
|
||||
|
||||
check_class_slots(context, class, index);
|
||||
|
||||
// Check that the class is not an enum and generic
|
||||
if is_enum_class_by_inheritance(db, env, class) && class.generic_context(db).is_some() {
|
||||
// Only generic classes need the enum inheritance check here.
|
||||
if class.generic_context(db).is_some() && is_enum_class_by_inheritance(db, env, class) {
|
||||
if let Some(builder) = context.report_lint(&INVALID_GENERIC_ENUM, class_node) {
|
||||
builder.into_diagnostic(format_args!(
|
||||
"Enum class `{}` cannot be generic",
|
||||
|
||||
Reference in New Issue
Block a user