Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Discovery strategy is still mysterious w/PEP 420 namespace packages #6989

Closed
@warsaw

Description

@warsaw

I'm having trouble understanding some of the subtleties of how mypy 0.701 (w/Python 3.7) discovers files to check. Here is a sample directory tree:

.
└── lipy-foo
    ├── setup.cfg
    └── src
        └── linkedin
            └── foo
                ├── __init__.py
                └── bar.py

linkedin is a PEP 420 namespace package and linkedin/foo/__init__.py is empty. bar.py contains:

def bar(a: int, b: int) -> str:
    return a + b

When I cd into the lipy-foo directory containing the setup.cfg, and try to run mypy, it can't find any .py files to check:

% ls
setup.cfg	src/
% mypy src --namespace-packages -v
There are no .py[i] files in directory 'src'

Clearly mypy doesn't recurse into the src directory to find the linkedin namespace package. Other options that don't work include:

% mypy -p linkedin --namespace-packages
Can't find package 'linkedin'
% MYPYPATH=src mypy -p linkedin --namespace-packages
Can't find package 'linkedin'

What does work:

% mypy src/linkedin --namespace-packages
src/linkedin/foo/bar.py:2: error: Incompatible return value type (got "int", expected "str")

Also, if I add an empty src/linkedin/__init__.py file, discovery does work as expected:

% tree
.
├── setup.cfg
└── src
    └── linkedin
        ├── __init__.py
        └── foo
            ├── __init__.py
            └── bar.py
% mypy src --namespace-packages --no-site-packages
src/linkedin/foo/bar.py:2: error: Incompatible return value type (got "int", expected "str")

So PEP 420 style namespace packages do seem to influence the behavior here.

Since we may have additional directories and namespace packages under src in other repositories, I'd prefer not to have to invoke mypy with src/linkedin, but there doesn't seem to be any other option.

Maybe I'm doing something wrong, so please tell me if this is pebkac, or is unsupported. Should mypy be able to discover the linkedin namespace package using src-style package layouts with PEP 420 namespaces?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions