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 support for Fable 🎉 #2345

Merged
merged 164 commits into from
Jul 16, 2021
Merged

Conversation

dbrattli
Copy link
Collaborator

@dbrattli dbrattli commented Jan 13, 2021

Work in progress Python support for Fable that hopefully one day will fix #2339. Ready for review. Still a lot more work needed but this PR have over 100 passing unit-tests.

TODO:

  • functions, including arrow and function-expressions
  • classes
  • printfn, sprintf, string interpolation
  • import handling
  • test framework using pytest
  • nonlocal variables
  • fable-library, String (mostly supported)
  • fable-library, Option
  • fable-library, Seq
  • fable-library, List
  • fable-library, Set (wip)
  • fable-library, Record
  • fable-library, Map (wip)
  • fable-library, move to Fable so we can mix it with the .fs implementations
  • py-interop
  • async
  • Transform from Fable AST
  • FIx field get of anonymous records for Python dict
  • Reflection (wip)

@dbrattli dbrattli changed the title Python support for Fable [WIP] Python support for Fable Jan 13, 2021
@dbrattli dbrattli marked this pull request as ready for review July 15, 2021 09:19
@dbrattli dbrattli changed the title [WIP] Python support for Fable Python support for Fable Jul 15, 2021
build.fsx Show resolved Hide resolved
@dbrattli dbrattli changed the title Python support for Fable Python support for Fable 🎉 Jul 15, 2021
Copy link
Member

@alfonsogarciacaro alfonsogarciacaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome @dbrattli! 👏 👏 👏 I just had a quick look at the common files and left a few minor comments. I will try to check the big files in Transforms/Python tomorrow, but I don't see any problem to merge this to next branch and hopefully release an alpha version soon. Thanks a lot for this!

src/Fable.Core/Fable.Core.JsInterop.fs Outdated Show resolved Hide resolved
src/Fable.Core/Fable.Core.PyInterop.fs Outdated Show resolved Hide resolved
src/Fable.Core/Fable.Core.PyInterop.fs Outdated Show resolved Hide resolved
src/Fable.Core/Fable.Core.PyInterop.fs Outdated Show resolved Hide resolved
src/Fable.Core/Fable.Core.PyInterop.fs Outdated Show resolved Hide resolved
@@ -9,6 +9,7 @@ module Util =
try failwith "JS only" // try/catch is just for padding so it doesn't get optimized
with ex -> raise ex

let inline pyNative<'T> : 'T = jsNative
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can just use a single helper (nativeOnly or similar) that can be used for bindings of any language?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's good idea. Perhaps in another PR?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the pyNative at the end of lines of 12 and 14 in /tests/python/util.fs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @IltaySaeedi! They are replaced in the next branch now 👍

src/Fable.Transforms/Replacements.fs Show resolved Hide resolved
@@ -539,7 +431,7 @@ let choose (chooser: 'T->'U option) (array: 'T[]) ([<Inject>] cons: Cons<'U>) =
match chooser array.[i] with
| None -> ()
| Some y -> pushImpl res y |> ignore
if jsTypeof cons = "function"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alfonsogarciacaro How can we write test this using non-js code? This is why I added that callable to Fable.Core.

Copy link
Member

@alfonsogarciacaro alfonsogarciacaro Jul 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used jsTypeof cons = "function" because it's what I usually do in JS, but I guess we can add [<AllowNullLiteral>] to type Cons and then just make a null check because we're not going to pass anything through that argument that's not a constructor.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't understand add [} to type Cons`. Please explain

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will boxing work with a null check?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed a quote and the text was not well formatted 😅 If we add AllowNullLiteral attribute we shouldn't need the boxing. I can do that 👍

@alfonsogarciacaro
Copy link
Member

Ok, let's merge this. We can keep evolving Python support in follow-up PRs. Thanks a lot for this impressive work @dbrattli!

@alfonsogarciacaro alfonsogarciacaro merged commit 864b0a2 into fable-compiler:next Jul 16, 2021
@dbrattli dbrattli deleted the python branch July 16, 2021 05:18
@7sharp9
Copy link
Collaborator

7sharp9 commented Jul 17, 2021

Could this be used in a Jupyter notebook transpiling the code prior to notebook eval? cc/ @dbrattli

@Zaid-Ajaj
Copy link
Member

@scitesy
Copy link
Contributor

scitesy commented Jul 18, 2021

Wow! This is unbelievably great work!

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 this pull request may close these issues.

Python Support for Fable
8 participants