简体   繁体   English

mypy 并分发命名空间包

[英]mypy and distributing a namespace package

I have a set of namespace packages that are meant to run in a python3.6 environment.我有一组要在 python3.6 环境中运行的命名空间包。

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健壮的类型检查时,问题就出现了。 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. mypy无法找到mynamespace.core子包被在源文件运行时mynamespace.subpackage (例如),这意味着我不明白在我的分装的界限强大的打字检查。

This appears to be a known problem: https://github.com/python/mypy/issues/1645这似乎是一个已知问题: 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. Guido 提到解决方法是“添加一个虚拟的__init__.py__init__.pyi文件”,但他并没有详细说明,事实证明这对我来说并不像我希望的那么明显。 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.将这些文件添加到本地存储库允许 mypy 按预期运行本地存储库,我不知道如何访问同级命名空间包中的输入信息。

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?我的问题是:我将如何修改mynamespace.core —— 以便在安装时, mypy能够在其他模块中获取它的类型信息?

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.请注意,当您使用-p arg 时,您也不能指定目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM