docstr icon indicating copy to clipboard operation
docstr copied to clipboard

split param-string by escaping brackets to allow for type containing brackets

Open louisdecharson opened this issue 1 year ago • 3 comments

Types in Python might contain brackets and commas resulting in errors when parsing the parameters of the function.

Example

def foo(bar: Dict[str, Dict[str, str]]) -> Dict[str, Dict[str, str]]:
    pass

Results in image

The PR creates a new function docstr--split-string-escape-brackets that split string while escaping the bracket to capture the full type: image

louisdecharson avatar Apr 23 '23 17:04 louisdecharson

I don't think it will work since it will affect all other languages. Python is indeed one of the hardest languages to parse using regex. 😓

Another approach is ts-docstr, but it requires tree-sitter. The parsing task is a lot easier there, and it's fast and accurate. Here is the result from ts-docstr:

def foo(bar: Dic[str, Dict[str, str]]) -> Dict[str, Dict[str, str]]:
    """

    Parameters
    -------
    bar : Dic[str, Dict[str, str]]
        [description]

    Returns
    -------
        [description]
    """
    pass

jcs090218 avatar Apr 23 '23 19:04 jcs090218

Hi @jcs090218, thank you for the swift reply. True that it will affect other languages but I think it's harmless as I cannot think of a programming language that would use unclosed brackets as part of a type or variable name - do you have an example in mind ?

Happy to use ts-docstr but it has some dependency to some niche packages like msgu which cannot be found on the usual Emacs lisp package archives.

louisdecharson avatar Apr 24 '23 08:04 louisdecharson

True that it will affect other languages but I think it's harmless as I cannot think of a programming language that would use unclosed brackets as part of a type or variable name - do you have an example in mind ?

I don't have an example in mind. Let me re-think this, and then I will get back to you!

Happy to use ts-docstr but it has some dependency to some niche packages like msgu which cannot be found on the usual Emacs lisp package archives.

msgu is under my own JCS-ELPA, including the package ts-docstr.

jcs090218 avatar Apr 24 '23 08:04 jcs090218