Repository navigation
Read optional params and defaults by one rule - #168
Merged
Merged
Conversation
Every style now splits a param's type the same way, in _text.type_spec: at commas outside brackets and quotes, taking out optional (in any case, or as *optional*), (optional), a trailing ?, and a default written as default 1, default=1, default: 1, default is 1 or defaults to 1. With no default in the type, _text.param_spec looks for one the description states: defaults to X, default is X, default value is X, default: X or default=X, where X is one value, maybe followed by a remark in parentheses or a condition. Prose such as "the default is to use all of them" gives none. Google only knew ". Defaults to X." in the middle of a description, ReST and Epydoc took everything after "defaults to". A param with a default is optional, and is_optional is always a bool: False instead of None for a param with no type. Numpydoc composes an optional param with no type as "x : optional", as "x, optional" read back as a param of that name.
rr-
force-pushed
the
rework/32-defaults
branch
from
November 6, 2026 17:36
5f4a71e to
48acc9a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Each style had its own way of finding that a param is optional and what its default is, and each missed things the others caught. A Google description starting with
Defaults to 3.gave no default, because Google only looked for. Defaults to X.in the middle of a sentence. ReST and Epydoc took everything after "defaults to", sopy_compile.compilegot "the PEP 3147/PEP 488 location" as a default. Numpydoc readdefault=theout of "By default the …".Now all four styles use one rule.
In the type, the text is split at commas outside brackets and quotes:
optional(in any case, or*optional*),(optional)and a trailing?mark the param optional.default 1,default=1,default: 1,default is 1,defaults to 1anddefaults 1give the default, without surrounding backticks.{'a', 'b'}, default 'a'keeps its braces andDict[str, int]keeps its comma.In the description, when the type has no default:
defaults to X,default is X,default value is X,default: Xordefault=X, case-insensitively. X has to be one value (a literal, a name, a call, or a quoted or bracketed expression), maybe followed by a remark in parentheses or byif,whenorunless:defaultDefaults to 3.3Value (default: 3).3Defaults toNone, meaning all.NoneDefaults to os.getcwd().os.getcwd()Defaults to 0 (no padding).0The default is to use all of them.Unlike the default :meth:run.A param with a default is optional. Breaking:
is_optionalof a parsed param is now always a bool,Falseinstead ofNonefor a param with no type.Numpydoc compose wrote an optional param with no type as
x, optional, which reads back as a param namedx, optional. It now writesx : optional. That case got more common with this change, so it's fixed here.Compose output changes only where a param became optional through its description. For example, Google
start (int): Defaults to 0.now composes asstart (int?): Defaults to 0..The numpydoc
PARAM_*_REGEXconstants andgoogle.GOOGLE_ARG_DESC_REGEXstay importable, but parsing no longer uses them.Parity against main: 3,289 of 29,123 docstrings differ.
is_optionalfromNonetoFalseThe two stdlib docstrings are
getpass.getpass, which gains the defaults'Password: 'andsys.stderr, andpy_compile.compile, which loses two prose "defaults". In the case files, the found defaults include'greedy'instead ofthe,np.meaninstead of`np.mean`, and pandas'default 'raise'taken out of a type it was stuck in. The dropped ones are prose like "English locale", "displaying nothing" and "an internal Console instance writing to stdout".Bench on Python 3.8: parse is 0.99–1.02× of main and compose 0.98–1.03×.