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

Add 3.12 typing features to the compiler #5302

Merged
merged 16 commits into from
May 10, 2024

Conversation

dchiquito
Copy link
Contributor

@dchiquito dchiquito commented May 7, 2024

This is the first step towards implementing 3.12 compliant typing(#5252 ) . It's taken me a long time to get this far so I wanted to check in before spending more time building on this foundation.

Changes made:

  • Add the builtin _typing stdlib module. This will eventually replace the fundamental typing primitives that are currently implemented in typing.py. This is still very much a work in progress, but it works well enough to exercise the new compiler features.
  • Compile and evaluate function generics (def foo[X](x: X): ...)
  • Compile and evaluate class generics (class C[X]: def __init__(x: X): ...)
  • Compile type alias statements (type X = list[int])

Some questions:

  • The push_symbol_table and pop_symbol_table are my attempt to simulate the behavior of the new Annotation Scopes. I'm not sure if I got this 100% right.
  • At some point, CPython introduced the compiler-level concept of intrinsics. There are a handful of instructions like CALL_INTRINSIC_1 and CALL_INTRINSIC_2 that do weird fundamental python things like printing, importing, or defining types. It seems to me that this is mostly a way of organizing the code than a fundamental design requirement, so I ignored this and added the intrinsics I needed as instructions. Perhaps this is not correct?
  • I made a small change to how the bookkeeping is done for unmarshalling bytecode. Adding a new instruction caused me some very odd intermittent errors that took me a while to debug, so my hope is to prevent headaches in the future.

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

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

Awesome! I like the changes.

The push_symbol_table and pop_symbol_table are my attempt to simulate the behavior of the new Annotation Scopes. I'm not sure if I got this 100% right.

To be honest, I don't know well about either Annotation Scopes or our symbol table. If you are not sure about the design, leaving a comment with your concern will be fine.
@coolreader18 Do you have any advice?

At some point, CPython introduced the compiler-level concept of intrinsics. There are a handful of instructions like CALL_INTRINSIC_1 and CALL_INTRINSIC_2 that do weird fundamental python things like printing, importing, or defining types. It seems to me that this is mostly a way of organizing the code than a fundamental design requirement, so I ignored this and added the intrinsics I needed as instructions. Perhaps this is not correct?

Yes, CPython is recently very quickly changing its instruction design. Our instructions design is staying around 3.6~3.8. Your approach seems correct and fits in traditional Python instruction design well. We will be able to introduce those new concepts later. Don't worry!

I made a small change to how the bookkeeping is done for unmarshalling bytecode. Adding a new instruction caused me some very odd intermittent errors that took me a while to debug, so my hope is to prevent headaches in the future.

I am sorry to make you to be confused. How about placing a comment to the last instruction like "Please add instruction in the middle of the enum; otherwise LAST_INSTRUCTION need to be updated"

vm/src/frame.rs Outdated Show resolved Hide resolved
@dchiquito
Copy link
Contributor Author

Thanks for the feedback! Would you prefer keeping this branch separate until typing is finished or merging it now?

@youknowone
Copy link
Member

I prefer to merge small patches, frequently. I will review this patch soon, thank you so much!

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

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

Awesome, finally we have _typing!

I also tried #4946 on it. _typing look like almost close to working.

Thank you for making a big progress 😄

@youknowone youknowone merged commit ac08f44 into RustPython:main May 10, 2024
11 checks passed
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.

None yet

2 participants