Skip to content

Commit 42b3fb3

Browse files
committed
Forbid use of super in NamedTuple subclasses
1 parent 69ace00 commit 42b3fb3

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

crates/ty_python_semantic/resources/mdtest/named_tuple.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,67 @@ class Vec2(NamedTuple):
408408

409409
Vec2(0.0, 0.0)
410410
```
411+
412+
## `super()` is not supported in NamedTuple methods
413+
414+
Using `super()` in a method of a `NamedTuple` subclass will raise an exception at runtime. In Python
415+
3.14+, a `TypeError` is raised; in earlier versions, a confusing `RuntimeError` about `__classcell__`
416+
is raised.
417+
418+
```py
419+
from typing import NamedTuple
420+
421+
class F(NamedTuple):
422+
x: int
423+
424+
def method(self):
425+
# error: [super-call-in-named-tuple-method] "Cannot use `super()` in a method of NamedTuple subclass `F`"
426+
super()
427+
428+
def method_with_args(self):
429+
# error: [super-call-in-named-tuple-method] "Cannot use `super()` in a method of NamedTuple subclass `F`"
430+
super(F, self)
431+
432+
def method_with_different_pivot(self):
433+
# Even passing a different pivot class fails - the error happens at class definition
434+
# time when the compiler sees the name `super`, regardless of arguments
435+
# error: [super-call-in-named-tuple-method] "Cannot use `super()` in a method of NamedTuple subclass `F`"
436+
super(tuple, self)
437+
438+
@classmethod
439+
def class_method(cls):
440+
# error: [super-call-in-named-tuple-method] "Cannot use `super()` in a method of NamedTuple subclass `F`"
441+
super()
442+
443+
@staticmethod
444+
def static_method():
445+
# error: [super-call-in-named-tuple-method] "Cannot use `super()` in a method of NamedTuple subclass `F`"
446+
super()
447+
448+
@property
449+
def prop(self):
450+
# error: [super-call-in-named-tuple-method] "Cannot use `super()` in a method of NamedTuple subclass `F`"
451+
return super()
452+
```
453+
454+
However, classes that **inherit from** a `NamedTuple` subclass (but don't directly inherit from
455+
`NamedTuple`) can use `super()` normally:
456+
457+
```py
458+
from typing import NamedTuple
459+
460+
class Base(NamedTuple):
461+
x: int
462+
463+
class Child(Base):
464+
def method(self):
465+
super()
466+
```
467+
468+
And regular classes that don't inherit from `NamedTuple` at all can use `super()` as normal:
469+
470+
```py
471+
class Regular:
472+
def method(self):
473+
super() # fine
474+
```

crates/ty_python_semantic/src/types/class.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::semantic_index::{
1919
use crate::types::bound_super::BoundSuperError;
2020
use crate::types::constraints::{ConstraintSet, IteratorConstraintsExtension};
2121
use crate::types::context::InferContext;
22-
use crate::types::diagnostic::INVALID_TYPE_ALIAS_TYPE;
22+
use crate::types::diagnostic::{INVALID_TYPE_ALIAS_TYPE, SUPER_CALL_IN_NAMED_TUPLE_METHOD};
2323
use crate::types::enums::enum_metadata;
2424
use crate::types::function::{DataclassTransformerParams, KnownFunction};
2525
use crate::types::generics::{
@@ -5546,6 +5546,20 @@ impl KnownClass {
55465546
return;
55475547
};
55485548

5549+
// Check if the enclosing class is a `NamedTuple`, which forbids the use of `super()`.
5550+
if CodeGeneratorKind::NamedTuple.matches(db, enclosing_class, None) {
5551+
if let Some(builder) = context
5552+
.report_lint(&SUPER_CALL_IN_NAMED_TUPLE_METHOD, call_expression)
5553+
{
5554+
builder.into_diagnostic(format_args!(
5555+
"Cannot use `super()` in a method of NamedTuple subclass `{}`",
5556+
enclosing_class.name(db)
5557+
));
5558+
}
5559+
overload.set_return_type(Type::unknown());
5560+
return;
5561+
}
5562+
55495563
// The type of the first parameter if the given scope is function-like (i.e. function or lambda).
55505564
// `None` if the scope is not function-like, or has no parameters.
55515565
let first_param = match scope.node(db) {
@@ -5585,6 +5599,22 @@ impl KnownClass {
55855599
overload.set_return_type(bound_super);
55865600
}
55875601
[Some(pivot_class_type), Some(owner_type)] => {
5602+
// Check if the enclosing class is a `NamedTuple`, which forbids the use of `super()`.
5603+
if let Some(enclosing_class) = nearest_enclosing_class(db, index, scope) {
5604+
if CodeGeneratorKind::NamedTuple.matches(db, enclosing_class, None) {
5605+
if let Some(builder) = context
5606+
.report_lint(&SUPER_CALL_IN_NAMED_TUPLE_METHOD, call_expression)
5607+
{
5608+
builder.into_diagnostic(format_args!(
5609+
"Cannot use `super()` in a method of NamedTuple subclass `{}`",
5610+
enclosing_class.name(db)
5611+
));
5612+
}
5613+
overload.set_return_type(Type::unknown());
5614+
return;
5615+
}
5616+
}
5617+
55885618
let bound_super = BoundSuperType::build(db, *pivot_class_type, *owner_type)
55895619
.unwrap_or_else(|err| {
55905620
err.report_diagnostic(context, call_expression.into());

crates/ty_python_semantic/src/types/diagnostic.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ pub(crate) fn register_lints(registry: &mut LintRegistryBuilder) {
101101
registry.register_lint(&POSSIBLY_MISSING_IMPORT);
102102
registry.register_lint(&POSSIBLY_UNRESOLVED_REFERENCE);
103103
registry.register_lint(&SUBCLASS_OF_FINAL_CLASS);
104+
registry.register_lint(&SUPER_CALL_IN_NAMED_TUPLE_METHOD);
104105
registry.register_lint(&OVERRIDE_OF_FINAL_METHOD);
105106
registry.register_lint(&TYPE_ASSERTION_FAILURE);
106107
registry.register_lint(&TOO_MANY_POSITIONAL_ARGUMENTS);
@@ -1760,6 +1761,33 @@ declare_lint! {
17601761
}
17611762
}
17621763

1764+
declare_lint! {
1765+
/// ## What it does
1766+
/// Checks for calls to `super()` inside methods of `NamedTuple` subclasses.
1767+
///
1768+
/// ## Why is this bad?
1769+
/// Using `super()` in a method of a `NamedTuple` subclass will raise an exception at runtime.
1770+
///
1771+
/// ## Examples
1772+
/// ```python
1773+
/// from typing import NamedTuple
1774+
///
1775+
/// class F(NamedTuple):
1776+
/// x: int
1777+
///
1778+
/// def method(self):
1779+
/// super() # error: super() is not supported in methods of NamedTuple subclasses
1780+
/// ```
1781+
///
1782+
/// ## References
1783+
/// - [Python documentation: super()](https://docs.python.org/3/library/functions.html#super)
1784+
pub(crate) static SUPER_CALL_IN_NAMED_TUPLE_METHOD = {
1785+
summary: "detects `super()` calls in methods of `NamedTuple` subclasses",
1786+
status: LintStatus::preview("1.0.0"),
1787+
default_level: Level::Error,
1788+
}
1789+
}
1790+
17631791
declare_lint! {
17641792
/// ## What it does
17651793
/// Checks for calls to `reveal_type` without importing it.

0 commit comments

Comments
 (0)