遵循PEP-561开发类型感知包时mypy导入忽略错误排查
Let’s tackle this mypy error step by step—first, yes, this error is expected in your current setup, but it’s fully fixable by ensuring mypy correctly recognizes your owclient package as a PEP-561-compliant typed module. Here’s what’s going on and how to resolve it:
Why the Error Happens
When you run mypy with --follow-imports=error, it enforces strict rules for imports: it will only recognize modules that are either:
- Part of the Python standard library,
- Installed in your environment with proper type hints (including a
py.typedmarker for PEP-561), - Explicitly passed to mypy via the command line.
In your test project, mypy isn’t picking up owclient as a valid typed package—even though it works in the package’s own mypy check—because the test environment isn’t properly set up to expose owclient’s type information to mypy.
Step-by-Step Fixes
1. Install owclient in Editable Mode in Your Test Environment
If you’re working locally (not using a published version), you need to install owclient in editable (development) mode so mypy can find its py.typed files and type hints.
- If using Poetry in your test project:
poetry add --editable ../path/to/owclient - If using pip:
pip install -e /path/to/owclient
This links your local owclient code to the test environment, so mypy can resolve the import and access the type markers.
2. Verify py.typed Files Are Properly Configured
Your setup.py includes py.typed in package_data, but double-check:
- Each package directory (
owclient/,owclient/devices/,owclient/exc/) has an emptypy.typedfile. This file tells mypy the package is typed, per PEP-561. - Remove the unnecessary
'*': ['pyproject.toml']line frompackage_data—mypy doesn’t need this file, and it could cause unexpected behavior.
3. Adjust Mypy Configuration for Strict Mode
If you want to keep using --follow-imports=error, explicitly tell mypy to trust owclient’s type information by adding a config file to your test project:
Create a pyproject.toml or mypy.ini with:
[tool.mypy] strict = true follow_imports = error [mypy-owclient.*] ignore_missing_imports = false
This ensures mypy doesn’t ignore the owclient import and checks against its type hints.
4. Resolve the Poetry Issue (Optional, to Return to Poetry Build)
You mentioned hitting Poetry issue #1338—first, update Poetry to the latest version, as many old package-data bugs have been fixed:
poetry self update
Then configure your owclient pyproject.toml correctly for PEP-561:
[tool.poetry] name = "owclient" version = "0.1.8" description = "A light layer to use OWFS and pyownet with a more OOP approach." authors = ["Ferran Comabella <ferran@gmail.com>"] repository = "https://gitlab.com/fcomabella/ow-client" readme = "README.rst" [tool.poetry.dependencies] python = ">=3.8,<4.0" pyownet = ">=0.10.0,<0.11.0" [tool.poetry.package-data] owclient = ["py.typed"] "owclient.devices" = ["py.typed"] "owclient.exc" = ["py.typed"] [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api"
Now poetry build will generate a package with the correct py.typed files, and installing it (even locally) will make mypy recognize it as a typed package.
Final Check
After making these changes, re-run your mypy command in the test project’s Poetry shell:
mypy test-owclient.py --follow-imports=error
The error should be gone, and mypy will start checking type hints for owclient usage in your test code.
内容的提问来源于stack exchange,提问作者Ferran Comabella

