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

[FEATURE] Add __all__ definition to __init__ #142

Closed
martsa1 opened this issue Jul 16, 2020 · 7 comments
Closed

[FEATURE] Add __all__ definition to __init__ #142

martsa1 opened this issue Jul 16, 2020 · 7 comments
Labels
answered feature New feature, enhancement or request

Comments

@martsa1
Copy link

martsa1 commented Jul 16, 2020

Is your feature request related to a problem

Yes

When running a fairly strict mypy configuration, mypy complains that the typer module is missing all its attributes.

The solution you would like

We can resolve this by adding a __all__ definition to the top level init.

Describe alternatives you've considered

Using a less strict mypy configuration doesn't yield these errors, but that then allows typing compromises to slip into my code elsewhere.

Additional context

Sample mypy config:

[mypy]
allow_redefinition = False
check_untyped_defs = True
disallow_any_generics = True
disallow_any_unimported = True
disallow_incomplete_defs = True
disallow_subclassing_any = True
disallow_untyped_calls = True
disallow_untyped_decorators = True
disallow_untyped_defs = True
ignore_errors = False
ignore_missing_imports = True
implicit_reexport = False
local_partial_types = True
mypy_path = type_stubs
no_implicit_optional = True
strict_equality = True
strict_optional = True
warn_no_return = True
warn_redundant_casts = True
warn_return_any = True
warn_unreachable = True
warn_unused_configs = True
warn_unused_ignores = True

Sample script:

import typer


def main() -> None:
    typer.secho("hello, world!", fg=typer.colors.GREEN)


if __name__ == "__main__":
    main()

Mypy Errors:

$ mypy sample.py
sample.py:5: error: Module has no attribute "secho"; maybe "echo"?
Found 1 error in 1 file (checked 1 source file)

Adding the following to typer/__init__.py:

__all__ = [
    "Abort",
    "Argument",
    "BadParameter",
    "CallbackParam",
    "Context",
    "Exit",
    "FileBinaryRead",
    "FileBinaryWrite",
    "FileText",
    "FileTextWrite",
    "Option",
    "Typer",
    "clear",
    "colors",
    "confirm",
    "echo",
    "echo_via_pager",
    "edit",
    "format_filename",
    "get_app_dir",
    "get_binary_stream",
    "get_terminal_size",
    "get_text_stream",
    "getchar",
    "launch",
    "open_file",
    "pause",
    "progressbar",
    "prompt",
    "run",
    "secho",
    "style",
    "unstyle",
]

Resolves the above error & any others like it.

Typer version: 0.2.1, mypy version: 0.782, python: 3.6.10

@martsa1 martsa1 added the feature New feature, enhancement or request label Jul 16, 2020
@victorphoenix3
Copy link
Contributor

I could work on this!

@martsa1
Copy link
Author

martsa1 commented Jul 22, 2020

If no-one gets to it I'll eventually raise a PR. I was thinking it would be worth a test to verify that the __all__ is up to date with the imports as part of such a PR. It wouldn't take very long (assuming the above list is correct), and would help ensure that any future import additions/removals are kept up to date.

I might have time the weekend after this, but if you're likely to get to it first, by all means go for it. 😄

@victorphoenix3
Copy link
Contributor

  • The issue arises because of the --no-implicit-reexport flag that is enabled during a --strict mypy run. It does not allow re-exporting imported modules unless the item is imported using import-x-as-x or is included in __all__ .
    In my PR I have used import x as x rather than __all__ which requires extra maintenance.
  • Another option is to add implicit_reexport = True to the mypy.ini file and the import/export errors don't arise even with the strict configuration.

@victorphoenix3
Copy link
Contributor

@tiangolo

@pmav99
Copy link

pmav99 commented Aug 3, 2020

this is a probably a duplicate of #112

@tiangolo
Copy link
Owner

Thanks for the discussion here everyone! @victorphoenix3 added support for mypy --strict on #147.

Available in Typer version 0.3.2 🚀 🎉

@martsa1
Copy link
Author

martsa1 commented Aug 16, 2020

@victorphoenix3 Thanks for getting to this! Your implementation is much nicer than anything I would have put together!

@tiangolo Thanks for merging, and thanks for the awesome Typer. 👍

@martsa1 martsa1 closed this as completed Aug 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered feature New feature, enhancement or request
Projects
None yet
Development

No branches or pull requests

4 participants