nodejs-polars icon indicating copy to clipboard operation
nodejs-polars copied to clipboard

Using `constructor.name` for `dataType.variant` seems not to be reliable

Open roll opened this issue 2 months ago • 4 comments

Have you tried latest version of polars?

  • [yes]

What version of polars are you using?

0.21.2

What operating system are you using polars on?

Ubuntu 24

What node version are you using

Node 24

Describe your bug.

I ran into weird bug when type inference based on DataFrame logic didn't match between running my project in Node and in Bun-compiled binary. After digging in a little bit a found out that the datatype.variant differs in builds (which broke the following login in the function):

polarsField: { name: 'name', type: DataType(String) },
polarsField: { name: 'name', type: DataType(String2) },

It's X layers of indirection (builds/compilers/etc) to figure out why it acts like this so I guess a safer solution would be just using constant for type identities to eliminate this possibility

roll avatar Sep 25 '25 10:09 roll

Do you think this is Bun vs. Node issue or nodejs-polars issue? Can you please elaborate on your fix? Thx

Bidek56 avatar Sep 25 '25 11:09 Bidek56

I think it might be unsafe to use return this.constructor.name as DataTypeName; as different tools can approach source code differently e.g. minifying class names String -> s. To eliminate this possibility I would suggest just using explicit variants something like:

export class Categorical extends DataType<"Categorical"> {
  variant = 'Categorical'
}

roll avatar Sep 25 '25 11:09 roll

We already have this definition:

export class Categorical extends DataType<"Categorical"> {
  declare __dtype: "Categorical";
}

Would you be willing to put a PR in? Thx

Bidek56 avatar Sep 25 '25 12:09 Bidek56

Sure, thanks, I will pr

roll avatar Sep 25 '25 12:09 roll