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
19 changes: 19 additions & 0 deletions pyrefly/lib/alt/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4028,6 +4028,25 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
"Type variable bounds and constraints must be concrete".to_owned(),
);
}
if type_form_context == TypeFormContext::TypeArgumentForType {
if let Some(cls) = match &ty {
Type::ClassType(cls) | Type::SelfType(cls) => Some(cls.class_object().clone()),
Type::ClassDef(cls) => Some(cls.clone()),
_ => None,
} {
if self.get_metadata_for_class(&cls).is_new_type() {
return self.error(
errors,
range,
ErrorInfo::Kind(ErrorKind::InvalidAnnotation),
format!(
"NewType `{}` is not a class and cannot be used with `type` or `Type`",
cls.name()
),
);
}
}
}
ty
}

Expand Down
16 changes: 16 additions & 0 deletions pyrefly/lib/test/new_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ y: type[Any] = Foo # E: `type[Foo]` is not assignable to `type[Any]`
"#,
);

testcase!(
test_new_type_type_argument,
r#"
from typing import NewType, Type

Thing = NewType("Thing", int)
ThingType = type[Thing] # E: NewType `Thing` is not a class and cannot be used with `type` or `Type`
OtherThingType = Type[Thing] # E: NewType `Thing` is not a class and cannot be used with `type` or `Type`

mapping: dict[int, ThingType] = {1: Thing} # E: `dict[int, type[Thing]]` is not assignable to `dict[int, type[Unknown]]`
Copy link
Author

Choose a reason for hiding this comment

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

note: I explicitly chose to keep cascading errors here. There may be a case for hiding them, but I think it makes sense to surface them 🤔


def func(x: ThingType) -> None: ...
func(Thing) # E: Argument `type[Thing]` is not assignable to parameter `x` with type `type[Unknown]` in function `func`
"#,
);

testcase!(
test_new_type_runtime_attrs,
r#"
Expand Down