Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pyrefly/lib/binding/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1621,7 +1621,20 @@ impl<'a> BindingsBuilder<'a> {
self.ensure_expr(new_type_name, class_object.usage());
self.check_functional_definition_name(&name.id, new_type_name, ErrorKind::InvalidArgument);
self.ensure_type(base, None, Evaluation::Eager);
self.bind_unread_keywords(keywords);
// NewType accepts exactly two positional arguments and no keywords.
for kw in keywords {
self.ensure_expr(&mut kw.value, class_object.usage());
let msg = if let Some(name) = &kw.arg {
format!("Unrecognized keyword argument `{name}`")
} else {
"Unpacking is not supported".to_owned()
};
self.error(
kw.range(),
ErrorKind::InvalidArgument,
format!("{msg} in NewType definition"),
);
}
self.synthesize_class_def(
class_name,
class_object,
Expand Down
11 changes: 11 additions & 0 deletions pyrefly/lib/test/new_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ userId = NewType() # E: Missing argument `name` # E: Missing argument `tp`
"#,
);

testcase!(
test_new_type_unexpected_keyword,
r#"
from typing import NewType, Any
A = NewType("A", int, extra=True) # E: Unrecognized keyword argument `extra` in NewType definition
a: A = A(1)
def f(kwargs: dict[str, Any]) -> None:
B = NewType("B", int, **kwargs) # E: Unpacking is not supported in NewType definition
"#,
);

testcase!(
test_new_type_call_base,
r#"
Expand Down
Loading