nutti / fake-bpy-module

Fake Blender Python API module collection for the code completion.
MIT License
1.35k stars 96 forks source link

Please mark generated code with py.typed for mypy type checking #92

Closed walles closed 3 years ago

walles commented 3 years ago

As a developer, I like to use mypy to type check my Python code. This converts various runtime problems into much simpler compile time problems and saves me time.

For mypy to use the stubs generated by fake-bpy-module, the resulting bpy package needs to come with a py.typed marker file.

The good news is that a lot of the bpy.types already come with type annotations (yay!), and adding this file here is the only thing left to let mypy benefit from them.

Example

Note the (empty) py.typed file I manually added in here.

~/s/find_bad_motion_tracks (main|✚1) $ ls -l env/lib/python3.9/site-packages/bpy
total 5240
-rw-r--r--   1 johan  staff      279 30 Jul 15:07 __init__.py
drwxr-xr-x   8 johan  staff      256 30 Jul 15:07 __pycache__
drwxr-xr-x   8 johan  staff      256 30 Jul 15:07 app
-rw-r--r--   1 johan  staff     4881 30 Jul 15:07 context.py
-rw-r--r--   1 johan  staff     1116 30 Jul 15:07 msgbus.py
drwxr-xr-x  77 johan  staff     2464 30 Jul 15:07 ops
-rw-r--r--   1 johan  staff     3595 30 Jul 15:07 path.py
-rw-r--r--   1 johan  staff    21901 30 Jul 15:07 props.py
-rw-r--r--   1 johan  staff        0 30 Jul 16:38 py.typed    <--- PLEASE ADD THIS EMPTY MARKER FILE
-rw-r--r--   1 johan  staff  2634790 30 Jul 15:07 types.py
drwxr-xr-x   6 johan  staff      192 30 Jul 15:07 utils
~/s/find_bad_motion_tracks (main|✚1) $

Are you willing to contribute about this feature. (Yes/No)

Possibly.

nutti commented 3 years ago

@walles

It seems good enhancement to me. Actually, I have not used mypy. So, I would like to know why adding py.typed file is useful.

Does py.typed reduce the process time from mypy? Is py.typed useful for other type checking tools (e.g. VSCode)?

walles commented 3 years ago

You should use mypy, Guido of I-started-Python fame is the number four top contributor :).

Anyway, adding py.typed moves mypy from "doesn't work at all" to "produces useful typing warnings".

Don't know about other tools, but personally I have the "Python > Linting: Mypy Enabled" box checked in VSCode. And without py.typed its helpfulness is limited, so from my perspective putting a py.typed file in there would definitely improve my VSCode experience.

Also, having the py.typed file is part of PEP-561, and following that PEP at least shouldn't hurt any other tool:

Package maintainers who wish to support type checking of their code MUST add a marker file named py.typed to their package supporting typing.

Examples

Without py.typed

On my Find-Bad Motion Tracks Blender add-on.

(env) ~/s/find_bad_motion_tracks (main|✔) $ mypy __init__.py
__init__.py:14: error: Skipping analyzing "bpy": found module but no type hints or library stubs
__init__.py:14: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
__init__.py:19: error: Skipping analyzing "bpy.types": found module but no type hints or library stubs
Found 2 errors in 1 file (checked 1 source file)

With py.typed

(env) ~/s/find_bad_motion_tracks (main|✔) [1] $ mypy __init__.py
Success: no issues found in 1 source file
(env) ~/s/find_bad_motion_tracks (main|✔) $
nutti commented 3 years ago

Thanks for your detail information. I will tackle this from now.

nutti commented 3 years ago

This enhancement has been merged.