use allocative::Allocative;
use starlark_derive::starlark_value;
use starlark_derive::NoSerialize;
use starlark_derive::ProvidesStaticType;
use crate as starlark;
use crate::typing::Ty;
use crate::typing::TyBasic;
use crate::values::StarlarkValue;
#[doc(hidden)]
#[derive(
Debug,
derive_more::Display,
Allocative,
ProvidesStaticType,
NoSerialize
)]
#[display("type")]
pub enum AbstractType {}
#[starlark_value(type = "type")]
impl<'v> StarlarkValue<'v> for AbstractType {
fn get_type_starlark_repr() -> Ty {
Ty::basic(TyBasic::Type)
}
fn eval_type(&self) -> Option<Ty> {
match *self {}
}
}
#[cfg(test)]
mod tests {
use crate::assert;
#[test]
fn test_isinstance() {
assert::is_true("isinstance(int, type)");
assert::is_false("isinstance(1, type)");
assert::is_true("isinstance(list[str], type)");
assert::is_true("isinstance(eval_type(list), type)");
}
#[test]
fn test_pass() {
assert::pass(
r#"
def accepts_type(t: type):
pass
def test():
accepts_type(int)
accepts_type(list[str])
accepts_type(None | int)
test()
"#,
);
}
#[test]
fn test_fail_compile_time() {
assert::fail(
r#"
def accepts_type(t: type):
pass
def test():
accepts_type(1)
"#,
"Expected type `type` but got `int`",
);
}
}