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

Mypy doesn't find type hints in imported module #9944

Closed
mvolfik opened this issue Jan 22, 2021 · 8 comments
Closed

Mypy doesn't find type hints in imported module #9944

mvolfik opened this issue Jan 22, 2021 · 8 comments
Labels
bug mypy got something wrong

Comments

@mvolfik
Copy link

mvolfik commented Jan 22, 2021

Bug Report

Mypy doesn't find type hints in my library.

To Reproduce

Pipfile:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
mypy = "==0.800"

[packages]
kayaclock-utils = {git = "https://github.com/kayaclock/kayaclock-utils.git", ref = "5658111116af089b3b74684a36e284adf6728504"}

[requires]
python_version = "3.8"

program.py:

from kayaclock_utils.data.basic_types import GateID

a = GateID(8)

Steps:

pipenv --python 3.8
pipenv update
pipenv run mypy .

The library itself is checked correctly with mypy configured to be strict and use pydantic plugin, see the full mypy.ini. If i copy this config into the directory with the bug setup as above, nothing changes (just an idea - there maybe should be a way for libraries to define what mypy plugins they recommend/require to typecheck correctly).

Expected Behavior

Everything typechecks correctly

Actual Behavior

program.py:1: error: Skipping analyzing 'kayaclock_utils.data.basic_types': found module but no type hints or library stubs
program.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.800
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none or strict + pydantic
  • Python version used: 3.8.5
  • Operating system and version: Ubuntu 20.04.1 LTS
@mvolfik mvolfik added the bug mypy got something wrong label Jan 22, 2021
@krassowski
Copy link

krassowski commented Jan 24, 2021

I experience potentially related issue with the newly release 0.800, the directives such as:

[mypy-pytest]
ignore_missing_imports = True

are no longer picked up from setup.cfg, also leading to found module but no type hints or library stubs error. It worked well with the previous version. Nothing else changed, the builds on CI just started failing overnight after 0.800 got picked up.

Could it be a regression in setup.cfg/Pipfile handling?

Edit: thanks @hauntsaninja! I don't know how I missed the other issue.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jan 24, 2021

That seems related to #9940. I think you can add a dummy [mypy] section as a workaround.

@hauntsaninja
Copy link
Collaborator

As for the original issue, do you have a py.typed? https://mypy.readthedocs.io/en/stable/installed_packages.html

@mvolfik
Copy link
Author

mvolfik commented Jan 24, 2021

As for the original issue, do you have a py.typed? https://mypy.readthedocs.io/en/stable/installed_packages.html

No, I don't. I tried searching if I needed to include anything for shipping a typed package before raising this. I didn't find this link, so I assumed it works automagically.

But after reading this, I'm still quite confused:

Generally, you do not need to do anything to use installed packages that support typing for the Python executable used to run mypy. Note that most packages do not support typing. Packages that do support typing should be automatically picked up by mypy and used for type checking.

So do I need the py.typed or not? If I do, the mypy error should say 'the installed package isn't marked with py.typed', instead of saying that there are no type hints, which definitely are there.

@hauntsaninja
Copy link
Collaborator

That's in the section for using, see the section for making https://mypy.readthedocs.io/en/stable/installed_packages.html#making-pep-561-compatible-packages

@mvolfik
Copy link
Author

mvolfik commented Jan 24, 2021

Oh, I see now, thanks. Not sure if this should be under configuring mypy though. Could you please make the error message a bit more helpful then? Something like "found installed package but not marked with py.typed"? And one more question - do I need to put this file into every subpackage, or is it enough at the top level?

package/
├─ __init__.py
├─ foo.py
├─ py.typed
└─ subpackage/
     ├─ __init__.py
     ├─ bar.py
     └─ py.typed # do i need this?

@MetRonnie
Copy link

For me, using --namespace-packages and --explicit-base-packages fixed these maddening "found module but no type hints or library stubs" errors

@castarco
Copy link

castarco commented Oct 4, 2021

I totally agree with @mvolfik , the error messages are highly uninformative and misleading.

JukkaL pushed a commit that referenced this issue Oct 4, 2021
Also re-order docs and make them more greppable

Helps with e.g. complaints in #9944

Co-authored-by: hauntsaninja <>
ilevkivskyi pushed a commit that referenced this issue Nov 16, 2021
Also re-order docs and make them more greppable

Helps with e.g. complaints in #9944

Co-authored-by: hauntsaninja <>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

5 participants