Skip to content

mypy stub for ConfigObj  #184

Open
Open

Description

@Vykstorm

Hello. I just wanted to suggest adding a 'stub' for this library for static typing using the library mypy (no need to change code, the stub can go in a separate file).
I have my own version here (incomplete)

For example, here is part of the code related to ConfigObj constructor

from typing import *

infile_type = Union[str, Union[List[str], Tuple[str]], IO[str], Dict[str, Any]]
outfile_type = Union[str, IO[str]]

class ConfigObj(Section):
    def __init__(
        self: Any,
        infile: Optional[infile_type] = None,
        raise_errors: bool = False, list_values: bool = True, create_empty: bool = False,
        file_error: bool = False, interpolation: bool = True, stringify: bool = True,
        configspec: Optional[infile_type] = None,
        indent_type: Optional[str] = None,
        encoding: Optional[str] = None, default_encoding: Optional[str] = None,
        unrepr: bool = False, write_empty_values: bool = False, _inspec: bool = False
        ): ...

Activity

  1. Vykstorm commented on May 21, 2019

    @Vykstorm
    Author

    It would be usefull for mypy to infer the types of the values returned by the methods as_bool(), as_int(), ... in the Section class

    mypy (not python) will raise an error when executing the next code:

    from configobj import ConfigObj
    
    def foo(a : str) -> None:
        pass
    
    config = ConfigObj('example.cnf')
    foo(config.as_bool('myval'))
    error: Argument 1 to "foo" has incompatible type "bool"; expected "str"
    
  2. davetapley commented on Sep 5, 2021

    @davetapley
  3. robdennis commented on Jan 8, 2023

    @robdennis
    Member

    I think this is a good idea and typing support in my IDE is generally an important thing for me as a developer.

    I don't think that I'm capable of doing anything with this for 5.1.0, but I hope someone is able to take something like this on

  4. added this to the Someday milestone on Jan 8, 2023
  5. jelmer commented on Sep 17, 2024

    @jelmer
    Collaborator

    @Vykstorm / @davetapley can you add this to the repo in a PR?

  6. changed the title [-]Suggestion: mypy stub for ConfigObj [/-] [+]mypy stub for ConfigObj [/+] on Nov 26, 2024
  7. igorp-collabora commented on Jul 31, 2025

    @igorp-collabora

    Because the minimal Python version is now 3.8 the type hints can easily be added to the source code. I can make an PR if you are interested. It is also possible to a CI stage to verify that the type hints don't regress.

  8. jelmer commented on Jul 31, 2025

    @jelmer
    Collaborator
  9. igorp-collabora commented on Aug 1, 2025

    @igorp-collabora

    @jelmer I start in this PR: #267

    I wasn't entirely sure which functions and attributes are expected to be used by the end user so I started adding type hints everywhere and checking them with mypy. I think there is one bug I found where an exception is initialised with wrong number of arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions