Skip to content

Commit fb0248f

Browse files
committed
fix: external pylib bugs
1 parent 2093579 commit fb0248f

File tree

7 files changed

+41
-5
lines changed

7 files changed

+41
-5
lines changed

crates/erg_compiler/context/inquire.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,7 @@ impl Context {
16871687
let mut errs = TyCheckErrors::empty();
16881688
// method: obj: 1, subr: (self: Int, other: Int) -> Int
16891689
// non-method: obj: Int, subr: (self: Int, other: Int) -> Int
1690+
// FIXME: staticmethod
16901691
let is_method = subr
16911692
.self_t()
16921693
.map_or(false, |self_t| self.subtype_of(obj.ref_t(), self_t));

crates/erg_compiler/declare.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ impl<A: ASTBuildable> GenericASTLowerer<A> {
4949
hir::Expr::TypeAsc(tasc) => enum_unwrap!(tasc.expr.as_ref(), hir::Expr::Accessor)
5050
.local_name()
5151
.map(Str::rc),
52-
hir::Expr::Accessor(acc) => acc.var_info().py_name.clone(),
53-
_ => sig.inspect().cloned(),
52+
hir::Expr::Accessor(hir::Accessor::Ident(ident)) => ident.vi.py_name.clone(),
53+
_ => sig.escaped(),
5454
};
5555
let found_body_t = chunk.ref_t();
5656
let ident = match &sig.pat {

crates/erg_compiler/error/tycheck.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,12 +805,18 @@ passed keyword args: {kw_args_len}"
805805
"english" =>sup_type.push_str("supertype: "),
806806
);
807807
sup_type.push_str_with_color_and_attr(format!("{sup_t}"), ERR, ATTR);
808+
let hint = switch_lang!(
809+
"japanese" => "これがあなたの定義した型ならば、型を共変もしくは反変にしてみてください(<: Output T もしくは <: Input T)",
810+
"simplified_chinese" => "如果这是您定义的类型,请尝试将类型变为协变或逆变(<: Output T 或 <: Input T)",
811+
"traditional_chinese" => "如果這是您定義的類型,請嘗試將類型變為協變或逆變(<: Output T 或 <: Input T)",
812+
"english" => "If this is the type you defined, try to make the type covariant or contravariant (<: Output T or <: Input T)",
813+
);
808814
Self::new(
809815
ErrorCore::new(
810816
vec![SubMessage::ambiguous_new(
811817
loc,
812818
vec![sub_type.to_string(), sup_type.to_string()],
813-
None,
819+
Some(hint.to_string()),
814820
)],
815821
switch_lang!(
816822
"japanese" => "不変な型パラメータを一意に決定できません",

crates/erg_compiler/lib/external/numpy.d/__init__.d.er

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@
99
ndim: Nat
1010
dtype: Type
1111
size: Nat
12+
copy: |T, S: [Nat; _]|(self: .NDArray(T, S),) -> .NDArray(T, S)
1213
reshape: |T, Old: [Nat; _], S: [Nat; _]|(
1314
self: .NDArray(T, Old),
1415
shape: {S},
1516
) -> .NDArray(T, S)
17+
sum: |T <: Num|(self: .NDArray(T, _),) -> T
18+
take: (|T|(self: .NDArray(T, _), indice: Nat) -> T) \
19+
and (|T|(self: .NDArray(T, _), indices: .NDArray(Nat) or [Nat; _]) -> .NDArray(T, _))
20+
tobytes: |T|(self: .NDArray(T, _),) -> Bytes
21+
tolist: |T|(self: .NDArray(T, _),) -> [T; _]
1622

1723
.nan: Float
1824
.Nan: Float
1925

20-
.abs: |T|(object: .NDArray(T),) -> .NDArray(T)
26+
.abs: |T, S: [Nat; _]|(object: .NDArray(T, S),) -> .NDArray(T, S)
2127
.add: |T, S: [Nat; _]|(object: .NDArray(T, S), other: .NDArray(T, S)) -> .NDArray(T, S)
2228
.all: |T <: Num|(object: .NDArray(T),) -> Bool
2329
.any: |T <: Num|(object: .NDArray(T),) -> Bool

crates/erg_compiler/lib/external/tqdm.d/__init__.d.er

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
.Tqdm! = 'tqdm': (T: Type) -> ClassType
22
.Tqdm!(T) <: Iterable T
33
.Tqdm!(T).
4-
# TODO: iterable should be Comparable
54
__call__: (
65
iterable: Iterable(T),
76
desc := Str,
@@ -30,3 +29,5 @@
3029
delay := Float,
3130
gui := Bool,
3231
) -> .Tqdm!(T)
32+
33+
.tqdm = .Tqdm!.__call__

crates/erg_parser/ast.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4406,6 +4406,18 @@ impl VarPattern {
44064406
}
44074407
}
44084408

4409+
pub fn escaped(&self) -> Option<Str> {
4410+
match self {
4411+
Self::Ident(ident) => {
4412+
let inspect = ident.inspect();
4413+
Some(Str::rc(
4414+
inspect.trim_end_matches('!').trim_start_matches('$'),
4415+
))
4416+
}
4417+
_ => None,
4418+
}
4419+
}
4420+
44094421
// _!(...) = ... is invalid
44104422
pub fn is_procedural(&self) -> bool {
44114423
match self {
@@ -4502,6 +4514,10 @@ impl VarSignature {
45024514
self.pat.inspect()
45034515
}
45044516

4517+
pub fn escaped(&self) -> Option<Str> {
4518+
self.pat.escaped()
4519+
}
4520+
45054521
pub const fn vis(&self) -> &VisModifierSpec {
45064522
self.pat.vis()
45074523
}

examples/external.er

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
time = pyimport "time"
22
tqdm = pyimport "tqdm"
33
j2 = pyimport "jinja2"
4+
np = pyimport "numpy"
45

56
for! tqdm.Tqdm!(0..<100), _ =>
67
time.sleep! 0.01
8+
for! tqdm.tqdm(0..<100), _ =>
9+
time.sleep! 0.01
710

811
plt = pyimport "matplotlib/pyplot"
912

@@ -14,3 +17,6 @@ plt.show!()
1417

1518
res = j2.Template("Hello {{ name }}!").render(name:="World")
1619
assert res == "Hello World!"
20+
21+
arr = np.array([1, 2, 3])
22+
assert arr.sum() == 6

0 commit comments

Comments
 (0)