Skip to content
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

Fix and expand datalad.runner type annotations #6893

Conversation

christian-monch
Copy link
Contributor

This PR fixes #6889

The runner-code passes validation tests of mypy version 0.971, i.e. with strict optional checking.

Changelog

🏠 Internal

This commit simplifies the type of
`WitlessProtocol.proc_out` and
`WitlessProtocol.proc_err` to `bool`. The code
treated it earlier as `Optional[bool]`.
This commit fixes the type annotations in
`datalad.runner.exceptions`, and adds
runtime type-checks that are required
by `mypy` for a successful validation.
This commit fixes type annotation problems
in `datalad.runner.runnerthreads`. It also
replaces `Union[x, None]` with `Optional[x]`.
This commit fixes a faulty type annotation
by adding the missing `Optional[...]` clause.
This commit adds logging of the protocol that
was provided to the runner in order to improve
the possibility to diagnose an error based on
debug log-entries.
This commit fixes existing type annotations
in `datalad.runner.nonasyncrunner`. A few
`isinstance()` and `assert` clauses had to be
added.

A number of `ThreadedRunner` attributes are
only declared in the constructor. This
silences some other tools that check for
declarations of used attributes in the
constructor of a class.
@christian-monch christian-monch added the semver-internal Changes only affect the internal API label Jul 27, 2022
@christian-monch christian-monch changed the base branch from master to maint July 27, 2022 10:38
@codecov
Copy link

codecov bot commented Jul 27, 2022

Codecov Report

Merging #6893 (895becc) into maint (1fa6441) will increase coverage by 0.99%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##            maint    #6893      +/-   ##
==========================================
+ Coverage   90.16%   91.15%   +0.99%     
==========================================
  Files         354      354              
  Lines       46197    46215      +18     
==========================================
+ Hits        41652    42127     +475     
+ Misses       4545     4088     -457     
Impacted Files Coverage Δ
datalad/runner/exception.py 100.00% <100.00%> (ø)
datalad/runner/nonasyncrunner.py 99.65% <100.00%> (+0.01%) ⬆️
datalad/runner/protocol.py 100.00% <100.00%> (ø)
datalad/runner/runner.py 100.00% <100.00%> (ø)
datalad/runner/runnerthreads.py 97.47% <100.00%> (+0.02%) ⬆️
datalad/runner/tests/test_exception.py 100.00% <100.00%> (ø)
datalad/runner/tests/test_nonasyncrunner.py 100.00% <100.00%> (ø)
datalad/runner/tests/test_threadsafety.py 100.00% <100.00%> (ø)
datalad/runner/tests/test_utils.py 100.00% <100.00%> (ø)
datalad/runner/utils.py 100.00% <100.00%> (ø)
... and 10 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1fa6441...895becc. Read the comment docs.

@jwodder
Copy link
Member

jwodder commented Jul 27, 2022

@christian-monch DataLad requires Python 3.7+, correct? If so, the recommended approach is to use "modern" type annotations by adding from __future__ import annotations to the top of annotated files, which lets you write, e.g., dict[str, str | int] instead of typing.Dict[str, typing.Union[str, int]].

datalad/runner/exception.py Outdated Show resolved Hide resolved
This commit adds a missing return type to `_format_json_error_messages`.

Co-authored-by: John T. Wodder II <jwodder@users.noreply.github.com>
@christian-monch
Copy link
Contributor Author

@christian-monch DataLad requires Python 3.7+, correct? If so, the recommended approach is to use "modern" type annotations by adding from __future__ import annotations to the top of annotated files, which lets you write, e.g., dict[str, str | int] instead of typing.Dict[str, typing.Union[str, int]].

Good point, thanks. The annotations stem from the earlier versions. Will verify the requirements and use the "modern" style, which I prefer anyway :-)

@yarikoptic
Copy link
Member

woohoo, thanks @christian-monch . I pushed little commit to add entire runner into typechecking CI of tox

@yarikoptic
Copy link
Member

for your convenience, here is what errors CI reports

datalad/runner/tests/test_utils.py:140: error: Need type annotation for "decoded_chars"
datalad/runner/tests/test_threadsafety.py:61: error: Need type annotation for "args"
datalad/runner/tests/test_witless_runner.py:22: error: Cannot find implementation or library stub for module named "pytest"
datalad/runner/tests/test_witless_runner.py:22: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 3 errors in 3 files (checked 22 source files)
ERROR: InvocationError for command /home/runner/work/datalad/datalad/.tox/typing/bin/mypy --follow-imports skip datalad/api.py datalad/downloaders/providers.py datalad/metadata/indexers/base.py datalad/runner datalad/support/collections.py (exited with code 1)

so only something funny left in the runner tests.

Overcomes

    error: Cannot find implementation or library stub for module named "pytest"

and I do not think there is types-pytest
@yarikoptic
Copy link
Member

For

error: Cannot find implementation or library stub for module named "pytest"

added pytest to deps of typing (@jwodder is there a better way?) . So we are left only with the other two

tox.ini Outdated
@@ -29,6 +29,7 @@ commands = flake8 {posargs}
[testenv:typing]
deps =
mypy~=0.900
pytest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than manually specifying test dependencies, it would be better to tell tox to install all of them via the tests extra:

[testenv:typing]
extras = tests
...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone please address this.

@christian-monch
Copy link
Contributor Author

christian-monch commented Jul 28, 2022

for your convenience, here is what errors CI reports

datalad/runner/tests/test_utils.py:140: error: Need type annotation for "decoded_chars"
datalad/runner/tests/test_threadsafety.py:61: error: Need type annotation for "args"
datalad/runner/tests/test_witless_runner.py:22: error: Cannot find implementation or library stub for module named "pytest"
datalad/runner/tests/test_witless_runner.py:22: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 3 errors in 3 files (checked 22 source files)
ERROR: InvocationError for command /home/runner/work/datalad/datalad/.tox/typing/bin/mypy --follow-imports skip datalad/api.py datalad/downloaders/providers.py datalad/metadata/indexers/base.py datalad/runner datalad/support/collections.py (exited with code 1)

so only something funny left in the runner tests.

Thx, forgot to check the tests-directory. Fixing it.

@@ -32,7 +33,7 @@ def pipe_data_received(self, fd, data):
self.send_result((fd, line))


def _runner_with_protocol(protocol) -> ThreadedRunner:
def _runner_with_protocol(protocol: Type[WitlessProtocol]) -> ThreadedRunner:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from __future__ import annotations lets you use the builtin type instead of typing.Type here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx. Changed it to type[...]

This commit modernizes class-type annotations
in the runner code and runner tests
tox.ini Outdated
@@ -29,6 +29,7 @@ commands = flake8 {posargs}
[testenv:typing]
deps =
mypy~=0.900
pytest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone please address this.

This commit moves test dependencies from
`tox.ini` into `setup.py` and just references
them in `tox.ini`.
@christian-monch
Copy link
Contributor Author

christian-monch commented Jul 28, 2022

Moved the type-test dependency definitions to setup.py. Tests and type-checking dependencies are both in requires['tests'].

@christian-monch christian-monch merged commit f57c417 into datalad:maint Jul 28, 2022
@christian-monch christian-monch deleted the fix-issue-6889-runner-type-annotations-2 branch July 28, 2022 19:02
@yarikoptic yarikoptic changed the title Fix runner type annotations Fix and expand datalad.runner type annotations Jul 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-internal Changes only affect the internal API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

datalad/runner already has lots of annotations but ATM fails mypy (@christian-monch interested? ;))
3 participants