Skip to content

Commit 598c86b

Browse files
committed
[red-knot] Special case @final, @staticmethod, @override
1 parent e93fa70 commit 598c86b

4 files changed

Lines changed: 50 additions & 3 deletions

File tree

crates/red_knot_python_semantic/src/types.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5882,6 +5882,12 @@ bitflags! {
58825882
const OVERLOAD = 1 << 2;
58835883
/// `@abc.abstractmethod`
58845884
const ABSTRACT_METHOD = 1 << 3;
5885+
/// `@typing.final`
5886+
const FINAL = 1 << 4;
5887+
/// `@staticmethod`
5888+
const STATICMETHOD = 1 << 5;
5889+
/// `@typing.override`
5890+
const OVERRIDE = 1 << 6;
58855891
}
58865892
}
58875893

@@ -6265,6 +6271,8 @@ pub enum KnownFunction {
62656271
Cast,
62666272
/// `typing(_extensions).overload`
62676273
Overload,
6274+
/// `typing(_extensions).override`
6275+
Override,
62686276
/// `typing(_extensions).is_protocol`
62696277
IsProtocol,
62706278
/// `typing(_extensions).get_protocol_members`
@@ -6332,6 +6340,7 @@ impl KnownFunction {
63326340
| Self::AssertNever
63336341
| Self::Cast
63346342
| Self::Overload
6343+
| Self::Override
63356344
| Self::RevealType
63366345
| Self::Final
63376346
| Self::IsProtocol
@@ -7709,6 +7718,7 @@ pub(crate) mod tests {
77097718
KnownFunction::Cast
77107719
| KnownFunction::Final
77117720
| KnownFunction::Overload
7721+
| KnownFunction::Override
77127722
| KnownFunction::RevealType
77137723
| KnownFunction::AssertType
77147724
| KnownFunction::AssertNever

crates/red_knot_python_semantic/src/types/call/bind.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,14 @@ impl<'db> Bindings<'db> {
585585
}
586586
}
587587

588+
Some(KnownFunction::Override) => {
589+
// TODO: This can be removed once we understand legacy generics because the
590+
// typeshed definition for `typing.overload` is an identity function.
591+
if let [Some(ty)] = overload.parameter_types() {
592+
overload.set_return_type(*ty);
593+
}
594+
}
595+
588596
Some(KnownFunction::AbstractMethod) => {
589597
// TODO: This can be removed once we understand legacy generics because the
590598
// typeshed definition for `abc.abstractmethod` is an identity function.
@@ -593,6 +601,14 @@ impl<'db> Bindings<'db> {
593601
}
594602
}
595603

604+
Some(KnownFunction::Final) => {
605+
// TODO: This can be removed once we understand legacy generics because the
606+
// typeshed definition for `abc.abstractmethod` is an identity function.
607+
if let [Some(ty)] = overload.parameter_types() {
608+
overload.set_return_type(*ty);
609+
}
610+
}
611+
596612
Some(KnownFunction::GetattrStatic) => {
597613
let [Some(instance_ty), Some(attr_name), default] =
598614
overload.parameter_types()

crates/red_knot_python_semantic/src/types/class.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,7 @@ pub(crate) enum KnownClass {
19331933
BaseException,
19341934
BaseExceptionGroup,
19351935
Classmethod,
1936+
Staticmethod,
19361937
Super,
19371938
// enum
19381939
Enum,
@@ -2052,6 +2053,7 @@ impl<'db> KnownClass {
20522053
// and raises a `TypeError` in Python >=3.14
20532054
// (see https://docs.python.org/3/library/constants.html#NotImplemented)
20542055
| Self::NotImplementedType
2056+
| Self::Staticmethod
20552057
| Self::Classmethod => Truthiness::Ambiguous,
20562058
}
20572059
}
@@ -2079,6 +2081,7 @@ impl<'db> KnownClass {
20792081
Self::BaseException => "BaseException",
20802082
Self::BaseExceptionGroup => "BaseExceptionGroup",
20812083
Self::Classmethod => "classmethod",
2084+
Self::Staticmethod => "staticmethod",
20822085
Self::GenericAlias => "GenericAlias",
20832086
Self::ModuleType => "ModuleType",
20842087
Self::FunctionType => "FunctionType",
@@ -2256,6 +2259,7 @@ impl<'db> KnownClass {
22562259
| Self::BaseException
22572260
| Self::BaseExceptionGroup
22582261
| Self::Classmethod
2262+
| Self::Staticmethod
22592263
| Self::Slice
22602264
| Self::Range
22612265
| Self::Super
@@ -2345,6 +2349,7 @@ impl<'db> KnownClass {
23452349
| Self::BaseException
23462350
| Self::BaseExceptionGroup
23472351
| Self::Classmethod
2352+
| Self::Staticmethod
23482353
| Self::GenericAlias
23492354
| Self::ModuleType
23502355
| Self::FunctionType
@@ -2419,6 +2424,7 @@ impl<'db> KnownClass {
24192424
| Self::BaseException
24202425
| Self::BaseExceptionGroup
24212426
| Self::Classmethod
2427+
| Self::Staticmethod
24222428
| Self::TypeVar
24232429
| Self::ParamSpec
24242430
| Self::ParamSpecArgs
@@ -2462,6 +2468,7 @@ impl<'db> KnownClass {
24622468
"BaseException" => Self::BaseException,
24632469
"BaseExceptionGroup" => Self::BaseExceptionGroup,
24642470
"classmethod" => Self::Classmethod,
2471+
"staticmethod" => Self::Staticmethod,
24652472
"GenericAlias" => Self::GenericAlias,
24662473
"NoneType" => Self::NoneType,
24672474
"ModuleType" => Self::ModuleType,
@@ -2540,6 +2547,7 @@ impl<'db> KnownClass {
25402547
| Self::EllipsisType
25412548
| Self::BaseExceptionGroup
25422549
| Self::Classmethod
2550+
| Self::Staticmethod
25432551
| Self::FunctionType
25442552
| Self::MethodType
25452553
| Self::MethodWrapperType

crates/red_knot_python_semantic/src/types/infer.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,15 +1500,28 @@ impl<'db> TypeInferenceBuilder<'db> {
15001500
function_decorators |= FunctionDecorators::ABSTRACT_METHOD;
15011501
continue;
15021502
}
1503+
Some(KnownFunction::Final) => {
1504+
function_decorators |= FunctionDecorators::FINAL;
1505+
continue;
1506+
}
1507+
Some(KnownFunction::Override) => {
1508+
function_decorators |= FunctionDecorators::OVERRIDE;
1509+
continue;
1510+
}
15031511
_ => {}
15041512
}
15051513
}
1506-
Type::ClassLiteral(class) => {
1507-
if class.is_known(self.db(), KnownClass::Classmethod) {
1514+
Type::ClassLiteral(class) => match class.known(self.db()) {
1515+
Some(KnownClass::Classmethod) => {
15081516
function_decorators |= FunctionDecorators::CLASSMETHOD;
15091517
continue;
15101518
}
1511-
}
1519+
Some(KnownClass::Staticmethod) => {
1520+
function_decorators |= FunctionDecorators::STATICMETHOD;
1521+
continue;
1522+
}
1523+
_ => {}
1524+
},
15121525
Type::DataclassTransformer(params) => {
15131526
dataclass_transformer_params = Some(params);
15141527
}

0 commit comments

Comments
 (0)