Skip to content

Commit

Permalink
fix: external pylib bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Dec 6, 2023
1 parent 2093579 commit fb0248f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions crates/erg_compiler/context/inquire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,7 @@ impl Context {
let mut errs = TyCheckErrors::empty();
// method: obj: 1, subr: (self: Int, other: Int) -> Int
// non-method: obj: Int, subr: (self: Int, other: Int) -> Int
// FIXME: staticmethod
let is_method = subr
.self_t()
.map_or(false, |self_t| self.subtype_of(obj.ref_t(), self_t));
Expand Down
4 changes: 2 additions & 2 deletions crates/erg_compiler/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ impl<A: ASTBuildable> GenericASTLowerer<A> {
hir::Expr::TypeAsc(tasc) => enum_unwrap!(tasc.expr.as_ref(), hir::Expr::Accessor)
.local_name()
.map(Str::rc),
hir::Expr::Accessor(acc) => acc.var_info().py_name.clone(),
_ => sig.inspect().cloned(),
hir::Expr::Accessor(hir::Accessor::Ident(ident)) => ident.vi.py_name.clone(),
_ => sig.escaped(),
};
let found_body_t = chunk.ref_t();
let ident = match &sig.pat {
Expand Down
8 changes: 7 additions & 1 deletion crates/erg_compiler/error/tycheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,12 +805,18 @@ passed keyword args: {kw_args_len}"
"english" =>sup_type.push_str("supertype: "),
);
sup_type.push_str_with_color_and_attr(format!("{sup_t}"), ERR, ATTR);
let hint = switch_lang!(
"japanese" => "これがあなたの定義した型ならば、型を共変もしくは反変にしてみてください(<: Output T もしくは <: Input T)",
"simplified_chinese" => "如果这是您定义的类型,请尝试将类型变为协变或逆变(<: Output T 或 <: Input T)",
"traditional_chinese" => "如果這是您定義的類型,請嘗試將類型變為協變或逆變(<: Output T 或 <: Input T)",
"english" => "If this is the type you defined, try to make the type covariant or contravariant (<: Output T or <: Input T)",
);
Self::new(
ErrorCore::new(
vec![SubMessage::ambiguous_new(
loc,
vec![sub_type.to_string(), sup_type.to_string()],
None,
Some(hint.to_string()),
)],
switch_lang!(
"japanese" => "不変な型パラメータを一意に決定できません",
Expand Down
8 changes: 7 additions & 1 deletion crates/erg_compiler/lib/external/numpy.d/__init__.d.er
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@
ndim: Nat
dtype: Type
size: Nat
copy: |T, S: [Nat; _]|(self: .NDArray(T, S),) -> .NDArray(T, S)
reshape: |T, Old: [Nat; _], S: [Nat; _]|(
self: .NDArray(T, Old),
shape: {S},
) -> .NDArray(T, S)
sum: |T <: Num|(self: .NDArray(T, _),) -> T
take: (|T|(self: .NDArray(T, _), indice: Nat) -> T) \
and (|T|(self: .NDArray(T, _), indices: .NDArray(Nat) or [Nat; _]) -> .NDArray(T, _))
tobytes: |T|(self: .NDArray(T, _),) -> Bytes
tolist: |T|(self: .NDArray(T, _),) -> [T; _]

.nan: Float
.Nan: Float

.abs: |T|(object: .NDArray(T),) -> .NDArray(T)
.abs: |T, S: [Nat; _]|(object: .NDArray(T, S),) -> .NDArray(T, S)
.add: |T, S: [Nat; _]|(object: .NDArray(T, S), other: .NDArray(T, S)) -> .NDArray(T, S)
.all: |T <: Num|(object: .NDArray(T),) -> Bool
.any: |T <: Num|(object: .NDArray(T),) -> Bool
Expand Down
3 changes: 2 additions & 1 deletion crates/erg_compiler/lib/external/tqdm.d/__init__.d.er
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.Tqdm! = 'tqdm': (T: Type) -> ClassType
.Tqdm!(T) <: Iterable T
.Tqdm!(T).
# TODO: iterable should be Comparable
__call__: (
iterable: Iterable(T),
desc := Str,
Expand Down Expand Up @@ -30,3 +29,5 @@
delay := Float,
gui := Bool,
) -> .Tqdm!(T)

.tqdm = .Tqdm!.__call__
16 changes: 16 additions & 0 deletions crates/erg_parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4406,6 +4406,18 @@ impl VarPattern {
}
}

pub fn escaped(&self) -> Option<Str> {
match self {
Self::Ident(ident) => {
let inspect = ident.inspect();
Some(Str::rc(
inspect.trim_end_matches('!').trim_start_matches('$'),
))
}
_ => None,
}
}

// _!(...) = ... is invalid
pub fn is_procedural(&self) -> bool {
match self {
Expand Down Expand Up @@ -4502,6 +4514,10 @@ impl VarSignature {
self.pat.inspect()
}

pub fn escaped(&self) -> Option<Str> {
self.pat.escaped()
}

pub const fn vis(&self) -> &VisModifierSpec {
self.pat.vis()
}
Expand Down
6 changes: 6 additions & 0 deletions examples/external.er
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
time = pyimport "time"
tqdm = pyimport "tqdm"
j2 = pyimport "jinja2"
np = pyimport "numpy"

for! tqdm.Tqdm!(0..<100), _ =>
time.sleep! 0.01
for! tqdm.tqdm(0..<100), _ =>
time.sleep! 0.01

plt = pyimport "matplotlib/pyplot"

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

res = j2.Template("Hello {{ name }}!").render(name:="World")
assert res == "Hello World!"

arr = np.array([1, 2, 3])
assert arr.sum() == 6

0 comments on commit fb0248f

Please sign in to comment.