DiffSK / configobj

Python 3+ compatible port of the configobj library
https://configobj.readthedocs.org
Other
314 stars 76 forks source link

Suggestion: mypy stub for ConfigObj #184

Open Vykstorm opened 5 years ago

Vykstorm commented 5 years ago

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
        ): ...
Vykstorm commented 5 years ago

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"
davetapley commented 2 years ago

I started something to do this: https://github.com/JEFuller/dataclasses-configobj

robdennis commented 1 year ago

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