Skip to content

Commit

Permalink
Add support non-English words in docstring arguments (#6307)
Browse files Browse the repository at this point in the history
* Add support non english works in docstring arguments

* Relax argument regex to handle non english words but also be less strict with invalid types  in the google doc style with invalid types.

---------

Co-authored-by: Eric Traut <[email protected]>
  • Loading branch information
bschnurr and erictraut authored Nov 15, 2023
1 parent 76564a7 commit eeb5f11
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,7 @@ class DocStringConverter {
}

// catch-all for styles except reST
const hasArguments =
!line?.endsWith(':') && !line?.endsWith('::') && !!line.match(/^\s*.*?\w+(\s*\(.*?\))*\s*:\s*\w+/g);
const hasArguments = !line?.endsWith(':') && !line?.endsWith('::') && !!line.match(/.*?\s*:\s*(.+)/gu);

// reSt params. Attempt to put directives lines into their own paragraphs.
const restDirective = DirectivesExtraNewlineRegExp.test(line); //line.match(/^\s*:param/);
Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/docStringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function extractParameterDocumentation(functionDocString: string, paramNa
return trimmedLine.substr(paramOffset);
}

// Check for Google (variant 1) format
// Check for Google (variant 2) format
paramOffset = trimmedLine.indexOf(paramName + ' (');
if (paramOffset >= 0) {
return trimmedLine.substr(paramOffset);
Expand Down
25 changes: 0 additions & 25 deletions packages/pyright-internal/src/tests/docStringConversion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,31 +746,6 @@ Returns:
_testConvertToMarkdown(docstring, markdown);
});

test('GoogleWithInvalidTypes', () => {
const docstring = `
Example function with types documented in the docstring.
Args:
param1: (int|bool))): The first parameter.
param2: (list[str] with others): The second parameter.
Returns:
bool: The return value. True for success, False otherwise.
`;

const markdown = `
Example function with types documented in the docstring.
Args:
&nbsp;&nbsp;&nbsp;&nbsp;param1: (int|bool))): The first parameter.
param2: (list\\[str\\] with others): The second parameter.
Returns:
&nbsp;&nbsp;&nbsp;&nbsp;bool: The return value. True for success, False otherwise.`;

_testConvertToMarkdown(docstring, markdown);
});

test('FieldListDontAddLineBreaksToHeaders', () => {
const docstring = `
Parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@
//// '''This docstring ''' f'''is split.'''
//// pass
////
//// def func4(a:int, b:int, c:int):
//// """
//// Args:
//// a (int): description
//// b (int|bool): 한국어
//// c (int): description
//// """
////
//// [|/*marker1*/func|]()
//// [|/*marker2*/func2|]()
//// [|/*marker3*/func3|]()
//// [|/*marker4*/func4|]()

helper.verifyHover('markdown', {
marker1: '```python\n(function) def func() -> None\n```\n---\nThis docstring is split.',
marker2: '```python\n(function) def func2() -> None\n```',
marker3: '```python\n(function) def func3() -> None\n```',
marker4:
'```python\n(function) def func4(a: int, b: int, c: int) -> None\n```\n---\nArgs: \n&nbsp;&nbsp;&nbsp;&nbsp;a (int): description \n&nbsp;&nbsp;&nbsp;&nbsp;b (int|bool): 한국어 \n&nbsp;&nbsp;&nbsp;&nbsp;c (int): description',
});

0 comments on commit eeb5f11

Please sign in to comment.