Skip to content

Keep the type of params as written - #158

Merged
rr- merged 1 commit into
mainfrom
rework/19-raw-type
Nov 6, 2026
Merged

rr- merged 1 commit into
mainfrom
rework/19-raw-type

Conversation

@rr-

@rr- rr- commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Every style splits the optional mark and the default off a param's type. Once that happens, nobody can tell what the docstring actually said:

>>> param = parse("Short.\n\nParameters\n----------\nx : int, default 1\n    X.").params[0]
>>> param.type_name, param.is_optional, param.default
('int', True, '1')
>>> param.raw_type_name
'int, default 1'

Users who compare a docstring against the code, like linters checking types against annotations, need the original text. pydoclint's fork of this library added the same field for Numpydoc. Here it covers all four styles:

  • ReST: :param str? x: and :type x: int, optional
  • Google: x (int, optional):
  • Numpydoc: x : int, optional
  • Epydoc: @type x: int, optional

raw_type_name is None when no type is written. Return types are never split, so returns don't get the field. Compose still builds the type from type_name, is_optional and default, so composed output stays the same.

Case files show raw_type_name only where it differs from type_name. That adds 478 lines to 167 case files: 137 corpus, 11 numpydoc, 9 google, 6 epydoc, 3 rest, 1 auto. By command: 289 parse numpydoc, 163 parse google, 18 parse epydoc, 7 parse rest, 1 parse auto. No existing line changes.

Parity against main, with the new field ignored: 0 of 29,074 docstrings differ.

Bench on 3.8: parse is 0.99–1.01x of main.

@rr- rr- self-assigned this Nov 6, 2026
A param's type loses its optional mark and default when they are split
off: "int, optional" becomes type_name "int" and is_optional True. A
linter comparing the docstring with an annotation needs what was
written, so every param now keeps it as raw_type_name.

It is None where no type was written. Compose still writes the type
from type_name, is_optional and default.
@rr-
rr- force-pushed the rework/19-raw-type branch from 83ea7cd to 432016d Compare November 6, 2026 16:38
@rr-
rr- merged commit d7a18a3 into main Nov 6, 2026
25 checks passed
@rr-
rr- deleted the rework/19-raw-type branch November 6, 2026 23:51
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.

1 participant