Skip to content

Local Variable Type Hints #7892

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

Draft
wants to merge 18 commits into
base: dev/feature
Choose a base branch
from

Conversation

APickledWalrus
Copy link
Member

@APickledWalrus APickledWalrus commented May 23, 2025

Problem

Local variables are essentially accepted wherever they are used. This results in more potential mistakes and (some) strange syntax behavior that is hard to track. Detailed type information for variables helps in two cases:

1. Mismatched types

It's possible you use the wrong variable somewhere. Detailed type information can catch this and print a warning (or error). Consider:

set {_a} to 5
set {_b} to "some string"
... do stuff ...
set {_c} to {_a} in lowercase # oops i used the wrong variable

We can catch that {_a} in lowercase is likely to be invalid and alert the user.

2. Syntax accepting objects

Consider syntax like ExprArithmetic:

set {_a} to "some string"
set {_b} to 1 + {_a}

With detailed type information, we can now identify at parse time that this is an invalid operation, instead of failing at runtime and returning none.

Solution

With systems like ExecutionIntent and multiple return types, we are finally in a position to execute this system well. Type tracking for local variables has been implemented across syntax interacting with variables using a HintManager that is attached to the ParserInstance.

The testing script along with HintManager's documentation provide insight into how hints are managed.

Assuming I can get this stable enough (i.e., depend on the type hints and avoid Object), I would likely consider making this an error. Due to addons, we might need a way to escape the error (operating "unsafely"?).

While the current draft is (somewhat) stable, further testing is needed, especially with addons.

Testing Completed

I have added a basic type hints.sk test (more to come). I plan to implement JUnit testing for HintManager.

Supporting Information

Depends on:


Completes:

Related:

@APickledWalrus APickledWalrus added bug An issue that needs to be fixed. Alternatively, a PR fixing an issue. feature Pull request adding a new feature. needs testing Needs testing to determine current status or issue validity, or for WIP feature pulls. labels May 23, 2025
@sovdeeth sovdeeth moved this to In Progress in 2.12 Release Jun 3, 2025
Variables will always use converters. so this allows us to represent a broader range of types (and likely reduce mistakes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An issue that needs to be fixed. Alternatively, a PR fixing an issue. feature Pull request adding a new feature. needs testing Needs testing to determine current status or issue validity, or for WIP feature pulls.
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

Local variable type hints. Helps identify local variable return types Variables can be used as function arguments of the wrong type
2 participants