Skip to content

Improvement Parser.addini - type parameter #11381

@harmin-parra

Description

@harmin-parra

What's the problem this feature will solve?

Avoid writing extra code to convert data type from string to a numeric type (integer, float, etc)

Describe the solution you'd like

Parser.addini method receives a parameter called type that can be any of these string literals:

        type: Optional[
            "Literal['string', 'paths', 'pathlist', 'args', 'linelist', 'bool']"
        ] = None,

Add the literals (int, float) and have pytest to do the type check and conversion behind the scene.

Alternative Solutions

    parser.addini(
        "my_param",
        type="string",
        default="1.0",
        help="My float param."
    )

def my_param(request):
    try:
        return float(request.config.getini("my_param"))
    except:
        return 1.0 #The default value set with default="1.0", 

With the implementation of this feature, this can be reduced to this;

    parser.addini(
        "my_param",
        type="float",
        default="1.0",
        help="My float param."
    )

def my_param(request):
    return request.config.getini("my_param")

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: configrelated to config handling, argument parsing and config filetopic: typingtype-annotation issuetype: enhancementnew feature or API change, should be merged into features branchtype: feature-branchnew feature or API change, should be merged into features branchtype: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions