Title: Importing a regular package from a namespace package seems to break intermittently · Issue #10056 · python/mypy · GitHub
Open Graph Title: Importing a regular package from a namespace package seems to break intermittently · Issue #10056 · python/mypy
X Title: Importing a regular package from a namespace package seems to break intermittently · Issue #10056 · python/mypy
Description: Bug Report If you have a library that creates a namespace package (say ns_package) with a regular type-hinted package inside it (say reg_package), and you import the regular package as from ns_package import reg_package, mypy sometimes g...
Open Graph Description: Bug Report If you have a library that creates a namespace package (say ns_package) with a regular type-hinted package inside it (say reg_package), and you import the regular package as from ns_pack...
X Description: Bug Report If you have a library that creates a namespace package (say ns_package) with a regular type-hinted package inside it (say reg_package), and you import the regular package as from ns_pack...
Opengraph URL: https://github.com/python/mypy/issues/10056
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Importing a regular package from a namespace package seems to break intermittently","articleBody":"**Bug Report**\r\nIf you have a library that creates a namespace package (say `ns_package`) with a regular type-hinted package inside it (say `reg_package`), and you import the regular package as `from ns_package import reg_package`, mypy sometimes gives the following error:\r\n\r\n```\r\nscript.py:1: error: Skipping analyzing 'ns_package': found module but no type hints or library stubs\r\nscript.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports\r\nFound 1 error in 1 file (checked 1 source file)\r\n```\r\n\r\nThe error only shows up intermittently. More details below.\r\n\r\n**To Reproduce**\r\n\r\n1. Create the following directory tree:\r\n```\r\n.\r\n├── lib/\r\n│ ├── ns_package/\r\n│ │ └── reg_package/\r\n│ │ ├── __init__.py\r\n│ │ └── py.typed\r\n│ └── setup.py\r\n└── script.py\r\n\r\n3 directories, 4 files\r\n```\r\nwith content\r\n```python\r\n# lib/ns_package/reg_package/__init__.py\r\nSOMETHING: int = 345\r\n\r\n# lib/setup.py\r\nfrom setuptools import setup, find_namespace_packages\r\n\r\nsetup(\r\n name=\"ns_package.reg_package\",\r\n description=\"\",\r\n version=\"1.0\",\r\n packages=find_namespace_packages(),\r\n zip_safe=False,\r\n package_data={\"\": [\"py.typed\"]},\r\n)\r\n\r\n# script.py\r\nfrom ns_package import reg_package\r\n\r\na = reg_package.SOMETHING\r\n\r\nprint(a)\r\n```\r\n2. Create venv, activate and install the lib and mypy: `python3.8 -m venv .venv; . .venv/bin/activate; python3.8 -m pip install -e ./lib/ mypy`\r\n3. Run mypy against `script.py` with `python3.8 -m mypy -p script`\r\n\r\n**Expected Behavior**\r\n\r\nMypy should report no errors since the library has a `py.typed` file and type annotations.\r\n\r\n**Actual Behavior**\r\n\r\nMypy's result changes with every run. Sometimes no error is reported. Sometimes the following error is reported:\r\n```\r\nscript.py:1: error: Skipping analyzing 'ns_package': found module but no type hints or library stubs\r\nscript.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports\r\nFound 1 error in 1 file (checked 1 source file)\r\n```\r\nFrom what I can tell, the error is **always** reported in every other run, consistently. Meaning if I run mypy twice in a row, one of them always fails and one of them always succeeds. This seems to suggest that mypy somehow maintains state from one run to the next?\r\n\r\nCouple of points to note:\r\n\r\n- If the import statement is changed to `import ns_package.reg_package as reg_package` or `from ns_package.reg_package import SOMETHING`, the error is gone.\r\n- Adding the `--namespace-packages` CLI flag doesn't change the result.\r\n- Whether the lib is installed as an editable package doesnt change the result.\r\n- Whether its installed from local dir or PyPi doesn't change the result.\r\n- If `ns_package` is just a directory in the cwd instead of an installed package, and the `--namespace-packages` CLI flag is passed, the error is gone.\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: 0.800\r\n- Mypy command-line flags: `-p script`\r\n- Mypy configuration options from `mypy.ini` (and other config files): None\r\n- Python version used: 3.8.6\r\n- Operating system and version: Red Hat Enterprise Linux Server 7.9 (Maipo)\r\n","author":{"url":"https://github.com/Cnoor0171","@type":"Person","name":"Cnoor0171"},"datePublished":"2021-02-09T23:31:51Z","url":"https://github.com/python/mypy/issues/10056"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ec09fe1f-eafb-10a5-95f8-eaacf27e315f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9518:36D598:2CDC:3CA6:6B00192E |
| html-safe-nonce | c286d5da0f8f91290def8b9c6c9ad532fac925944392cd7c1f7978c28457064e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NTE4OjM2RDU5ODoyQ0RDOjNDQTY6NkIwMDE5MkUiLCJ2aXNpdG9yX2lkIjoiMTI4NzM4ODE4MzU5MTM5MzU4MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 90ddfda322c877d3964d295d79743dfd4d3cc1bfe09155c42c5bf85353e1db3d |
| hovercard-subject-tag | issue:805013724 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/10056/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a032cc0187529c17dd38ef5768b4fdb24fdf9280996e1a9b9f206b598903e15c/python/mypy/issues/10056 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a032cc0187529c17dd38ef5768b4fdb24fdf9280996e1a9b9f206b598903e15c/python/mypy/issues/10056 |
| og:image:alt | Bug Report If you have a library that creates a namespace package (say ns_package) with a regular type-hinted package inside it (say reg_package), and you import the regular package as from ns_pack... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Cnoor0171 |
| hostname | github.com |
| expected-hostname | github.com |
| None | a2dd2291b1b13cc9c06f27ec92fa3dbaf34116fc24c9347b8539db61884c59f4 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 70121d4e855ee0bb7b75cf15d061d91f8863c0f8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width