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

--docstrings option for returning signatures with docstrings #25

Closed
simonw opened this issue Jun 20, 2023 · 5 comments
Closed

--docstrings option for returning signatures with docstrings #25

simonw opened this issue Jun 20, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@simonw
Copy link
Owner

simonw commented Jun 20, 2023

Can use ast.get_docstring(node) for this.

I'd like to show these as double quoted or triple-double-quoted strings.

@simonw simonw added the enhancement New feature or request label Jun 20, 2023
@simonw
Copy link
Owner Author

simonw commented Jun 20, 2023

Some ChatGPT research: https://chat.openai.com/share/f9b4f1d0-cedb-4576-a145-ad3eac345d27

Which helped me by writing this:

def convert_to_quoted(s):
    # Check if the string spans multiple lines
    if "\n" in s:
        # Escape triple double quotes
        s = s.replace('"""', '\\"\\"\\"')
        # Now wrap the whole string into triple double quotes
        return f'"""{s}"""'
    else:
        # Escape double quotes
        s = s.replace('"', '\\"')
        # Now wrap the whole string into double quotes
        return f'"{s}"'

@simonw
Copy link
Owner Author

simonw commented Jun 20, 2023

Slight point of confusion: --docstrings causes the docstrings to be displayed, but it shows functions without docsstrings too.

It might be expected to act as a filter, showing only functions with docstrings - for consistency with --function and --class and --async and --untyped and so-on.

Now that I'm trying it out I find myself wanting a way to see "all functions with docstrings" and "all functions without docstrings".

I could do --documented and --undocumented for that.

@simonw
Copy link
Owner Author

simonw commented Jun 20, 2023

git diff | llm --system 'describe this change' -m 3.5-16k

The diff command shows the changes made to the files README.md, symbex/cli.py, and symbex/lib.py. Here are the highlights of the changes:

In README.md:

  • Added two new filtering options: --documented and --undocumented.
  • Updated the description of the --signatures option to mention the new --docstrings option.
  • Added an example command to include docstrings in function/class signatures.

In symbex/cli.py:

  • Added a new --docstrings option as a flag.
  • Added documented and undocumented as options to the cli function's signature.
  • Updated the logic to handle the --docstrings option.
  • Updated the filter function to filter based on documentation presence.
  • Added an add_docstring function to add docstrings to function/class definitions.

In symbex/lib.py:

  • Added the add_docstring function to add docstrings to function/class definitions.
  • Updated the code_for_node function to use add_docstring.
  • Added a new quoted_string function to escape quotes in docstrings.

In the tests:

  • Added new tests to verify the --documented and --undocumented filters.
  • Added a new test to verify the --docstrings option and the inclusion of docstrings in the output.

Overall, these changes enhance the functionality of the symbex tool by providing options to filter based on the presence or absence of docstrings and include docstrings in the function/class signatures.

@simonw simonw closed this as completed in 495c377 Jun 20, 2023
@simonw
Copy link
Owner Author

simonw commented Jun 20, 2023

simonw added a commit that referenced this issue Jun 20, 2023
@simonw
Copy link
Owner Author

simonw commented Jun 21, 2023

Example:

symbex --docstrings --documented -f symbex/lib.py
# File: symbex/lib.py Line: 12
def find_symbol_nodes(code: str, filename: str, symbols: Iterable[str]) -> List[Tuple[(AST, Optional[str])]]
    "Returns ast Nodes matching symbols"

# File: symbex/lib.py Line: 36
def code_for_node(code: str, node: AST, class_name: str, signatures: bool, docstrings: bool) -> Tuple[(str, int)]
    "Returns the code for a given node"

# File: symbex/lib.py Line: 80
def match(name: str, symbols: Iterable[str]) -> bool
    "Returns True if name matches any of the symbols, resolving wildcards"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant