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 typing hints #83

Closed
wants to merge 4 commits into from
Closed

Add typing hints #83

wants to merge 4 commits into from

Conversation

WhyNotHugo
Copy link
Contributor

@WhyNotHugo WhyNotHugo commented May 26, 2021

This is an attempt at add type hints to the codebase.

I'm mostly doing this since I want to better understand some of the
helper functions so as to expose a few and make them reusable.

There's still a issues in _format_envvars and _generate_nodes. Both
of them take an argument and pass it to a conflicting function.

There's a second commit to this PR which has a change of which I'm not fully sure. That's not a typing change, but an actual logic change. It does work for me, however, I can't really say it's correct. It does remove one typing error.

Hugo Osvaldo Barrera added 2 commits May 26, 2021 21:27
This is an attempt at add type hints to the codebase.

I'm mostly doing this since I want to better understand some of the
helper functions so as to expose a few and make them reusable.

There's still a issues in `_format_envvars` and `_generate_nodes`. Both
of them take an argument and pass it to a conflicting function.
@WhyNotHugo
Copy link
Contributor Author

Any feedback for this?

@stephenfin
Copy link
Member

I won't get to this before the weekend at the earliest, but thanks for doing this. Will definitely review

Copy link
Member

@stephenfin stephenfin left a comment

Choose a reason for hiding this comment

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

This is a good start. Some small comments inline but crucially, this is untested. Can you add the relevant changes to tox.ini and setup.cfg to enable this? Something like this tox.ini should do:

[testenv:mypy]
deps =
    mypy
commands =
    mypy sphinx_click

Likewise, you probably want something like this in setup.cfg:

[mypy]
python_version = 3.8
disallow_incomplete_defs = True
show_column_numbers = True
show_error_context = True
ignore_missing_imports = True
follow_imports = skip
check_untyped_defs = True
warn_unused_ignores = True
strict_optional = False

from typing import List
from typing import Optional
from typing import Tuple
from typing import Union
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 importing the individual types, can you import typing and use that. i.e.

import typing as ty

from click import core

# ...

def _get_help_record(opt: core.Option) -> ty.Tuple[str, str]:
    # ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's the purpose of aliasing Tuple to ty.Tuple?
I don't mind that it's longer, but I fear that using a custom name for these common types will hurt readability with those unfamiliar with this codebase.

Copy link
Member

Choose a reason for hiding this comment

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

It's not aliasing, at least not for the classes - I tend to prefer importing modules rather than the classes themselves because it's less noisy. You could drop the aliasing of the module (so typing.Tuple but that seems unnecessarily verbose

@@ -256,7 +269,7 @@ def _get_lazyload_commands(multicommand):
return commands
Copy link
Member

Choose a reason for hiding this comment

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

You missed some inner methods

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not clear to me what _get_lazyload_commands handles, since it doesn't seem to run for the projects I'm using sphinx.

Using MultiCommand does not match here.

@stephenfin
Copy link
Member

Done in e31c727 and cdcfa3e

@stephenfin stephenfin closed this Apr 7, 2022
@WhyNotHugo WhyNotHugo deleted the typing branch April 7, 2022 17:58
@WhyNotHugo
Copy link
Contributor Author

Thanks!

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.

2 participants