Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Boxing bytes looses type information #3766

Open
HLWeil opened this issue Feb 22, 2024 · 3 comments · May be fixed by #3804
Open

[Python] Boxing bytes looses type information #3766

HLWeil opened this issue Feb 22, 2024 · 3 comments · May be fixed by #3804

Comments

@HLWeil
Copy link

HLWeil commented Feb 22, 2024

Description

When boxing a byte value, it looses its underlying type information.

Repro code

test.fsx

let intValue = box 1
let byteValue = box 2uy

let matchType (v : obj) =
    match v with
    | :?int as i -> printfn "int: %d" i
    | :?byte as u -> printfn "byte: %u" u
    | :?uint8 as u -> printfn "uint8: %u" u // equals byte, just for testing against Fable
    | _ -> printfn "other"

matchType intValue // int: 1
matchType byteValue // other

printfn $"{2uy.GetType().Name}" // Byte
printfn $"{byteValue.GetType().Name}" // Object

test.cmd

dotnet fable . --lang python
python test.py

Expected and actual results

Expected result: Boxed byte value should match the reflective match case, and calling GetType().Name should return "Byte"

Actual result: Match case is not hit and "Object" is returned

Related information

  • dotnet fable --version: 4.13.0
  • dotnet tool list/update/install: 4.13.0
  • Windows11

@Freymaurer

@dbrattli
Copy link
Collaborator

@HLWeil @Freymaurer, this is a bit tricky since Python really just have int so we need to do some tricks if we want to make this work. Another possibility is that we (Fable) take a dependency on NumPy to have sized and signed integers. Would NumPy as a dependency be acceptable for your application?

@ncave
Copy link
Collaborator

ncave commented Mar 12, 2024

Same thing with JS/TS, there is limited RTTI cause the base type for all integer types is either number (i.e. float64) or bigint.

@dbrattli
Copy link
Collaborator

Yes, with Python I'm considering removing the int subclasses which would make all integers transpile to variable size int and instead use numpy if more specific integers and reflection handling is needed. That should fix most array problems as well, i.e use list for arrays or numpy arrays if available.

@dbrattli dbrattli linked a pull request Apr 10, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants