DetachHead / basedpyright

pyright fork with various type checking improvements, improved vscode support and pylance features built into the language server
https://docs.basedpyright.com
Other
1.02k stars 19 forks source link

generated stubs for mediapipe have broken imports #412

Open hermanumrao opened 3 months ago

hermanumrao commented 3 months ago

I am constantly facing issues like this: Screenshot from 2024-06-13 23-05-36

so I tried putting a pyrightconfig.json

{
  "typeCheckingMode": "strict",
  "stubPath": "/home/herman/stubs/"
}

which works half way but.. image

also I thought to put this into the basedpyright.lua so that I don't have to add this config file each time but that too doesn't work ( attached the file here) basedpyright.lua

also here is the location of stubs : image

generated all the stubs using

stubgen .local/lib/python3.10/site-packages/mediapipe -o stubs
DetachHead commented 3 months ago

this seems to be an issue with those stubs. i generated them the same way with stubgen and solutions does not seem to be re-exported from mediapipe which is causing your issue.

# stubs/mediapipe/__init__.pyi

__version__: str

generating them with pyright's stubgen instead seems to work slightly better though:

basedpyright --createstub mediapipe
# stubs/mediapipe/__init__.pyi

"""
This type stub file was generated by pyright.
"""

import mediapipe.tasks.python as tasks
from mediapipe.python import solutions as solutions

__version__ = ...

but unfortunately the solutions stub is empty, so you still get the same error:

# stubs/mediapipe/python/solutions/__init__.pyi

i'm not really familiar with how either of these stub generators work though, but i suspect the issue may be that the way mediapipe.python.solutions re-exports other modules is by using import statements instead of from/import statements:

"""MediaPipe Solutions Python API."""

import mediapipe.python.solutions.drawing_styles
import mediapipe.python.solutions.drawing_utils
import mediapipe.python.solutions.face_detection
import mediapipe.python.solutions.face_mesh
import mediapipe.python.solutions.face_mesh_connections
import mediapipe.python.solutions.hands
import mediapipe.python.solutions.hands_connections
import mediapipe.python.solutions.holistic
import mediapipe.python.solutions.objectron
import mediapipe.python.solutions.pose
import mediapipe.python.solutions.selfie_segmentation

instead of:

from mediapipe.python.solutions import drawing_styles
...
hermanumrao commented 3 months ago

I had the same issue with pyright's stub gen , that is why I used mypy it gave something better but I have not really checked the contents, 😕

DetachHead commented 3 months ago

we can see if we can try to improve pyright's stubgen, i'll re-purpose this issue for that. however in the mean time you will probably have to just manually fix up the generated stubs yourself

hermanumrao commented 3 months ago

we can see if we can try to improve pyright's stubgen, i'll re-purpose this issue for that. however in the mean time you will probably have to just manually fix up the generated stubs yourself

can you please guide me through that if possible? or if there are any sources I can refer please let me know

Thanks a ton in advance

DetachHead commented 3 months ago

maybe have a look through the type stub docs - this section at the bottom talks about manually fixing them up.

all this stuff is unchanged from upstream, but let me know if you have any further questions and maybe we can improve these docs a bit.

one thing that's worth mentioning which i don't see mentioned there is that re-exports should be defined using an alias with the same name:

from foo import bar # this import is only intended to be used within the current file and not meant to be re-exported

from foo import bar as bar # this import is intended to be re-exported

more info on that here

hermanumrao commented 3 months ago

generating them with pyright's stubgen instead seems to work slightly better though:

basedpyright --createstub mediapipe
# stubs/mediapipe/__init__.pyi

"""
This type stub file was generated by pyright.
"""

import mediapipe.tasks.python as tasks
from mediapipe.python import solutions as solutions

__version__ = ...

but unfortunately the solutions stub is empty, so you still get the same error:

# stubs/mediapipe/python/solutions/__init__.pyi

i'm not really familiar with how either of these stub generators work though, but i suspect the issue may be that the way mediapipe.python.solutions re-exports other modules is by using import statements instead of from/import statements:

"""MediaPipe Solutions Python API."""

import mediapipe.python.solutions.drawing_styles
import mediapipe.python.solutions.drawing_utils
import mediapipe.python.solutions.face_detection
import mediapipe.python.solutions.face_mesh
import mediapipe.python.solutions.face_mesh_connections
import mediapipe.python.solutions.hands
import mediapipe.python.solutions.hands_connections
import mediapipe.python.solutions.holistic
import mediapipe.python.solutions.objectron
import mediapipe.python.solutions.pose
import mediapipe.python.solutions.selfie_segmentation

instead of:

from mediapipe.python.solutions import drawing_styles
...

also I just checked my stubs follow this format ->

from mediapipe.python import *

__version__: str
from mediapipe.python.solutions import *

__version__: str
from mediapipe.python._framework_bindings import model_ckpt_util as model_ckpt_util, resource_util as resource_util
from mediapipe.python._framework_bindings.calculator_graph import CalculatorGraph as CalculatorGraph, GraphInputStreamAddMode as GraphInputStreamAddMode
from mediapipe.python._framework_bindings.image import Image as Image
from mediapipe.python._framework_bindings.image_frame import ImageFormat as ImageFormat, ImageFrame as ImageFrame
from mediapipe.python._framework_bindings.matrix import Matrix as Matrix
from mediapipe.python._framework_bindings.packet import Packet as Packet
from mediapipe.python._framework_bindings.timestamp import Timestamp as Timestamp
from mediapipe.python._framework_bindings.validated_graph_config import ValidatedGraphConfig as ValidatedGraphConfig
DetachHead commented 3 months ago

also I just checked my stubs follow this format ->

from mediapipe.python import *

__version__: str
from mediapipe.python.solutions import *

__version__: str
from mediapipe.python._framework_bindings import model_ckpt_util as model_ckpt_util, resource_util as resource_util
from mediapipe.python._framework_bindings.calculator_graph import CalculatorGraph as CalculatorGraph, GraphInputStreamAddMode as GraphInputStreamAddMode
from mediapipe.python._framework_bindings.image import Image as Image
from mediapipe.python._framework_bindings.image_frame import ImageFormat as ImageFormat, ImageFrame as ImageFrame
from mediapipe.python._framework_bindings.matrix import Matrix as Matrix
from mediapipe.python._framework_bindings.packet import Packet as Packet
from mediapipe.python._framework_bindings.timestamp import Timestamp as Timestamp
from mediapipe.python._framework_bindings.validated_graph_config import ValidatedGraphConfig as ValidatedGraphConfig

i'm assuming this is mediapipe/solutions/__init__.pyi generated by mypy's stubgen, i got a similar result. i think it's the same issue i mentioned in https://github.com/DetachHead/basedpyright/issues/412#issuecomment-2167165693, specifically with this import here:

from mediapipe.python.solutions import *

since that's not counted as a re-export, pyright doesn't recognize any imports from mediapipe.solutions that come from there. i also think that wildcard imports are generally discouraged so i would try to avoid using them in stubs anyway.

i would recommend starting with pyright's generated stubs instead because it looks like they'll be easier to fix since it seems more compatible with pyright's type checker.

here's an example of how you'd fix that module, starting with pyright's generated stubs:

before

import mediapipe.python.solutions.drawing_utils
import mediapipe.python.solutions.face_detection
import mediapipe.python.solutions.face_mesh
import mediapipe.python.solutions.face_mesh_connections
import mediapipe.python.solutions.hands
import mediapipe.python.solutions.hands_connections
import mediapipe.python.solutions.holistic
import mediapipe.python.solutions.objectron
import mediapipe.python.solutions.pose
import mediapipe.python.solutions.selfie_segmentation
from mediapipe.python.solutions import drawing_styles

after

from mediapipe.python.solutions import drawing_utils as drawing_utils
from mediapipe.python.solutions import face_detection as face_detection
from mediapipe.python.solutions import face_mesh as face_mesh
from mediapipe.python.solutions import face_mesh_connections as face_mesh_connections
from mediapipe.python.solutions import hands as hands
from mediapipe.python.solutions import hands_connections as hands_connections
from mediapipe.python.solutions import holistic as holistic
from mediapipe.python.solutions import objectron as objectron
from mediapipe.python.solutions import pose as pose
from mediapipe.python.solutions import selfie_segmentation as selfie_segmentation
from mediapipe.python.solutions import drawing_styles as drawing_styles