pacti-org / pacti

A package for compositional system analysis and design
https://www.pacti.org
BSD 3-Clause "New" or "Revised" License
19 stars 5 forks source link

[BUG] missing library stubs or py.typed marker #343

Open NicolasRouquette opened 8 months ago

NicolasRouquette commented 8 months ago

Describe the bug A clear and concise description of what the bug is.

With VsCode configured for type analysis with mypy, we get several problems with Pacti imports.

from pacti.contracts import PolyhedralIoContract, PolyhedralIoContractCompound
from pacti.terms.polyhedra import PolyhedralTermList
from pacti.utils.lists import list_union

Each of the above line produces an analysis error like this:

[{
    "resource": "/C:/opt/local/github.pacti/pacti-instrumentation/src/pacti_instrumentation/pacti_counters.py",
    "owner": "_generated_diagnostic_collection_name_#11",
    "code": {
        "value": "import-untyped",
        "target": {
            "$mid": 1,
            "path": "/en/latest/_refs.html",
            "scheme": "https",
            "authority": "mypy.readthedocs.io",
            "fragment": "code-import-untyped"
        }
    },
    "severity": 8,
    "message": "Skipping analyzing \"pacti.contracts\": module is installed, but missing library stubs or py.typed marker",
    "source": "Mypy",
    "startLineNumber": 2,
    "startColumn": 1,
    "endLineNumber": 2,
    "endColumn": 2
}]

To Reproduce

Expected behavior A clear and concise description of what you expected to happen.

Pacti imports should yield no type analysis errors with mypy.

Screenshots If applicable, add screenshots to help explain your problem.

System (please complete the following information):

Additional context Add any other context about the problem here.

iincer commented 8 months ago

Thanks, @NicolasRouquette, for reporting this. Just to make sure we are on the same page, I recall make check-types is okay in main---see this run. When we run make check-types, we execute mypy with the configuration file config/mypy.ini. Would it be sufficient to add this configuration to VS Code for its integration with mypy to be clean? If yes, perhaps we should add a VS Code configuration file to the project. Also, do you know what is the relation between Pylance and VS Code's integration with mypy? I recall Pylance does some type checking for Python in VS Code.

NicolasRouquette commented 8 months ago

The problem manifests in VS Code but the root cause is in the lack of published stubs or py-typed markers: https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-library-stubs-or-py-typed-marker

That is, we need Pacti to publish such stubs or markers. There is a semi-automated process that involves two steps:

I tested this approach for the pacti-instrumentation repo.

pdm run stubgen .\src\pacti_instrumentation\pacti_counters.py
Processed 1 modules
Generated out\pacti_instrumentation/pacti_counters.pyi

Then we have to manually review the generated stub in the out folder and copy it in the src folder like this: https://github.com/pacti-org/pacti-instrumentation/tree/master/src/pacti_instrumentation

Since we have to review/revise the generated stubs, we could have CI run stubgen and provide a report about differences, if any, but not fail the workflow since some differences may be intentional.