-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Allow int for truncation_limit_lines and truncation_limit_chars in TOML
#14692
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The :confval:`truncation_limit_lines` and :confval:`truncation_limit_chars` configuration options now accept integer values in TOML configuration files, while still accepting strings for backward compatibility. |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,26 @@ | |||||||||
|
|
||||||||||
| FILE_OR_DIR = "file_or_dir" | ||||||||||
|
|
||||||||||
| #: The string tags accepted by :meth:`Parser.addini` for its ``type`` argument. | ||||||||||
| _IniTypeTag = Literal[ | ||||||||||
| "string", "paths", "pathlist", "args", "linelist", "bool", "int", "float" | ||||||||||
| ] | ||||||||||
|
|
||||||||||
| #: An ini option type: either a single tag, or a tuple of tags meaning "accept | ||||||||||
| #: a value of any of these types" (e.g. ``("int", "string")``). | ||||||||||
| IniType = _IniTypeTag | tuple[_IniTypeTag, ...] | ||||||||||
|
|
||||||||||
| _INI_TYPE_TAGS: tuple[str, ...] = ( | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| "string", | ||||||||||
| "paths", | ||||||||||
| "pathlist", | ||||||||||
| "args", | ||||||||||
| "linelist", | ||||||||||
| "bool", | ||||||||||
| "int", | ||||||||||
| "float", | ||||||||||
| ) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @final | ||||||||||
| class Parser: | ||||||||||
|
|
@@ -54,7 +74,7 @@ def __init__( | |||||||||
| file_or_dir_arg = self.optparser.add_argument(FILE_OR_DIR, nargs="*") | ||||||||||
| file_or_dir_arg.completer = filescompleter # type: ignore | ||||||||||
|
|
||||||||||
| self._inidict: dict[str, tuple[str, str, Any]] = {} | ||||||||||
| self._inidict: dict[str, tuple[str, IniType, Any]] = {} | ||||||||||
| # Maps alias -> canonical name. | ||||||||||
| self._ini_aliases: dict[str, str] = {} | ||||||||||
|
|
||||||||||
|
|
@@ -182,10 +202,7 @@ def addini( | |||||||||
| self, | ||||||||||
| name: str, | ||||||||||
| help: str, | ||||||||||
| type: Literal[ | ||||||||||
| "string", "paths", "pathlist", "args", "linelist", "bool", "int", "float" | ||||||||||
| ] | ||||||||||
| | None = None, | ||||||||||
| type: _IniTypeTag | tuple[_IniTypeTag, ...] | None = None, | ||||||||||
| default: Any = NOTSET, | ||||||||||
| *, | ||||||||||
| aliases: Sequence[str] = (), | ||||||||||
|
|
@@ -210,6 +227,16 @@ def addini( | |||||||||
|
|
||||||||||
| The ``float`` and ``int`` types. | ||||||||||
|
|
||||||||||
| A tuple of the above tags may also be passed to accept a value of | ||||||||||
| any of those types, for example ``("int", "string")``. In TOML | ||||||||||
| configuration files the value may then be any of the member types; | ||||||||||
| string-based formats (INI files, ``-o`` overrides) coerce it to the | ||||||||||
| first member that accepts it. | ||||||||||
|
|
||||||||||
| .. versionadded:: 9.1 | ||||||||||
|
|
||||||||||
| Passing a tuple of types. | ||||||||||
|
|
||||||||||
| For ``paths`` and ``pathlist`` types, they are considered relative to the config-file. | ||||||||||
| In case the execution is happening without a config-file defined, | ||||||||||
| they will be considered relative to the current working directory (for example with ``--override-ini``). | ||||||||||
|
|
@@ -233,19 +260,10 @@ def addini( | |||||||||
| The value of configuration keys can be retrieved via a call to | ||||||||||
| :py:func:`config.getini(name) <pytest.Config.getini>`. | ||||||||||
| """ | ||||||||||
| assert type in ( | ||||||||||
| None, | ||||||||||
| "string", | ||||||||||
| "paths", | ||||||||||
| "pathlist", | ||||||||||
| "args", | ||||||||||
| "linelist", | ||||||||||
| "bool", | ||||||||||
| "int", | ||||||||||
| "float", | ||||||||||
| ) | ||||||||||
| if type is None: | ||||||||||
| type = "string" | ||||||||||
| tags = type if isinstance(type, tuple) else (type,) | ||||||||||
| assert tags and all(tag in _INI_TYPE_TAGS for tag in tags), type | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably should use an exception here, given this might be an user-provided value.
|
||||||||||
| if default is NOTSET: | ||||||||||
| default = get_ini_default_for_type(type) | ||||||||||
|
|
||||||||||
|
|
@@ -261,16 +279,15 @@ def addini( | |||||||||
| self._ini_aliases[alias] = name | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def get_ini_default_for_type( | ||||||||||
| type: Literal[ | ||||||||||
| "string", "paths", "pathlist", "args", "linelist", "bool", "int", "float" | ||||||||||
| ], | ||||||||||
| ) -> Any: | ||||||||||
| def get_ini_default_for_type(type: IniType) -> Any: | ||||||||||
| """ | ||||||||||
| Used by addini to get the default value for a given config option type, when | ||||||||||
| default is not supplied. | ||||||||||
| """ | ||||||||||
| if type in ("paths", "pathlist", "args", "linelist"): | ||||||||||
| if isinstance(type, tuple): | ||||||||||
| # A union has no unambiguous implicit default; require an explicit one. | ||||||||||
| return None | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC, |
||||||||||
| elif type in ("paths", "pathlist", "args", "linelist"): | ||||||||||
| return [] | ||||||||||
| elif type == "bool": | ||||||||||
| return False | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.