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

add type annotations to docutils.writers #12420

Merged
merged 7 commits into from
Oct 3, 2024

Conversation

danieleades
Copy link
Contributor

No description provided.

This comment has been minimized.

@danieleades danieleades marked this pull request as ready for review July 24, 2024 19:25
@danieleades danieleades marked this pull request as draft July 26, 2024 12:00
@danieleades
Copy link
Contributor Author

there's definitely there's definitely some issues with these stubs.

@jayaddison
@picnixz

can i get your insight?

Copy link
Contributor

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

For the mypy errors:

  • Missing generics are normal, since we didn't specify one.
  • Errors on the reader should not be error but it's because the typings are incorrect (there shouldn't be a bound for this type variable).
  • + sphinx/builders/latex/__init__.py:281:28: error: Argument "components" to "OptionParser" has incompatible type "tuple[LaTeXWriter]"; expected "Iterable[type[Parser]]" [arg-type]: the error is in the stub, it should not be Iterable[type[Parser]] but Iterable[Component] (I don't know why it's typed like that and I don't know when it was typed like that).
  • Concerning the fact that None has no attribute walkabout, this is because the variables are set at runtime when calling write() but this is impossible to indicate as a typing intent I think... unless you use Any (but it defeats the purpose ..., though this is what's being done for `docutils.parsers.

I'll review other errors after you changed the Reader class and remove ClassVars.

stubs/docutils/docutils/writers/__init__.pyi Outdated Show resolved Hide resolved
stubs/docutils/docutils/readers/__init__.pyi Outdated Show resolved Hide resolved
stubs/docutils/docutils/readers/doctree.pyi Show resolved Hide resolved
stubs/docutils/docutils/writers/__init__.pyi Outdated Show resolved Hide resolved
stubs/docutils/docutils/writers/__init__.pyi Outdated Show resolved Hide resolved
stubs/docutils/docutils/writers/__init__.pyi Outdated Show resolved Hide resolved
stubs/docutils/docutils/writers/__init__.pyi Outdated Show resolved Hide resolved

This comment has been minimized.

@picnixz
Copy link
Contributor

picnixz commented Jul 27, 2024

The only relevant error is now:

Cannot override class variable (previously declared on base class "SettingsSpec") with instance variable

and

  • sphinx/builders/text.py:74:27: error: Argument 1 to "write" of "Writer" has incompatible type "Node"; expected "document" [arg-type]

@danieleades danieleades marked this pull request as ready for review July 27, 2024 12:09

This comment has been minimized.

Copy link
Contributor

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

Surprisingly, we have no additional issue. So, the remaining issues in Sphinx are real issues IMO:

Cannot override class variable (previously declared on base class "SettingsSpec") with instance variable

We write

    settings_spec: tuple[str, Any, tuple[tuple[str, list[str], dict[str, str]], ...]]

but this should be a class var (we can even omit it after this PR is merged). As for the second error, we do self.document.walkabout(visitor) so we should have some assert just before the call to help mypy (or just type-ignore it). Finally, for

  • sphinx/builders/text.py:74:27: error: Argument 1 to "write" of "Writer" has incompatible type "Node"; expected "document" [arg-type]

It's because we have an incorrect signature:

    def write_doc(self, docname: str, doctree: Node) -> None: ...

It should be doctree: nodes.document instead. Now, we just need to add the generic types now in Sphinx but AFAIK, we only use strings and not bytes.

@AA-Turner do you want to have a look here?

Co-authored-by: James Addison <55152140+jayaddison@users.noreply.github.com>
Copy link
Contributor

github-actions bot commented Oct 3, 2024

Diff from mypy_primer, showing the effect of this PR on open source code:

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/io.py:146: error: Unused "type: ignore" comment  [unused-ignore]
+ sphinx/io.py:146:25: error: Missing type parameters for generic type "UnfilteredWriter"  [type-arg]
+ sphinx/io.py:146:25: note: Error code "type-arg" not covered by "type: ignore" comment
+ sphinx/writers/latex.py:68: error: Unused "type: ignore" comment  [unused-ignore]
+ sphinx/writers/latex.py:68:19: error: Missing type parameters for generic type "Writer"  [type-arg]
+ sphinx/writers/latex.py:68:19: note: Error code "type-arg" not covered by "type: ignore" comment
+ sphinx/writers/latex.py: note: In class "LaTeXWriter":
+ sphinx/writers/latex.py:77:5: error: Cannot override class variable (previously declared on base class "SettingsSpec") with instance variable  [misc]
+ sphinx/writers/latex.py: note: In member "translate" of class "LaTeXWriter":
+ sphinx/writers/latex.py:87:9: error: Item "None" of "document | None" has no attribute "walkabout"  [union-attr]
+ sphinx/writers/text.py:359: error: Unused "type: ignore" comment  [unused-ignore]
+ sphinx/writers/text.py:359:18: error: Missing type parameters for generic type "Writer"  [type-arg]
+ sphinx/writers/text.py:359:18: note: Error code "type-arg" not covered by "type: ignore" comment
+ sphinx/writers/text.py: note: In class "TextWriter":
+ sphinx/writers/text.py:362:5: error: Cannot override class variable (previously declared on base class "SettingsSpec") with instance variable  [misc]
+ sphinx/writers/text.py: note: In member "translate" of class "TextWriter":
+ sphinx/writers/text.py:372:9: error: Item "None" of "document | None" has no attribute "walkabout"  [union-attr]
+ sphinx/writers/texinfo.py:107: error: Unused "type: ignore" comment  [unused-ignore]
+ sphinx/writers/texinfo.py:107:21: error: Missing type parameters for generic type "Writer"  [type-arg]
+ sphinx/writers/texinfo.py:107:21: note: Error code "type-arg" not covered by "type: ignore" comment
+ sphinx/writers/texinfo.py: note: In class "TexinfoWriter":
+ sphinx/writers/texinfo.py:112:5: error: Cannot override class variable (previously declared on base class "SettingsSpec") with instance variable  [misc]
+ sphinx/writers/texinfo.py:120:5: error: Cannot override class variable (previously declared on base class "SettingsSpec") with instance variable  [misc]
+ sphinx/writers/texinfo.py: note: In member "translate" of class "TexinfoWriter":
+ sphinx/writers/texinfo.py:133:9: error: Item "None" of "document | None" has no attribute "walkabout"  [union-attr]

@JelleZijlstra JelleZijlstra merged commit 8228faa into python:main Oct 3, 2024
48 checks passed
@danieleades danieleades deleted the docutils.writers branch October 3, 2024 07:35
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.

4 participants