13559.bugfix.rst
Overview
This file documents a bug fix related to the type annotation in the `pytest` testing framework, specifically concerning the `addini` method of the `pytest.Parser` class. The fix involves adding missing `int` and `float` variants to the `Literal` type annotation of the `type` parameter in the method signature.
The purpose of this fix is to enhance the accuracy and completeness of type annotations, which improves static type checking and developer experience when using or extending the `pytest` framework.
Detailed Explanation
Context
In `pytest`, configuration options can be added via the `addini` method on a `Parser` object. These options can have different expected data types, such as strings, integers, or floats. Prior to this fix, the `type` parameter's type annotation in `addini` did not explicitly include `int` and `float` as valid literals, which could cause type-checking tools to flag errors or warnings when these types were used.
Changes Introduced
The
typeparameter in the method pytest.Parser.addini now has aLiteraltype annotation that includes the variants'int'and'float'along with previously supported types.This change ensures that when developers specify the type of an ini option as
'int'or'float', the type checker recognizes these as valid literals.
No functional code changes were made; this is purely a type annotation improvement.
Classes and Methods Affected
pytest.Parser.addini
Purpose:
Adds a new ini-file configuration option to the pytest parser, allowing users to define custom configuration keys that can be set in pytest ini files.Method Signature (simplified with fix):
def addini( self, name: str, help: str, *, type: Literal['args', 'bool', 'path', 'linelist', 'string', 'int', 'float'] = 'string', default: Union[str, int, float, List[str], None] = None ) -> None:Parameters:
name(str): The name of the configuration option.help(str): A help string describing the option.type(Literal[...]): The expected data type of the option value. The fix adds'int'and'float'as valid literals here.default(optional): The default value for the option, matching the specified type.
Returns:
NoneUsage Example:
parser.addini( name='max_retries', help='Maximum number of retries for flaky tests', type='int', default=3 ) parser.addini( name='timeout', help='Timeout value in seconds', type='float', default=5.0 )
Important Implementation Details
The fix addresses type annotation correctness without changing runtime behavior.
Ensures compatibility with static type checkers such as
mypy.Improves developer experience by clarifying the valid values for the
typeparameter.Helps maintain consistency with other parts of pytest where
intandfloattypes are supported for ini options.
Interaction with Other Parts of the System
This change affects the
pytest.Parserclass, which is part of the pytest core responsible for parsing configuration options.The
addinimethod is used internally when pytest loads and validates ini files (pytest.ini,tox.ini,setup.cfg).External plugins and users extending pytest can use this method to add custom ini options with accurate type hints.
Static analysis tools and IDEs reading the type annotations will provide better autocomplete and error detection.
Visual Diagram
classDiagram
class Parser {
+addini(name: str, help: str, type: Literal['args', 'bool', 'path', 'linelist', 'string', 'int', 'float'] = 'string', default: Union[str, int, float, List[str], None] = None) void
}
This class diagram highlights the `Parser` class with the updated `addini` method, showing the relevant method signature including the fixed type annotation for the `type` parameter.
Summary
This bug fix primarily enhances the typing metadata for the [pytest.Parser.addini](/projects/286/67266) method by including `int` and `float` as valid literals for the `type` parameter. It helps maintain pytest's robustness and developer friendliness without modifying any runtime logic.
By ensuring correct and comprehensive type annotations, this fix facilitates better static analysis, reduces potential bugs related to configuration option types, and supports future maintainability of the pytest codebase.