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

chore: add types #225

Merged
merged 18 commits into from
Mar 11, 2022
Merged

chore: add types #225

merged 18 commits into from
Mar 11, 2022

Conversation

miketheman
Copy link
Member

There's a lot of commits here for a relatively small amount of changes, and it can seem overwhelming at first.

I tried to keep each commit well-scoped, and describe the journey on adding mypy as a testing strategy, correcting the issues surfaced during the initial (non-strict) runs, and then applying stricter rules, until it's finally a fully-typed library.

(I snuck in a pytest warning removal while I was at it.)

Completing this work, and releasing a version should allow us to remove a line in the warehouse config.

Let me know what y'all think!

Resolves #166

Scaffolding in the testing, and will not complete succssfully yet.

Will be followed by code changes to pass.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Surfaced via `mypy`, in that an empty dict could not infer what types
could be there.

Instead of importing `typing` and annotating the empty Dict, I opted
to ignore the line, as we do not expect to populate the dict at all,
and are using it to **prevent** additions to the value.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Surfaced via `mypy`, recommended adding a type to the empty list.

The list was originally empty back in 0.1.0.

Instead of adding a type, remove the constant, and the code that uses it
from `clean()` - as it was partially reverted in pypa#121.

The default `ALLOWED_STYLES` in the underlying library is `[]`.

Related: pypa#114 (comment)

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Surfaced via `mypy`, in that the `html.parser` module does not have
a direct implementation of `unescape`.
Refs: https://docs.python.org/3.6/library/html.html#html.unescape

In pypa#192 support for Python 2.7 was removed, the import path changed.

This works due to imports placing the imported code in the local scope.
If the `html.parser` module ever stopped importing `unescape`,
this import would break as a result.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Currently emits a warning:

    PytestRemovedIn8Warning: The --strict option is deprecated, use --strict-markers instead.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Surfaced by running mypy in strict mode, and added types where relevant.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Signed-off-by: Mike Fiedler <miketheman@gmail.com>
The types-docutils hints are still incomplete,
good progress is being made.
See: python/typeshed#7256

I've had to use an ignore on the class inheritance, and a couple of
`typing.Any` annotations until that package implements more type hints.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
`mypy` strict mode is having a hard time with the `distutils`
imports, since they are wrapped in `setuptools` right now as a private
package. This pacakge's distutils integration will need to be reworked
anyhow. Left a comment with details at the top of the file.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Prevent new things from creeping in during development.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Include a blank `py.typed` file in the package to inform `mypy` that
there's types to be found in this package.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
@di di mentioned this pull request Mar 10, 2022
Copy link
Member

@di di left a comment

Choose a reason for hiding this comment

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

Thank you! LGTM, I'll give this ~24 hours for others to review before merging.

Copy link
Contributor

@bhrutledge bhrutledge left a comment

Choose a reason for hiding this comment

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

Thanks for doing this! At @di's suggestion, I reviewed it based on my experience in Twine. I've got some questions/suggestions about style, but I don't think there's anything that should be considered a blocker.

I confirmed that using this branch allows Twine to remove its mypy ignore for this package.

readme_renderer/markdown.py Show resolved Hide resolved
readme_renderer/rst.py Outdated Show resolved Hide resolved
readme_renderer/clean.py Outdated Show resolved Hide resolved
readme_renderer/rst.py Outdated Show resolved Hide resolved
tox.ini Outdated Show resolved Hide resolved
pyproject.toml Show resolved Hide resolved
@miketheman
Copy link
Member Author

@bhrutledge Thanks for the review!

Allows other tools to ebenfit from a consistent configuration.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Signed-off-by: Mike Fiedler <miketheman@gmail.com>
pyproject.toml Show resolved Hide resolved
pyproject.toml Show resolved Hide resolved
readme_renderer/clean.py Outdated Show resolved Hide resolved
readme_renderer/rst.py Outdated Show resolved Hide resolved
Signed-off-by: Mike Fiedler <miketheman@gmail.com>
@miketheman
Copy link
Member Author

@di would appreciate the CI-approval to get the tests running to confirm.

Comment on lines +69 to +70
# The renderer is a lambda function, and mypy fails lambdas right now.
rendered = renderer(raw) # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you try to work around this? I wonder if Callable and/or cast would help. Or, maybe variants could be updated to use explicit private functions with annotations, instead of lambdas.

Copy link
Member Author

Choose a reason for hiding this comment

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

I did not try to modify the code to make the type annotations work - that felt counter to the effort at hand. mypy struggles with lambdas - see open issue: python/mypy#4226

It'd be awesome if you wanted to take on that as a follow-up once this is merged to remove the ignore!

Copy link
Contributor

@bhrutledge bhrutledge left a comment

Choose a reason for hiding this comment

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

Thanks again for taking my feedback; it was good exercise for me. :)

@di di merged commit 5d721e1 into pypa:main Mar 11, 2022
@di
Copy link
Member

di commented Mar 11, 2022

Released in https://pypi.org/project/readme-renderer/34.0/.

@miketheman miketheman deleted the miketheman/mypy branch March 11, 2022 22:07
@bhrutledge bhrutledge mentioned this pull request Mar 12, 2022
miketheman added a commit to miketheman/warehouse that referenced this pull request Jun 12, 2022
With the release of `readme_renderer` 35.0, we no longer need to exclude
the library from type evaluation.

Refs: pypa/readme_renderer#225
Refs: pypa/readme_renderer#228

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
di added a commit to pypi/warehouse that referenced this pull request Jun 12, 2022
With the release of `readme_renderer` 35.0, we no longer need to exclude
the library from type evaluation.

Refs: pypa/readme_renderer#225
Refs: pypa/readme_renderer#228

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

Co-authored-by: Dustin Ingram <di@users.noreply.github.com>
miketheman added a commit to miketheman/readme_renderer that referenced this pull request Aug 19, 2022
As of pypa#225, we now exposed type information to users of the library.
Let's also show it via classifier!

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
miketheman added a commit to miketheman/readme_renderer that referenced this pull request Aug 20, 2022
As of pypa#225, we now exposed type information to users of the library.
Let's also show it via classifier!

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
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.

Add type annotations
3 participants