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

Module "jaraco" has no attribute "env" (from jaraco import env) #15970

Closed
jaraco opened this issue Aug 26, 2023 · 6 comments
Closed

Module "jaraco" has no attribute "env" (from jaraco import env) #15970

jaraco opened this issue Aug 26, 2023 · 6 comments
Labels
bug mypy got something wrong

Comments

@jaraco
Copy link
Member

jaraco commented Aug 26, 2023

Bug Report

In jaraco/pip-run#79, I noticed that the mypy tests started failing and traced the emergent failure to enhancements in the typing of a sibling submodule.

To Reproduce

import jaraco.functools
from jaraco import env
 draft @ pip-run mypy 'jaraco.functools==3.9' jaraco.env -- -m mypy --ignore-missing-imports test.py
test.py:2: error: Module "jaraco" has no attribute "env"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Run instead with jaraco.functools<3.9 and mypy succeeds:

 draft @ pip-run mypy 'jaraco.functools<3.9' jaraco.env -- -m mypy --ignore-missing-imports test.py
Success: no issues found in 1 source file

jaraco.functools 3.9 moved functools to its own submodule and declared that module as typed (jaraco/jaraco.functools#22).

Expected Behavior

Changes to a sibling submodule shouldn't affect the reliable checking when importing a module. mypy should detect from module import submodule as valid.

Your Environment

  • Mypy version used: 1.5.1
  • Mypy command-line flags: --ignore-missing-imports
  • Mypy configuration options from mypy.ini (and other config files): none in repro, but some in project.
  • Python version used: 3.11.4
@jaraco jaraco added the bug mypy got something wrong label Aug 26, 2023
@jaraco
Copy link
Member Author

jaraco commented Aug 26, 2023

Removing the py.typed from jaraco.functools bypasses the issue:

 draft @ py -3.11 -m venv .venv
 draft @ py -m pip install mypy jaraco.functools jaraco.env
...
Successfully installed jaraco.env-1.0.0 jaraco.functools-3.9.0 more-itertools-10.1.0 mypy-1.5.1 mypy-extensions-1.0.0 typing-extensions-4.7.1

 draft @ ls .venv/lib/python3.11/site-packages/jaraco/functools
__init__.py  __init__.pyi __pycache__  py.typed
 draft @ rm .venv/lib/python3.11/site-packages/jaraco/functools/py.typed
 draft @ py -m mypy --ignore-missing-imports test.py
Success: no issues found in 1 source file

@jaraco
Copy link
Member Author

jaraco commented Aug 26, 2023

Note that jaraco is a PEP 420 namespace package.

@jaraco
Copy link
Member Author

jaraco commented Aug 26, 2023

Here's something interesting. If I install jaraco.functools and jaraco.env into ., the issue doesn't occur:

 draft @ rm -r .venv
 draft @ ls
test.py
 draft @ pip install --quiet --target . jaraco.functools==3.9 jaraco.env
 draft @ mypy --ignore-missing-imports test.py
Success: no issues found in 1 source file

It only occurs if jaraco.functools and jaraco.env are installed to site-packages. That's weird, right?

@jaraco
Copy link
Member Author

jaraco commented Aug 26, 2023

I am able to replicate the issue more simply, without the official versions of jaraco.functools or jaraco.env with empty modules thus:

 draft @ py -3.11 -m venv .venv
 draft @ mkdir -p .venv/lib/python3.11/site-packages/jaraco/functools
 draft @ touch .venv/lib/python3.11/site-packages/jaraco/env.py
 draft @ touch .venv/lib/python3.11/site-packages/jaraco/functools/__init__.py
 draft @ touch .venv/lib/python3.11/site-packages/jaraco/functools/py.typed
 draft @ py -m pip install -q mypy

[notice] A new release of pip is available: 23.0.1 -> 23.2.1
[notice] To update, run: /Users/jaraco/draft/.venv/bin/python -m pip install --upgrade pip
 draft @ py -m mypy test.py
test.py:2: error: Module "jaraco" has no attribute "env"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

jaraco added a commit to jaraco/pip-run that referenced this issue Aug 26, 2023
Suppress the spurious error from mypy as reported in python/mypy#15970. Closes #22.
jaraco added a commit to jaraco/pip-run that referenced this issue Aug 26, 2023
Suppress the spurious error from mypy as reported in python/mypy#15970. Closes #79.
@tmke8
Copy link
Contributor

tmke8 commented Aug 27, 2023

My guess: both jaraco.env and jaraco.functools were previously untyped, so mypy just raised a missing-import for it (which you're ignoring) and moved on. But now, jaraco.functools is typed, and I guess mypy now also treats jaraco as typed? And so it looks for jaraco.env but that is still not typed, so mypy can't find it. So, the import error turned into an attribute error.

@asottile-sentry
Copy link

likely duplicate of #10360

@jaraco jaraco closed this as not planned Won't fix, can't repro, duplicate, stale May 3, 2024
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

3 participants