spotify / pedalboard

🎛 🔊 A Python library for audio.
https://spotify.github.io/pedalboard
GNU General Public License v3.0
5.25k stars 261 forks source link

Type checking fails on any symbol exported by `pedalboard` #333

Open mttbernardini opened 6 months ago

mttbernardini commented 6 months ago

I created a minimum failing example here with a workflow matrix that tests using mypy, pyright, pyre and pytype. Of all the type checkers, only pyright is able to properly evaluate the snippet without raising errors and properly infer the type of gain:

import pedalboard

# mypy: Name "pedalboard.Plugin" is not defined [name-defined]
# pyre: Undefined or invalid type [11]: Annotation `pedalboard.Plugin` is not defined as a type.
gain: pedalboard.Plugin

# mypy: Module has no attribute "Gain" [attr-defined]
# pyre: Undefined attribute [16]: Module `pedalboard` has no attribute `Gain`.
gain = pedalboard.Gain()

# mypy, pyre, pytype: Revealed type is "Any"
# pyright: Type of "gain" is "Gain"
reveal_type(gain)

Versions:

hendriks73 commented 3 weeks ago

For mypy, I guess you would first have to run stubgen to get better type-checking. Unfortunately the generated stubs also have quite a few errors in them...

So what's missing, I guess, is some manually-checked/-authored pedalboard-stubs package.

After some more digging, I realized that the stubs are already there of course. But they are only picked up by mypy when using import pedalboard_native instead of import pedalboard.

Is there any reason not to use pedalboard_native? Or the other way around: Is there a way to tell mypy that what applies for pedalboard_native also applies to pedalboard?

mttbernardini commented 3 weeks ago

Or the other way around: Is there a way to tell mypy that what applies for pedalboard_native also applies to pedalboard?

According to PEP-561, Python packages containing stubs (i.e. only .pyi files) should use the -stubs suffix, quoting the PEP:

The name of the stub package MUST follow the scheme foopkg-stubs for type stubs for the package named foopkg

So I suppose pedalboard_native should be named pedalboard-stubs instead, for typing information to be properly picked by mypy & co, if that's the package meant to be stubs.