Skip to content

A couple of _ast related issues #4760

Description

@isidentical

I was checking out the type stub, and found these points;

While examining this, I got an idea of autogenerating stub files by inputting a program with ASDLs (abstract syntax description language). So I wrote generators/typing_stub.py to my ASDL implementation, also generated test stub file.

I didn't test using that stub file, so don't know if there is a problem with it, but from a high level view they look similar with the _ast.pyi here, plus it covers the points I just mentioned (be aware, it is completely auto generated). There is only a single problem with that stub file, it ignores one of the broken parts of our AST. Dict(expr* keys, expr* values) actually doesn't describe the Dict well since when one of the items is **star_unpack the keys will contain None (so it should be List[Optional[expr]] not List[expr]). The currently type stub handles it, so if anyone wants to make a PR by copying things over the auto generated type stub, this point should not be actually copied.

Activity

  1. isidentical commented on Nov 13, 2020

    @isidentical
    SponsorMemberAuthor

    Another thing I noticed is;

    import ast
    node = ast.AST(body=[])
    print(node.body)
    
     $ mypy t.py                    
    t.py:4: error: "AST" has no attribute "body"
    Found 1 error in 1 file (checked 1 source file)
    

    I believe (did not test) can be fixed using something similiar to types.SimpleNamespace.
    https://github.com/isidentical/pyasdl/blob/c44b9a87ae9b6cf5ab98ef0f32b1757964d5a0de/generators/PythonAST.pyi#L10-L12

  2. srittau commented on Nov 30, 2020

    @srittau
    Collaborator

    Thank you and sorry for the late reply. Autogenerating the stub file sounds interesting indeed. I will take a closer look when I find the time.

  3. srittau commented on Dec 14, 2020

    @srittau
    Collaborator

    The only problem I see with autogenerating is supporting multiple Python version, which we need to do in typeshed.

  4. isidentical commented on Dec 14, 2020

    @isidentical
    SponsorMemberAuthor

    The only problem I see with autogenerating is supporting multiple Python version, which we need to do in typeshed.

    I don't quite get what you meant here, pyasdl's type stub generator generates for multiple versions. And also tries to do it in a gentle way. Check out the output stub.

  5. srittau commented on Dec 14, 2020

    @srittau
    Collaborator

    I totally missed that, that looks great!

  6. added a commit that references this issue on Dec 29, 2020
  7. added a commit that references this issue on Dec 29, 2020
  8. Avasam commented on Apr 10, 2025

    @Avasam
    SponsorCollaborator
    1. Python 3.7: Addressed in ast: remove docstring attribute #4868 But also support for 3.7 and 3.8 are now dropped.
    2. type_comment: Added in improvement: Add AST subclass constructors #11880
    3. Is there specific string fields identified? I'm not sure whether that's still something that needs to be updated or not.
    4. The TODO was also addressed in improvement: Add AST subclass constructors #11880
    5. Is this something we want to support? (with a typevar with can at least encode whether there are any custom attributes)
      from ast import AST
      AST(custom_attribute=[]).custom_attribute

    It's not the best but it kinda works better than just a bare Any:

    _CustomASTAttributesT = typing_extensions.TypeVar("_CustomASTAttributesT", default=NoReturn)
    class AST(Generic[_CustomASTAttributesT]):
        @overload
        def __init__(self) -> None: ...
        @overload
        def __init__(self, **kwargs: _CustomASTAttributesT) -> None: ...
        def __getattribute__(self, name: str) -> _CustomASTAttributesT: ...

    Image
    Image
    Image

    (the wonders AnyOf from a union would do here 😉 )

    Edit: Oh! Even better: <python-input-1>:1: DeprecationWarning: ast.AST.__init__ got an unexpected keyword argument 'test'. Support for arbitrary keyword arguments is deprecated and will be removed in Python 3.15.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions