opentelemetry-python: PyLance / mypy doesn't accept `opentelemetry` as a namespace package
The opentelemetry-api
package, exporting opentelemetry
doesn’t successfully import into vscode.
-
In vanilla VSCode, the
import opentelemetry
is white -
In VSCode with mypy / pylance enabled with strict mode:
gives

It would seem this was solved a way back https://github.com/microsoft/pylance-release/issues/555 but it’s not
Describe your environment Describe any aspect of your environment relevant to the problem, including your Python version, platform, version numbers of installed dependencies, information about your cloud hosting provider, etc. If you’re reporting a problem with a specific version of a library in this repo, please check whether the problem has been fixed on main.
- Python v3.9.10, with venv
- VSCode on macOS
- Settings:
- Latest of all dependencies I could find
Steps to reproduce
Install “opentelemetry” and see the import in VSCode.
What is the expected behavior?
I’d expect typing to work.
Additional context Add any other context about the problem here.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 3
- Comments: 18 (13 by maintainers)
I opened this PR to see if we can add the necessary docs for mypy use: https://github.com/open-telemetry/opentelemetry.io/pull/1611
@sk- Thanks I figured out the same but forgot to post here. I find it a bit surprising that mypy requires you to set flags depending on details of your dependencies but that seems more like a mypy issue than a opentelemetry one.
As in it’s way more obvious that I have to set the namespace flag if I am typechecking a package that is it self a namespace package than when typechecking a package that happens to depend on a namespace package
@jenshnielsen for mypy you need to set
namespace_packages = True
or the command line option--namespace-packages
. See https://github.com/open-telemetry/opentelemetry-python/issues/1608#issuecomment-1075799695I also had to add:
It’s a pity this info is not readily available in docs.
I ran into an issue typechecking import from opentelemetry-sdk
Running
where test.py simply contains an import from opentelemetry-sdk
results in
Adding a
__init__.pyi
note the i to the toplevelfolder of opentelemetry here https://github.com/open-telemetry/opentelemetry-python/tree/main/opentelemetry-api/src/opentelemetry whould resolve the issue and the code type checks correctly.Are there strong arguments against adding such a file ? I understand thata adding a
__init__.py
file is not a good idea but addign a__init__.pyi
file seems fine? I note that opentelemetry-sdk it self ships such a file here which seems to have been created when that package it self was converted to a namespace package.Seen with
python 3.8 and the following versions
This was fixed in https://github.com/microsoft/pylance-release/issues/2562 — and there’s an action point for this project to be more PEP-compatible:
Then it should all work