feat: support `typing.cast`

This commit is contained in:
Shunsuke Shibayama 2023-04-19 20:19:30 +09:00
parent 5167ccc7ec
commit a974c1ff81
5 changed files with 44 additions and 30 deletions

48
Cargo.lock generated
View File

@ -252,9 +252,9 @@ checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
[[package]] [[package]]
name = "els" name = "els"
version = "0.1.23-nightly.0" version = "0.1.23-nightly.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0318245a907791e8519c8d74b9c6d07f8af4f0389c316a07fc558c4f2baa58f" checksum = "a10c98bd826f9954de66945f3b9fcdfd042d552725d44ae2ef05960280a06e56"
dependencies = [ dependencies = [
"erg_common", "erg_common",
"erg_compiler", "erg_compiler",
@ -274,9 +274,9 @@ dependencies = [
[[package]] [[package]]
name = "erg_common" name = "erg_common"
version = "0.6.11-nightly.0" version = "0.6.11-nightly.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0b8ba19d2ef219b9b2b8edcb29cd7059628bc49332495968438a564511f7a34" checksum = "3022fd66079dd98cdf7b609f6600b8a66c841c05cc9d87d66b6ad573797a27ab"
dependencies = [ dependencies = [
"backtrace-on-stack-overflow", "backtrace-on-stack-overflow",
"hermit-abi", "hermit-abi",
@ -286,9 +286,9 @@ dependencies = [
[[package]] [[package]]
name = "erg_compiler" name = "erg_compiler"
version = "0.6.11-nightly.0" version = "0.6.11-nightly.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5b8f4a9483689cec631d09c7d7a55f7c9714db761b50514478cd5ded75af62b" checksum = "4a54e29dc11754d29ca79302d6997d569af7216fd2f5471306d01aa1aaa49498"
dependencies = [ dependencies = [
"erg_common", "erg_common",
"erg_parser", "erg_parser",
@ -296,9 +296,9 @@ dependencies = [
[[package]] [[package]]
name = "erg_parser" name = "erg_parser"
version = "0.6.11-nightly.0" version = "0.6.11-nightly.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8259ee261c05034b9b4c0c548be4701651c3a54cc4a0c9c25464c700b45f5910" checksum = "344c547b69efd08d496ec84ecc4e9250862209965328d773cbe44589c4c8ceb1"
dependencies = [ dependencies = [
"erg_common", "erg_common",
"unicode-xid 0.2.4", "unicode-xid 0.2.4",
@ -430,9 +430,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.140" version = "0.2.141"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c" checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5"
[[package]] [[package]]
name = "log" name = "log"
@ -593,16 +593,16 @@ checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.54" version = "1.0.56"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e472a104799c74b514a57226160104aa483546de37e839ec50e3c2e41dd87534" checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]] [[package]]
name = "py2erg" name = "py2erg"
version = "0.0.20" version = "0.0.21"
dependencies = [ dependencies = [
"erg_common", "erg_common",
"erg_compiler", "erg_compiler",
@ -611,7 +611,7 @@ dependencies = [
[[package]] [[package]]
name = "pylyzer" name = "pylyzer"
version = "0.0.20" version = "0.0.21"
dependencies = [ dependencies = [
"els", "els",
"erg_common", "erg_common",
@ -792,9 +792,9 @@ dependencies = [
[[package]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.22" version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4a36c42d1873f9a77c53bde094f9664d9891bc604a45b4798fd2c389ed12e5b" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]] [[package]]
name = "rustpython-parser" name = "rustpython-parser"
@ -820,18 +820,18 @@ checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.159" version = "1.0.160"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c04e8343c3daeec41f58990b9d77068df31209f2af111e059e9fe9646693065" checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.159" version = "1.0.160"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585" checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -840,9 +840,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.95" version = "1.0.96"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744" checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1"
dependencies = [ dependencies = [
"itoa", "itoa",
"ryu", "ryu",
@ -920,9 +920,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.12" version = "2.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79d9531f94112cfc3e4c8f5f02cb2b58f72c97b7efd85f70203cc6d8efda5927" checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View File

@ -15,16 +15,16 @@ members = [
] ]
[workspace.package] [workspace.package]
version = "0.0.20" version = "0.0.21"
authors = ["Shunsuke Shibayama <sbym1346@gmail.com>"] authors = ["Shunsuke Shibayama <sbym1346@gmail.com>"]
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
edition = "2021" edition = "2021"
repository = "https://github.com/mtshiba/pylyzer" repository = "https://github.com/mtshiba/pylyzer"
[workspace.dependencies] [workspace.dependencies]
erg_common = { version = "0.6.11-nightly.0", features = ["py_compat", "els"] } erg_common = { version = "0.6.11-nightly.1", features = ["py_compat", "els"] }
erg_compiler = { version = "0.6.11-nightly.0", features = ["py_compat", "els"] } erg_compiler = { version = "0.6.11-nightly.1", features = ["py_compat", "els"] }
els = { version = "0.1.23-nightly.0", features = ["py_compat"] } els = { version = "0.1.23-nightly.1", features = ["py_compat"] }
rustpython-parser = "0.1.2" rustpython-parser = "0.1.2"
# erg_compiler = { git = "https://github.com/erg-lang/erg", branch = "main", features = ["py_compat", "els"] } # erg_compiler = { git = "https://github.com/erg-lang/erg", branch = "main", features = ["py_compat", "els"] }
# erg_common = { git = "https://github.com/erg-lang/erg", branch = "main", features = ["py_compat", "els"] } # erg_common = { git = "https://github.com/erg-lang/erg", branch = "main", features = ["py_compat", "els"] }
@ -44,7 +44,7 @@ erg_compiler = { workspace = true }
erg_common = { workspace = true } erg_common = { workspace = true }
els = { workspace = true } els = { workspace = true }
rustpython-parser = { workspace = true } rustpython-parser = { workspace = true }
py2erg = { version = "0.0.20", path = "./crates/py2erg" } py2erg = { version = "0.0.21", path = "./crates/py2erg" }
[lib] [lib]
path = "src/lib.rs" path = "src/lib.rs"

View File

@ -104,7 +104,7 @@ pylyzer converts Python ASTs to Erg ASTs and passes them to Erg's type checker.
* [ ] `Final` * [ ] `Final`
* [ ] `Annotated` * [ ] `Annotated`
* [ ] `TypeAlias` * [ ] `TypeAlias`
* [ ] type assertion (`typing.cast`) * [x] type assertion (`typing.cast`)
* [x] type narrowing (`is`, `isinstance`) * [x] type narrowing (`is`, `isinstance`)
* [ ] type guard (`TypeGuard`) * [ ] type guard (`TypeGuard`)

9
tests/casting.py Normal file
View File

@ -0,0 +1,9 @@
import typing
s = "a"
assert isinstance(s, int) # ERR
# force cast to int
i = typing.cast(int, s)
print(i + 1) # OK

View File

@ -88,3 +88,8 @@ fn exec_projection() {
fn exec_narrowing() { fn exec_narrowing() {
expect("tests/narrowing.py", 0, 1); expect("tests/narrowing.py", 0, 1);
} }
#[test]
fn exec_casting() {
expect("tests/casting.py", 1, 1);
}