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 "name" / "value" support for Python with #2162

Merged
merged 10 commits into from
Jan 31, 2024

Conversation

pokey
Copy link
Member

@pokey pokey commented Jan 3, 2024

Checklist

  • I have added tests
  • [-] I have updated the docs and cheatsheet
  • [-] I have not broken the cheatsheet

Copy link
Member Author

Choose a reason for hiding this comment

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

this is analogous to what we do for const aaa = bbb, ccc = ddd; in typescript, where there is one scope for each subclause and one scope for the entire statement

@@ -76,6 +78,8 @@ const scopeSupportFacets = [
"value.return",
"value.return.lambda",
"value.field",
"value.resourceManagement",
Copy link
Member Author

@pokey pokey Jan 3, 2024

Choose a reason for hiding this comment

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

This concept is supported in a few languages other than Python. Here's more info, courtesy of chatgpt:


Several languages have constructs similar to Python's with statement, designed for managing resources in a structured way. Here's a closer look at how some languages implement this with ___-style setup:

  1. C# (.NET Framework):

    • Construct: using
    • Usage: with equivalent in C# is using. It's used for managing resources like file streams, network connections, or database connections. It ensures the resources are disposed of once the block is exited.
    • Example:
      using (var resource = new Resource())
      {
          // Use resource
      } // Automatic disposal of resource
  2. Java:

    • Construct: try-with-resources
    • Usage: Introduced in Java 7, try-with-resources is similar to Python's with. It ensures that each resource is closed at the end of the statement.
    • Example:
      try (Resource resource = new Resource()) {
          // Use resource
      } // Automatic resource closure
  3. VB.NET:

    • Construct: Using
    • Usage: Similar to C#, VB.NET uses Using for managing and disposing of resources automatically.
    • Example:
      Using resource As New Resource()
          ' Use resource
      End Using ' Automatic disposal of resource
  4. Swift:

    • Construct: defer
    • Usage: While not a direct equivalent, Swift's defer statement schedules code to be executed when the current scope is exited, similar to the cleanup part of Python's with.
    • Example:
      let resource = Resource()
      defer {
          resource.close()
      }
      // Use resource
  5. F#:

    • Construct: use
    • Usage: F# has a use keyword that is similar to using in C#. It's used for automatic disposal of resources.
    • Example:
      use resource = new Resource()
      // Use resource

These constructs are primarily used for resource management to ensure that resources such as file handles, network connections, or database connections are properly released after their use, even if an error occurs. Each language has its own syntax and nuances, but the underlying principle remains the same.


Looks like C#, Java, and VB.NET are good matches, where name and value could be defined similarly to what we've done here for Python. The others are a bit more dicey

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, Java's try-with-resources was the first one to come to mind other than Python.

Don't forget the ECMAScript explicit resource management proposal

@pokey pokey changed the title Add "value" support for Python with Add "name" / "value" support for Python with Jan 3, 2024
queries/python.scm Outdated Show resolved Hide resolved
@pokey
Copy link
Member Author

pokey commented Jan 25, 2024

ok @AndreasArvidsson this one has been updated based on our discussion

@pokey
Copy link
Member Author

pokey commented Jan 25, 2024

Note that CI will fail until #2206 is merged

@pokey pokey requested a review from josharian as a code owner January 31, 2024 16:41
@pokey pokey force-pushed the pokey/add-value-support-for-python-with branch from 3be886c to 71c5288 Compare January 31, 2024 16:46
@pokey pokey enabled auto-merge January 31, 2024 16:56
@pokey pokey added this pull request to the merge queue Jan 31, 2024
Merged via the queue into main with commit 09c28c8 Jan 31, 2024
14 checks passed
@pokey pokey deleted the pokey/add-value-support-for-python-with branch January 31, 2024 17:15
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.

3 participants