Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
34 changes: 32 additions & 2 deletions pyrefly/lib/alt/class/class_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,12 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
// The alias body is `type[T]` for bare aliases and `Annotated[T]` for Annotated aliases;
// we must handle both.
match &ty {
Type::Type(box Type::ClassType(cls))
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.

Is this case necessary? It looks like type[None] is handled by the Type::Type(box Type::None) case you added below.

if cls.class_object() == self.stdlib.builtins_type().class_object()
&& let Some(subscript_base_expr) = BaseClassExpr::from_expr(slice) =>
{
self.base_class_expr_infer_for_metadata(&subscript_base_expr, errors)
}
Type::Forall(forall)
if forall.tparams.len() == 1
&& let Forallable::TypeAlias(type_alias) = &forall.body
Expand Down Expand Up @@ -1179,6 +1185,28 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
}
}
}
Type::None if is_new_type => {
let base_cls = self.stdlib.none_type().class_object();
let metadata = self.get_metadata_for_class(base_cls);
BaseClassParseResult::Parsed({
ParsedBaseClass {
class_object: base_cls.dupe(),
range,
metadata,
}
})
}
Type::Type(box Type::None) if is_new_type => {
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.

You can combine this case and the previous one by doing Type::None | Type::Type(box Type::None) if is_new_type.

let base_cls = self.stdlib.none_type().class_object();
let metadata = self.get_metadata_for_class(base_cls);
BaseClassParseResult::Parsed({
ParsedBaseClass {
class_object: base_cls.dupe(),
range,
metadata,
}
})
}
Type::Type(box Type::Any(_)) => {
// `type[Any]` is equivalent to `type` or `Type`
let type_obj = self.stdlib.builtins_type().class_object();
Expand Down Expand Up @@ -1337,8 +1365,10 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
range,
metadata,
}) => {
if metadata.is_final()
|| (metadata.is_enum() && !self.get_enum_members(&class_object).is_empty())
if !is_new_type
&& (metadata.is_final()
|| (metadata.is_enum()
&& !self.get_enum_members(&class_object).is_empty()))
{
self.error(
errors,
Expand Down
33 changes: 33 additions & 0 deletions pyrefly/lib/test/new_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,39 @@ Foo.mro() # E: Object of class `object` has no attribute `mro`
"#,
);

testcase!(
test_newtype_none_is_nominal,
r#"
from typing import NewType
from types import NoneType

NewNoneType = NewType("NewNoneType", NoneType)
NewNone = NewNoneType(None)

def test(x: int | NewNoneType) -> None:
pass

test(None) # E: Argument `None` is not assignable to parameter `x` with type `NewNoneType | int` in function `test`
test(NewNone)
test(1)
"#,
);

testcase!(
test_newtype_type_none_is_nominal,
r#"
from typing import NewType

NewNoneType = NewType("NewNoneType", type[None])

def test(x: int | NewNoneType) -> None:
pass

test(None) # E: Argument `None` is not assignable to parameter `x` with type `NewNoneType | int` in function `test`
test(1)
"#,
);

testcase!(
test_tuple,
r#"
Expand Down
Loading