feat: string literal type (forward reference)

This commit is contained in:
Shunsuke Shibayama 2024-08-19 21:48:38 +09:00
parent dcae47070a
commit 6e88efebe8
3 changed files with 19 additions and 1 deletions

View File

@ -134,6 +134,7 @@ pylyzer converts Python ASTs to Erg ASTs and passes them to Erg's type checker.
* [ ] others
* [x] type assertion (`typing.cast`)
* [x] type narrowing (`is`, `isinstance`)
* [ ] `pyi` (stub) files support
---

View File

@ -947,7 +947,22 @@ impl ASTConverter {
self.convert_ident_type_spec(name.id.to_string(), name.location())
}
py_ast::Expr::Constant(cons) => {
if cons.value.is_none() {
self.convert_ident_type_spec("NoneType".into(), cons.location())
} else if let Some(name) = cons.value.as_str() {
self.convert_ident_type_spec(name.into(), cons.location())
} else {
let err = CompileError::syntax_error(
self.cfg.input.clone(),
line!() as usize,
pyloc_to_ergloc(cons.range()),
self.cur_namespace(),
format!("{:?} is not a type", cons.value),
None,
);
self.errs.push(err);
Self::gen_dummy_type_spec(cons.location())
}
}
py_ast::Expr::Attribute(attr) => {
let namespace = Box::new(self.convert_expr(*attr.value));

View File

@ -19,6 +19,8 @@ class C:
return self.x
def id(self) -> Self:
return self
def id2(self) -> "C":
return self
c = C(1, 2)
assert c.x == 1