Skip to content

Fix most mypy errors. #403

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

Merged
merged 10 commits into from
Jul 7, 2021
Merged

Fix most mypy errors. #403

merged 10 commits into from
Jul 7, 2021

Conversation

tristanlatr
Copy link
Contributor

No description provided.

@tristanlatr tristanlatr requested a review from mthuurne June 16, 2021 18:16
@codecov
Copy link

codecov bot commented Jun 16, 2021

Codecov Report

Merging #403 (c96d8df) into master (566286b) will decrease coverage by 0.04%.
The diff coverage is 66.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #403      +/-   ##
==========================================
- Coverage   87.30%   87.26%   -0.05%     
==========================================
  Files          22       22              
  Lines        4262     4264       +2     
  Branches      839      840       +1     
==========================================
  Hits         3721     3721              
- Misses        350      351       +1     
- Partials      191      192       +1     
Impacted Files Coverage Δ
pydoctor/model.py 91.17% <0.00%> (-0.36%) ⬇️
pydoctor/epydoc/markup/restructuredtext.py 85.11% <100.00%> (ø)
pydoctor/templatewriter/__init__.py 90.37% <100.00%> (ø)

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 566286b...c96d8df. Read the comment docs.

@tristanlatr tristanlatr requested a review from adiroiban June 16, 2021 20:42
@tristanlatr
Copy link
Contributor Author

Opinions on the two issues @adiroiban @mthuurne ?

pydoctor/epydoc/markup/restructuredtext.py:55: error: Module "docutils.parsers.rst" has no attribute "directives"; maybe "Directive"?  [attr-defined]
pydoctor/epydoc/markup/restructuredtext.py:395: error: Argument 1 to "OptionParser" has incompatible type "List[Any]"; expected "Optional[str]"  [arg-type]

@mthuurne
Copy link
Contributor

Sorry for my slow responses; it's been a busy week and we had a bit of a heatwave here. I'll have a look now.

@mthuurne
Copy link
Contributor

pydoctor/epydoc/markup/restructuredtext.py:55: error: Module "docutils.parsers.rst" has no attribute "directives"; maybe "Directive"?  [attr-defined]
pydoctor/epydoc/markup/restructuredtext.py:395: error: Argument 1 to "OptionParser" has incompatible type "List[Any]"; expected "Optional[str]"  [arg-type]

The first error is weird: I can reproduce it under tox, but in my manually maintained venv it doesn't occur, even if I'm using the same version of mypy, docutils, and types-docutils.

The second error I can reproduce in both setups.

@tristanlatr
Copy link
Contributor Author

Ok. So let’s ignore them ?

@mthuurne
Copy link
Contributor

Ok. So let’s ignore them ?

I want to dig a bit deeper first.

@mthuurne
Copy link
Contributor

The problem with OptionParser seems to be that docutils.frontend.OptionParser inherits from optparse.OptionParser, but has a different __init__() signature, which is missing from the type stubs. Therefore this one can be ignored indeed, with a link to python/typeshed#5667 so we know what is happening and when we can remove the suppression.

@mthuurne
Copy link
Contributor

docutils.parsers.rst.directives is a module, which is present in docutils itself but missing from typing-docutils. So this is another case of the docutils stubs being incomplete.

I still don't understand why I don't get the error in my dev venv: I thought the reason might be because I have docutils installed, but the mypy tox environment also installs docutils since it's a dependency of sphinx, which we depend on since they ship type annotations in their main distribution itself.

So ignoring the error would be the correct short-term solution here as well. You could link to python/typeshed#1269 as documentation of the problem.

@@ -808,6 +808,7 @@ def introspectModule(self,
module_full_name = f'{package.fullName()}.{module_name}'

spec = importlib.util.spec_from_file_location(module_full_name, path)
assert spec is not None, f"Cannot find spec for module {module_full_name} at {path}"
Copy link
Contributor

Choose a reason for hiding this comment

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

We should only use assert if we can we guarantee that the condition is always true. If we're not that sure, it would be better to raise an exception instead.

I think there is at least one unlikely but possible scenario in which this condition is not true: if a file is deleted or moved between the moment its name is discovered and the moment it is introspected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A RuntimeError is OK ?

@tristanlatr
Copy link
Contributor Author

You could link to python/typeshed#1269 as documentation of the problem.

I've tried using the docutils-stubs as mentioned in this ticket but it's becoming a real headache, more than 50 errors that require a lot of additional checks in the restructuredtext module. I don't want to fix all mypy errors honestly. I will revert back to use types-docutils for the principal mypy env and create a separate one that uses docutils-stubs so we can look at it later.

Also the HTMLTranslator class has been moved to pydoctor.node2stan._PydoctorHTMLTranslator in #386

Copy link
Member

@glyph glyph left a comment

Choose a reason for hiding this comment

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

🚢


commands =
mypy \
--cache-dir="{toxworkdir}/mypy_cache" \
{tty:--pretty:} \
{posargs:pydoctor docs/epytext_demo}

[testenv:mypy-docutils-stubs]
description = run mypy with docutils-stubs (does not pass for now)
; See: https://github.com/python/typeshed/issues/1269
Copy link
Member

Choose a reason for hiding this comment

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

Do we want an issue for this repo as well, to track turning it on in CI when this is done?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See #409

@@ -808,6 +808,8 @@ def introspectModule(self,
module_full_name = f'{package.fullName()}.{module_name}'

spec = importlib.util.spec_from_file_location(module_full_name, path)
if spec is None:
raise RuntimeError(f"Cannot find spec for module {module_full_name} at {path}")
Copy link
Member

Choose a reason for hiding this comment

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

Can you do a test for this apparently untested case in a follow-up?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Like @mthuurne was saying, this error is raised when a file is deleted or moved between the moment its name is discovered and the moment it is introspected. So honestly I don't know how to test such a case.

Copy link
Member

Choose a reason for hiding this comment

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

This is why FilePath narrows down its interface to a single object which returns other objects, so you can pass a parameter along to the system under test. In the absence of such compositional design, the way to test this is to mock out spec_from_file_location with patch in a test.

@tristanlatr
Copy link
Contributor Author

Thank @glyph for your review.

I'll let @mthuurne merge this PR.

@glyph, If you want to help out reviewing more PRs, that would be great.
There is #386 waiting for a review ;)

@glyph
Copy link
Member

glyph commented Jun 22, 2021

@glyph, If you want to help out reviewing more PRs, that would be great.

I'd love to, but WYSIWYG in this case — I already have a recurring task in my personal task management system to review open source PRs as frequently as I can, and … this is about it ;)

@tristanlatr
Copy link
Contributor Author

@mthuurne Can I merge that ? Thanks

@tristanlatr
Copy link
Contributor Author

@mthuurne I will merge this PR now since it's blocking the CI from being green on all other branches.
If there is anything you'd like to be modified, please speak up and I'll submit a patch.

@tristanlatr tristanlatr merged commit 86cd1f5 into twisted:master Jul 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants