codekoriko
codekoriko

Reputation: 880

stub files present in package, still mypy complains: Cannot find implementation

package 1

I created a package my_utils and generated the stud files with the mypy utility stubgen

stubgen -p my_utils
# >>> Processed 2 modules
# >>> Generated files under out/my_utils/

moved the stubs to the package root dir

mv out/my_utils/* my_utils

mypy is happy about all

mypy my_utils
# >>> # Success: no issues found in 2 source files

package 2

package 2 have my_utils as a dependency so I installed my my_utils using poetry

poetry add git+ssh://[email protected]/username/my-utils.git

my virtual env library have the giving me the following package structure:

my_utils
├── __init__.py
├── __init__.pyi
├── os.py
├── os.pyi
└── py.typed

but still mypy complains when i check my package 2

mypy r26/bwf_converter.py
# r26/bwf_converter.py:12: error: Cannot find implementation or library stub for module named "my_utils.os"

__init__.pyi is empty and os.pyi has the following:

import csv
import json
from typing import Dict, List, Literal, Union

OutputProc = Literal["wait", "raw", "json", "csv", ""]
StdType = Literal["stderr", "stdout"]
JsonContent = Dict[str, str]
CsvContent = List[Dict[str, str]]

class PopenWrapper:
    cmd: Union[str, List[str]]
    ouput_proc: Literal["wait", "raw", "json", "csv", ""]
    log_output: bool
    kwargs: Dict[str, str]
    def __init__(self, cmd: Union[str, List[str]], ouput_proc: OutputProc = ..., log_output: bool = ..., **kwargs: Dict[str, str]) -> None: ...
    def run(self) -> Union[bool, str, CsvContent, JsonContent]: ...

If anyone could hint me about what Am I doing wrong...

Upvotes: 1

Views: 1991

Answers (1)

adam.hendry
adam.hendry

Reputation: 5653

To troubleshoot, install and use pyright. If you use vscode, pylance ships with pyright tools, so you don't have to install it. Just set

"python.analysis.typeCheckingMode": "strict",

in your settings.json to reveal deeper errors.

Since your package 2 is a 3rd-party package (installed, not local), PEP 561 applies:

  1. Your my-utils should be in your site-packages folder in your venv

  2. You have a py.typed, which is good as that is required by PEP 561:

Package maintainers who wish to support type checking of their code MUST add a marker file named py.typed to their package supporting typing. This marker applies recursively: if a top-level package includes it, all its sub-packages MUST support type checking as well.

stubgen often provides incomplete typing, so a couple things to check:

  1. Per PEP 561

If a stub package distribution is partial it MUST include partial\n in a py.typed file

Upvotes: 1

Related Questions