Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mypy and distributing a namespace package

I have a set of namespace packages that are meant to run in a python3.6 environment.

They are each set up as follows:

if sys.version_info < (3, 6):
    print("Python versions < 3.6 unsupported", file=sys.stderr)
    sys.exit(1)

setup(
    name="mynamespace.subpackage",
    version=VERSION,

    packages=[
        "mynamespace.subpackage",
    ],
    package_dir={"": "src"},
    package_data={
        "": [],
    },
    include_package_data=True,
    zip_safe=False,

    install_requires=[
        "mynamespace.core",  # May have explicit dependencies that are not cyclic
    ],

    namespace_packages=["mynamespace"],
    ...
)

All of the subpackages install just fine side-by-side.

The problem comes when I want to get robust type-checking via mypy. mypy is unable to find the mynamespace.core subpackage when being run on the source files for mynamespace.subpackage (for example) which means that I don't get robust typing checking across my sub-package boundaries.

This appears to be a known problem: https://github.com/python/mypy/issues/1645

Guido mentions that the workaround is to "add a dummy __init__.py or __init__.pyi files", but he doesn't really elaborate and it turns out that this isn't as obvious to me as I had hoped. Adding those files to the local repo allows mypy to run over the local repo as expected, I can't figure out how to get access to the typing information in a sibling namespace package.

My question is: how would I modify mynamespace.core -- so that when installed, mypy is able to pick up it's type information in other modules?

like image 555
mgilson Avatar asked Nov 08 '22 10:11

mgilson


1 Answers

Hopefully you've fixed it by now (or given up!!) but for any fellow dwellers, the answer is to run:

mypy --namespace-packages -p mynamespace.subpackage

Note that when you use the -p arg you cannot specify a directory as well.

like image 88
Constantinos Avatar answered Nov 15 '22 06:11

Constantinos