-
Notifications
You must be signed in to change notification settings - Fork 82
Draft: Add type hints #267
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: master
Are you sure you want to change the base?
Conversation
| arg_match = self._matchfinder.match(arg_string) | ||
| if arg_match is None: | ||
| # Bad syntax | ||
| raise VdtParamError('Bad syntax in check "%s".' % check) |
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.
I believe there is a real bug here:
src/configobj/validate.py:643: error: Missing positional argument "value" in call to "VdtParamError" [call-arg]
raise VdtParamError('Bad syntax in check "%s".' % check)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Found 1 error in 1 file (checked 1 source file)
VdtParamError takes 2 arguments to its initializer but only 1 is passed here. I haven't changed the code as it probably belongs to another PR.
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.
Yeah, I agree - it would be great if you could submit a separate PR for this.
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.
Sure.
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.
Hmmm... After some investigation I believe this was already fixed in the master branch but not release branch: dff3ca0
I am not entirely sure what is the relationship between those two branches.
|
The |
| """An unknown check function was requested""" | ||
|
|
||
| def __init__(self, value): | ||
| def __init__(self, value: Any): |
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.
There is no value in adding "Any" annotations; can we be more specific here? Even just having a "Value" type alias might be useful for readability - and there are certain constraints on Values I think.
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.
The value will be converted to a string anyway:
ValidateError.__init__(self, 'the check "%s" is unknown.' % (value,))
The object can be used here because any object can be converted to string: https://github.com/python/typeshed/blob/2888ec7011f5f2623fd3f3718d4668311932bb30/stdlib/builtins.pyi#L123
My plan is to add type hints with as minimal changes to the code as possible. Because of dynamic nature of existing code the use of Any is basically required. If you look at how serialization libraries are typed in standard library, all of them return Any when serialising or deserialising data: https://github.com/python/typeshed/blob/2888ec7011f5f2623fd3f3718d4668311932bb30/stdlib/json/__init__.pyi#L60
0165c35 to
d741891
Compare
|
I added very early |
src/configobj/__init__.py
Outdated
| def getObj(s) -> Any: | ||
| global compiler | ||
| if compiler is None: | ||
| import compiler |
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.
I have no idea what this compiler module is. Nothing on PyPI matches it.
src/configobj/__init__.py
Outdated
| pass | ||
|
|
||
|
|
||
| class Builder(object): |
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.
I don't believe this class is actually used anywhere?:
$ rg '_builder' src/
src/configobj/__init__.py
197:_builder = Builder()
| def _interpolate(self, key: str, value: Any) -> Any: | ||
| try: | ||
| # do we already have an interpolation engine? | ||
| engine = self._interpolation_engine |
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.
I believe there might be a bug in this function. First the name = self.main.interpolation where interpolation is a bool. If interpolation then name will be assigned a string and .lower() will be called. However, if interpolation then name will remain a bool that does not have .lower() method.
I belive what prevents it is that __getitem__ checks for self.main.interpolation before calling _interpolate but if this check was omitted this function would definitely raise an error.
d398e2c to
c165064
Compare
| def _decode(self, infile, encoding): | ||
| def _decode(self, infile: str | bytes | list[bytes] | list[str] | tuple[str, ...], encoding: str | None) -> list[str]: | ||
| """ | ||
| Decode infile to unicode. Using the specified encoding. |
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.
There is an issue with this function. It says that it will decode file to unicode (str) but if encoding is None it will happily return bytes. encoding can be None because self.encoding can also be None.
src/configobj/__init__.py
Outdated
| self.configspec = None | ||
| self.initial_comment: list[str] = [] | ||
| self.final_comment: list[str] = [] | ||
| self.configspec: ConfigObj | None = None |
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.
There are a lot of type checking errors because self.configspec can be None. The None is not being checked for.
c165064 to
8c1f63e
Compare
|
I reduced number of type errors from over 300 to 44 but it is still pretty ugly. Here is the mypy output: https://gist.github.com/igorp-collabora/5eaa20e23b063d04f986d08d53d7c29a |
|
Hi @igorp-collabora , are you still working on this? |
I ran in to some questions that I didn't have answers for:
|
Please target master. I'm working to simplify the branch situation.
We can remove this. The compiler module used to be shipped with Python 2 (prior to 2.7?). Since we only support Python 3, we can drop it. |
|
Looks like master branch has already removed the compiler module. I will see if I can rework this MR to target master. |
8c1f63e to
1c90267
Compare
|
I've changed the target of this PR to |
Work in progress.
Files type hints added to:
src/configobj/validate.pysrc/configobj/__init__.pypy.typedmarkerCI stages added to verify typing: