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 hinting #143

Merged
merged 6 commits into from
Oct 27, 2021
Merged

Conversation

ziegenberg
Copy link
Collaborator

@ziegenberg ziegenberg commented Oct 24, 2021

Adds some basic type hinting.

mypy ansi2html/style.py is happy and mypy ansi2html/converter.py spits out a few errors I don't know how to fix.

ansi2html/converter.py:38: error: Cannot find implementation or library stub for module named "ordereddict"
ansi2html/converter.py:38: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
ansi2html/converter.py:38: error: Name "OrderedDict" already defined (possibly by an import)
ansi2html/converter.py:559: error: Incompatible return value type (got "Union[bool, str, Set[str]]", expected "str")
ansi2html/converter.py:567: error: Unsupported right operand type for in ("Union[bool, str, Set[str]]")
ansi2html/converter.py:726: error: "TextIO" has no attribute "detach"
Found 5 errors in 1 file (checked 1 source file)

Fixes: #140

@ziegenberg ziegenberg force-pushed the add-basic-type-hinting branch 3 times, most recently from 1674fec to 14f19e0 Compare October 24, 2021 15:10
FIXES pycontribs#140

Signed-off-by: Daniel Ziegenberg <daniel@ziegenberg.at>
@ssbarnea ssbarnea added the bug This issue/PR relates to a bug. label Oct 25, 2021
Copy link
Member

@ssbarnea ssbarnea left a comment

Choose a reason for hiding this comment

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

Please also add mypy to pre-commit config, without it there is no proof that mypy would pass your changes. You can inspire from https://github.com/ansible-community/ansible-lint/blob/main/.pre-commit-config.yaml#L93-L118 -- obviously depenendencies would be totally different.

Btw, thanks for taking care of this.

@ziegenberg
Copy link
Collaborator Author

ansi2html/util.py is only used in tests. should it move to the tests then?

Signed-off-by: Daniel Ziegenberg <daniel@ziegenberg.at>
Signed-off-by: Daniel Ziegenberg <daniel@ziegenberg.at>
@ziegenberg ziegenberg changed the title add type hinting add type hinting and update dependencies Oct 25, 2021
@ziegenberg
Copy link
Collaborator Author

If you want those dependency updates in their own PR, I can move them.

@ziegenberg
Copy link
Collaborator Author

For the remaining type hint errors, I probably need help. I'm not as familiar with the code...

ansi2html/converter.py:323: error: Incompatible types in assignment (expression has type "List[str]", variable has type "Iterator[Union[str, OSC_Link, CursorMoveUp]]")
ansi2html/converter.py:323: note: "list" is missing following "Iterator" protocol member:
ansi2html/converter.py:323: note:     __next__
ansi2html/converter.py:324: error: Incompatible types in assignment (expression has type "List[Union[str, OSC_Link, CursorMoveUp]]", variable has type "Iterator[Union[str, OSC_Link, CursorMoveUp]]")
ansi2html/converter.py:324: note: "list" is missing following "Iterator" protocol member:
ansi2html/converter.py:324: note:     __next__
ansi2html/converter.py:338: error: Incompatible types in assignment (expression has type "List[str]", variable has type "Iterator[Union[str, OSC_Link, CursorMoveUp]]")
ansi2html/converter.py:338: note: "list" is missing following "Iterator" protocol member:
ansi2html/converter.py:338: note:     __next__
ansi2html/converter.py:338: error: Argument 1 to "_check_links" has incompatible type "Iterator[Union[str, OSC_Link, CursorMoveUp]]"; expected "List[str]"
ansi2html/converter.py:339: error: Argument 1 to "join" of "str" has incompatible type "Iterator[Union[str, OSC_Link, CursorMoveUp]]"; expected "Iterable[str]"
ansi2html/converter.py:400: error: "yield from" can't be applied to "Union[str, CursorMoveUp]"
ansi2html/converter.py:407: error: The return type of a generator function should be "Generator" or one of its supertypes
ansi2html/converter.py:521: error: Non-overlapping equality check (left operand type: "Union[str, OSC_Link, CursorMoveUp]", right operand type: "Type[CursorMoveUp]")
ansi2html/converter.py:531: error: Argument 1 to "append" of "list" has incompatible type "Union[str, OSC_Link, CursorMoveUp]"; expected "str"
ansi2html/converter.py:571: error: Incompatible return value type (got "Union[bool, str, Set[str]]", expected "str")
ansi2html/converter.py:579: error: Unsupported right operand type for in ("Union[bool, str, Set[str]]")
ansi2html/converter.py:738: error: "TextIO" has no attribute "detach"
tests/test_ansi2html.py:101: error: Function is missing a type annotation for one or more arguments
tests/test_ansi2html.py:131: error: Function is missing a type annotation for one or more arguments
tests/test_ansi2html.py:147: error: Function is missing a type annotation for one or more arguments
Found 15 errors in 2 files (checked 6 source files)

@ziegenberg
Copy link
Collaborator Author

And if you get #126 in first, I will happily rebase

@ssbarnea
Copy link
Member

@ziegenberg I am ok with dependency version bumping but it should be done a separated pull-request. The one that adds typing (this one) should only contain changes related to typing and nothing that could affect the code at runtime.

Probably you can first do the deps bumping and after we merge it you rebase this, making it clean.

That is important for traceability.

@ziegenberg ziegenberg changed the title add type hinting and update dependencies add type hinting Oct 25, 2021
@ssbarnea
Copy link
Member

@ziegenberg First accept invitation from https://github.com/pycontribs/ansi2html/invitations and after that review #126 so we can merge it. I looks at it briefly and it looks ok, but I need another pair of eyes before merging that one.

@ziegenberg
Copy link
Collaborator Author

So I cleaned up the branch and force pushed. Mypy still fails with wit 15 errors. I brought this number down from originally 80 but I need help with the last tricky bits.

Mypy details
ansi2html/converter.py:323: error: Incompatible types in assignment (expression has type "List[str]", variable has type "Iterator[Union[str, OSC_Link, CursorMoveUp]]")
ansi2html/converter.py:323: note: "list" is missing following "Iterator" protocol member:
ansi2html/converter.py:323: note: __next__
ansi2html/converter.py:324: error: Incompatible types in assignment (expression has type "List[Union[str, OSC_Link, CursorMoveUp]]", variable has type "Iterator[Union[str, OSC_Link, CursorMoveUp]]")
ansi2html/converter.py:324: note: "list" is missing following "Iterator" protocol member:
ansi2html/converter.py:324: note: __next__
ansi2html/converter.py:338: error: Incompatible types in assignment (expression has type "List[str]", variable has type "Iterator[Union[str, OSC_Link, CursorMoveUp]]")
ansi2html/converter.py:338: note: "list" is missing following "Iterator" protocol member:
ansi2html/converter.py:338: note: __next__
ansi2html/converter.py:338: error: Argument 1 to "_check_links" has incompatible type "Iterator[Union[str, OSC_Link, CursorMoveUp]]"; expected "List[str]"
ansi2html/converter.py:339: error: Argument 1 to "join" of "str" has incompatible type "Iterator[Union[str, OSC_Link, CursorMoveUp]]"; expected "Iterable[str]"
ansi2html/converter.py:400: error: "yield from" can't be applied to "Union[str, CursorMoveUp]"
ansi2html/converter.py:407: error: The return type of a generator function should be "Generator" or one of its supertypes
ansi2html/converter.py:521: error: Non-overlapping equality check (left operand type: "Union[str, OSC_Link, CursorMoveUp]", right operand type: "Type[CursorMoveUp]")
ansi2html/converter.py:531: error: Argument 1 to "append" of "list" has incompatible type "Union[str, OSC_Link, CursorMoveUp]"; expected "str"
ansi2html/converter.py:571: error: Incompatible return value type (got "Union[bool, str, Set[str]]", expected "str")
ansi2html/converter.py:579: error: Unsupported right operand type for in ("Union[bool, str, Set[str]]")
ansi2html/converter.py:738: error: "TextIO" has no attribute "detach"
tests/test_ansi2html.py:101: error: Function is missing a type annotation for one or more arguments
tests/test_ansi2html.py:131: error: Function is missing a type annotation for one or more arguments
tests/test_ansi2html.py:147: error: Function is missing a type annotation for one or more arguments
Found 15 errors in 2 files (checked 6 source files)

Signed-off-by: Daniel Ziegenberg <daniel@ziegenberg.at>
@ziegenberg
Copy link
Collaborator Author

Ok, I got it down to 4 errors in only one file.

ansi2html/converter.py:422: error: Incompatible types in "yield" (actual type "Type[CursorMoveUp]", expected type "Union[str, CursorMoveUp]")
ansi2html/converter.py:524: error: Unsupported right operand type for in ("Union[str, OSC_Link]")
ansi2html/converter.py:570: error: Incompatible return value type (got "Union[bool, str, Set[str]]", expected "str")
ansi2html/converter.py:578: error: Unsupported right operand type for in ("Union[bool, str, Set[str]]")
Found 4 errors in 1 file (checked 6 source files)

@ssbarnea
Copy link
Member

@ziegenberg Feel free to add # type: ignore for these if possible or even ignore the entire file for the moment, with note mentioning we need to fix it in follow-up, so we can merge it.

They need to be fixed later.

Signed-off-by: Daniel Ziegenberg <daniel@ziegenberg.at>
@ziegenberg
Copy link
Collaborator Author

@ssbarnea I made the suggested changes and created #149 so it's documented that it needs fixing.
Do you want me to squash my commits?

@ssbarnea ssbarnea changed the title add type hinting Add type hinting Oct 27, 2021
@ssbarnea ssbarnea merged commit 331214b into pycontribs:main Oct 27, 2021
@ziegenberg ziegenberg deleted the add-basic-type-hinting branch October 28, 2021 14:25
ziegenberg added a commit to ziegenberg/ansi2html that referenced this pull request Feb 6, 2022
While adding typehinting the option to specify an input encoding
got ignored. This commit fixes this regression.

This commit also fixes the tests with calling ansi2html as command.
As the pytest documentation states, during test execution `stdin`
is set to a “null” object which will fail on attempts to read from it
because it is rarely desired to wait for interactive input when
running automated tests. So we also patch now `sys.stdin` using a
`io.TextIOWrapper` and wrapping any actual input in a `io.BytesIO`.

Signed-off-by: Daniel Ziegenberg <daniel@ziegenberg.at>
ziegenberg added a commit to ziegenberg/ansi2html that referenced this pull request Feb 6, 2022
While adding type-hinting the option to specify an input encoding
got ignored. This commit fixes this regression.

This commit also fixes the tests which call ansi2html as a command.
As the pytest documentation states, during test execution `stdin`
is set to a “null” object which will fail on attempts to read from it
because it is rarely desired to wait for interactive input when
running automated tests. So we also patch now `sys.stdin` using a
`io.TextIOWrapper` and wrapping any actual input in an `io.BytesIO`.

Signed-off-by: Daniel Ziegenberg <daniel@ziegenberg.at>
ssbarnea pushed a commit that referenced this pull request May 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add typehints
2 participants