[ty] Make NamedTuple(...) and namedtuple(...) calls stricter#22601
Merged
charliermarsh merged 2 commits intomainfrom Jan 15, 2026
Merged
[ty] Make NamedTuple(...) and namedtuple(...) calls stricter#22601charliermarsh merged 2 commits intomainfrom
NamedTuple(...) and namedtuple(...) calls stricter#22601charliermarsh merged 2 commits intomainfrom
Conversation
Typing conformance resultsNo changes detected ✅ |
|
d54657d to
89f6327
Compare
Member
Author
|
I believe the Rotki diagnostic is a true positive ( |
Member
yup, it fails at runtime (with... not a great error message, hmm): >>> from typing import NamedTuple
>>> NamedTuple('Version', ['version'])(version=1)
Traceback (most recent call last):
File "<python-input-1>", line 1, in <module>
NamedTuple('Version', ['version'])(version=1)
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/alexw/.pyenv/versions/3.13.1/lib/python3.13/typing.py", line 3102, in NamedTuple
nt = _make_nmtuple(typename, fields, module=_caller())
File "/Users/alexw/.pyenv/versions/3.13.1/lib/python3.13/typing.py", line 2976, in _make_nmtuple
fields = [n for n, t in types]
^^^^
ValueError: too many values to unpack (expected 2) |
Member
|
You can reduce duplicated code between the branches a bit: diff --git a/crates/ty_python_semantic/src/types/infer/builder.rs b/crates/ty_python_semantic/src/types/infer/builder.rs
index 42578d4889..bc86b65b33 100644
--- a/crates/ty_python_semantic/src/types/infer/builder.rs
+++ b/crates/ty_python_semantic/src/types/infer/builder.rs
@@ -6522,6 +6522,25 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
let has_starred = args.iter().any(ast::Expr::is_starred_expr);
let has_double_starred = keywords.iter().any(|kw| kw.arg.is_none());
+ // Emit diagnostic for missing required arguments or unsupported variadic arguments.
+ // For `typing.NamedTuple`, emit a diagnostic since variadic arguments are not supported.
+ // For `collections.namedtuple`, silently fall back since it's more permissive at runtime.
+ if (has_starred || has_double_starred)
+ && kind.is_typing()
+ && let Some(builder) = self.context.report_lint(&INVALID_ARGUMENT_TYPE, call_expr)
+ {
+ let arg_type = if has_starred && has_double_starred {
+ "Variadic positional and keyword arguments are"
+ } else if has_starred {
+ "Variadic positional arguments are"
+ } else {
+ "Variadic keyword arguments are"
+ };
+ builder.into_diagnostic(format_args!(
+ "{arg_type} not supported in `NamedTuple()` calls"
+ ));
+ }
+
// Need at least typename and fields/field_names.
let [name_arg, fields_arg, rest @ ..] = &**args else {
for arg in args {
@@ -6530,30 +6549,8 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
for kw in keywords {
self.infer_expression(&kw.value, TypeContext::default());
}
- // Emit diagnostic for missing required arguments or unsupported variadic arguments.
- if has_starred || has_double_starred {
- // For `typing.NamedTuple`, emit a diagnostic since variadic arguments are not supported.
- // For `collections.namedtuple`, silently fall back since it's more permissive at runtime.
- match kind {
- NamedTupleKind::Typing => {
- if let Some(builder) =
- self.context.report_lint(&INVALID_ARGUMENT_TYPE, call_expr)
- {
- let arg_type = if has_starred && has_double_starred {
- "Variadic positional and keyword arguments are"
- } else if has_starred {
- "Variadic positional arguments are"
- } else {
- "Variadic keyword arguments are"
- };
- builder.into_diagnostic(format_args!(
- "{arg_type} not supported in `NamedTuple()` calls"
- ));
- }
- }
- NamedTupleKind::Collections => {}
- }
- } else {
+
+ if !has_starred && !has_double_starred {
let fields_param = match kind {
NamedTupleKind::Typing => "fields",
NamedTupleKind::Collections => "field_names",
@@ -6586,27 +6583,6 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
for kw in keywords {
self.infer_expression(&kw.value, TypeContext::default());
}
- // For `typing.NamedTuple`, emit a diagnostic since variadic arguments are not supported.
- // For `collections.namedtuple`, silently fall back since it's more permissive at runtime.
- match kind {
- NamedTupleKind::Typing => {
- if let Some(builder) =
- self.context.report_lint(&INVALID_ARGUMENT_TYPE, call_expr)
- {
- let arg_type = if has_starred && has_double_starred {
- "Variadic positional and keyword arguments are"
- } else if has_starred {
- "Variadic positional arguments are"
- } else {
- "Variadic keyword arguments are"
- };
- builder.into_diagnostic(format_args!(
- "{arg_type} not supported in `NamedTuple()` calls"
- ));
- }
- }
- NamedTupleKind::Collections => {}
- }
return KnownClass::NamedTupleFallback.to_subclass_of(self.db());
}
@@ -15614,6 +15590,10 @@ impl NamedTupleKind {
matches!(self, Self::Collections)
}
+ const fn is_typing(self) -> bool {
+ matches!(self, Self::Typing)
+ }
+
fn from_type<'db>(db: &'db dyn Db, ty: Type<'db>) -> Option<Self> {
match ty {
Type::SpecialForm(SpecialFormType::NamedTuple) => Some(NamedTupleKind::Typing), |
AlexWaygood
reviewed
Jan 15, 2026
AlexWaygood
approved these changes
Jan 15, 2026
Member
AlexWaygood
left a comment
There was a problem hiding this comment.
LG other than my two suggested simplifications above!
e7248b8 to
1ca6785
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes astral-sh/ty#2513.