Skip to content

Report super() in a directly-defined NamedTuple method#3845

Open
markselby9 wants to merge 1 commit into
facebook:mainfrom
markselby9:feat/3763-super-namedtuple
Open

Report super() in a directly-defined NamedTuple method#3845
markselby9 wants to merge 1 commit into
facebook:mainfrom
markselby9:feat/3763-super-namedtuple

Conversation

@markselby9

Copy link
Copy Markdown
Contributor

Closes #3763.

Summary

A zero-argument super() inside a method of a class that directly subclasses NamedTuple makes the class fail to define at runtime: the compiler creates a __class__ cell for super(), but the NamedTuple machinery does not propagate __classcell__ to type.__new__, raising RuntimeError. Subclasses of a concrete NamedTuple are built by ordinary class machinery and are unaffected.

This records whether a NamedTuple is directly defined (vs. inherited) in its metadata and emits an invalid-super-call error for a no-argument super() resolved inside a directly-defined NamedTuple method.

Note: only the zero-argument form is detected. An explicit super(F, self) fails at runtime for the same reason, but the lexical enclosing class is not threaded into the explicit-args binding, so it is not flagged yet (documented with a bug= test).

Test Plan

New tests in pyrefly/lib/test/named_tuple.rs: super(), super().__repr__(), and classmethod super() in a direct NamedTuple are flagged; (deep) subclasses and methods without super() are not; explicit super(F, self) is tracked as a known gap. cargo test passes; full lib test suite is green.

@github-actions

This comment has been minimized.

Comment thread pyrefly/lib/alt/solve.rs Outdated
errors,
range,
ErrorKind::InvalidSuperCall,
"`super` call with no arguments is not allowed in a `NamedTuple` method; it raises a `RuntimeError` when the class is defined".to_owned(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just "super call with no arguments is not allowed in a NamedTuple method" is enough, I don't think the latter part contains info that helps the user fix the issue

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — dropped the trailing clause; the message is now just "`super` call with no arguments is not allowed in a `NamedTuple` method".

@yangdanny97 yangdanny97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM aside from one comment

@NathanTempest if you get around to merging this, maybe you can patch it to address the comment since it's small?

Comment thread pyrefly/lib/alt/types/class_metadata.rs Outdated
/// If true, the namedtuple fields were dynamically generated (e.g., using a
/// generator or variable) and couldn't be statically resolved.
pub has_dynamic_fields: bool,
/// True if this class directly subclasses `NamedTuple` (so it is built by the NamedTuple

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can leave it at "Does this class directly extend NamedTuple?"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — simplified the doc comment to "Does this class directly extend NamedTuple?".

A zero-argument `super()` inside a method of a class that directly subclasses
`NamedTuple` makes the class fail to define at runtime: the compiler creates a
`__class__` cell for `super()`, but the NamedTuple machinery does not propagate
`__classcell__` to `type.__new__`, raising `RuntimeError`. Subclasses of a
concrete NamedTuple are built by ordinary class machinery and are unaffected.

Record whether a NamedTuple is directly defined (vs inherited) in its metadata,
and emit an `InvalidSuperCall` error for a no-argument `super()` resolved inside
a directly-defined NamedTuple method.

Closes facebook#3763
@markselby9 markselby9 force-pushed the feat/3763-super-namedtuple branch from aebace2 to 7909f37 Compare June 18, 2026 02:11
@github-actions github-actions Bot added size/m and removed size/m labels Jun 18, 2026
@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

new rule - ban super() call in NamedTuples

3 participants