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

[QUESTION] mypy complaining about missing attributes when importing typer #112

Closed
4 tasks done
j-martin opened this issue Jun 1, 2020 · 8 comments
Closed
4 tasks done
Labels
answered question Question or problem

Comments

@j-martin
Copy link

j-martin commented Jun 1, 2020

First check

  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Typer documentation, with the integrated search.
  • I already searched in Google "How to X in Typer" and didn't find any information.
  • I already searched in Google "How to X in Click" and didn't find any information.

Description

I am trying to use the type types get parsed in mypy. From my experience with mypy, the packages are either supported "out of the box" or not at all and must be ignored.

It's a different issue than #53

I am not sure if I am doing something wrong or this is a bug.

I am using mypy==0.770 on Python 3.7

Thanks,

Jean-Martin

Additional context

mypy command:

mypy --show-error-codes --strict some_script.py

mypy error (the line number will not match truncate script below)

some_script.py:10: error: Module has no attribute "Typer"  [attr-defined]
some_script.py:13: error: Untyped decorator makes function "main" untyped  [misc]
some_script.py:14: error: Function is missing a return type annotation  [no-untyped-def]
some_script.py:18: error: Module has no attribute "Option"  [attr-defined]
some_script.py:19: error: Module has no attribute "Option"  [attr-defined]
some_script.py:20: error: Module has no attribute "Option"  [attr-defined]
some_script.py:21: error: Module has no attribute "Option"  [attr-defined]
some_script.py:31: error: Module has no attribute "run"  [attr-defined]

mypy config

[mypy]
namespace_packages = true

# The following 3rd party dependencies don't have type hints. We ignore them one by one instead of
# blindly running with --ignore-missing-imports.

[mypy-typer]

[mypy-setuptools]
ignore_missing_imports = True

Truncated code:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json

import typer
import yaml

app = typer.Typer()


@app.command(
    help="Something."
)
def main(
    option_a: str = typer.Option(...),
    option_b: str = typer.Option(...),
    option_c: str = typer.Option(...),
    option_d: str = typer.Option(...),
) -> None:
    # Non problematic code
    ...


if __name__ == "__main__":
    typer.run(main)

Workaround

#112 (comment)

@j-martin j-martin added the question Question or problem label Jun 1, 2020
@mrpgraae
Copy link

mrpgraae commented Jun 9, 2020

I am also having this problem. It is my understanding, that since #58 typer should work with mypy out-of-the-box?

@kevincon
Copy link

I'm experiencing the same problem. I'm using typer==0.2.1 and mypy==0.780, and when I try to type check some code I've written that uses Typer, I get errors like error: Module has no attribute "Typer" for any symbol I've imported from the typer module.

@kevincon
Copy link

I believe this may be the issue: python/mypy#7030

In short, a recent version of mypy introduced a --no-implicit-reexport feature that that has become part of the default checks enabled when using --strict, which I guess everyone reporting this issue (including me) is using: https://mypy.readthedocs.io/en/latest/command_line.html#cmdoption-mypy-no-implicit-reexport

By default, imported values to a module are treated as exported and mypy allows other modules to import them. This flag changes the behavior to not re-export unless the item is imported using from-as or is included in all.

So I think this issue can be resolved if typer is updated to explicitly export the modules it imports in typer/__init__.py. I.e. change all of the imports in that file like:

https://github.com/tiangolo/typer/blob/338d05fdc756b51699847540888c7ef612849b7e/typer/__init__.py#L5

to:

from click.exceptions import Abort as Abort, BadParameter as BadParameter, Exit as Exit

@kevincon
Copy link

As a temporary workaround, you can add this section to your project's mypy.ini file to make mypy --strict happy:

[mypy-typer.*]
; https://github.com/tiangolo/typer/issues/112
implicit_reexport = True

@j-martin
Copy link
Author

I can confirm this fixed typing entirely with typer. Thank you so much!

@mrpgraae
Copy link

Can confirm this workaround fixed it for me too. Thanks!

@tiangolo
Copy link
Member

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

Available in Typer version 0.3.2 🚀 🎉

@j-martin
Copy link
Author

Confirming it solved the mypy issue.

Thanks!

akaihola added a commit to akaihola/darker that referenced this issue Mar 29, 2021
MyPy strict mode now disallows implicit re-export.
See fastapi/typer#112 (comment)
akaihola added a commit to akaihola/darker that referenced this issue Apr 3, 2021
MyPy strict mode now disallows implicit re-export.
See fastapi/typer#112 (comment)
akaihola added a commit to akaihola/darker that referenced this issue Apr 3, 2021
MyPy strict mode now disallows implicit re-export.
See fastapi/typer#112 (comment)
akaihola added a commit to akaihola/darker that referenced this issue Apr 3, 2021
MyPy strict mode now disallows implicit re-export.
See fastapi/typer#112 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered question Question or problem
Projects
None yet
Development

No branches or pull requests

4 participants