Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mypy stub files to PyAV #642

Closed
tooxo opened this issue Apr 28, 2020 · 17 comments
Closed

Add mypy stub files to PyAV #642

tooxo opened this issue Apr 28, 2020 · 17 comments

Comments

@tooxo
Copy link

tooxo commented Apr 28, 2020

Overview

To simplify developing and linting with PyAV, stub files should be included. explanation of stubs

Desired Behavior

Every object / method should have defined stubs, which are detectable by IDEs such as PyCharm aswell as typical linters like MyPy.

Example API

Not relevant for this problem (?)

Additional context

For futher explanation see: https://www.python.org/dev/peps/pep-0484/

A possible solution could be using a stub generator like stubgen.

@tooxo tooxo changed the title Add mypy stub files to all PyAV Add mypy stub files to PyAV Apr 28, 2020
@jlaine
Copy link
Member

jlaine commented Apr 28, 2020

I'd very much enjoy having such stubs! However, having already written stubs for a fairly large project (cryptography) I know this is going to take more time than I currently have. Would you consider submitting a PR?

@tooxo
Copy link
Author

tooxo commented Apr 28, 2020

Yes sure, if I get to it I will submit a PR.

@mikeboers
Copy link
Member

Is there any way to auto-generate this from the Cython source? I'm not looking forward to maintaining the stub files in the future, and if it can be a near-automatic process it is more likely to be maintained by me.

@tooxo
Copy link
Author

tooxo commented May 1, 2020

Yes and no, there is a mypy stub generator for cpython files, but that requires pyx files with every type indicated. If it's not the case the stubgen will create stub files with any type indicated as any, which doesn't really help.

For example this will be the result on the descriptor.pyx file:

from typing import Any

class Descriptor:
    name: Any = ...
    options: Any = ...
    @classmethod
    def __init__(self, *args, **kwargs) -> None: ...
    def __reduce__(self) -> Any: ...
    def __setstate__(self, state) -> Any: ...

So the (only) possible solution would be to refactor the whole library to use these type hints.

@tooxo
Copy link
Author

tooxo commented May 1, 2020

Another (smaller) step would be to follow up on the promise here https://github.com/PyAV-Org/PyAV/blob/develop/av/__init__.py#L14 and import anything into the __init__.py, and not just the things that are human constructable, so that IDEs like PyCharm can at least auto-complete the class names.

@moi90
Copy link
Contributor

moi90 commented Sep 26, 2021

So the (only) possible solution would be to refactor the whole library to use these type hints.

Couldn't we just start slowly? PyAV supports Python from 3.5 on, so adding type annotations shouldn't break anything.

@synodriver
Copy link

synodriver commented Mar 14, 2022

Hi.
Add stub files would not be so hard(I think). After install pyav and mypy, just stubgen -p av and that would generate pyi files. Althogh some of their types are Any.

@jlaine
Copy link
Member

jlaine commented Mar 14, 2022

Hi. Add stub files would not be so hard(I think). After install pyav and mypy, just stubgen -p av and that would generate pyi files. Althogh some of their types are Any.

Great, I look forward to your pull request!

@synodriver
Copy link

synodriver commented Mar 14, 2022

Hi. Add stub files would not be so hard(I think). After install pyav and mypy, just stubgen -p av and that would generate pyi files. Althogh some of their types are Any.

Great, I look forward to your pull request!

But when function signatures changed or new functions been add, the stubgen command should be run again to generate new pyi files against new modules. Should we do that in CI?

@moi90
Copy link
Contributor

moi90 commented Mar 14, 2022

stubgen is surely a good approach. But why not include type annotations directly in the code?

@jlaine
Copy link
Member

jlaine commented Mar 14, 2022

@moi90 I'm unfamiliar with how to include type annotations for Cython code, but on principle yes I would prefer them to be straight in the code if that is possible.

@moi90
Copy link
Contributor

moi90 commented Mar 15, 2022

@jlaine Ah, yes. Compiled modules will require pyi files. However, stubgen support for Cython seems to be currently limited, see python/mypy#7542.

Anyways, the annotations produced by stubgen can serve as a starting point and can then be manually improved. (This seems to be the preferred way in typeshed as well.)

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Jul 14, 2022
@moi90
Copy link
Contributor

moi90 commented Jul 14, 2022

This shouldn't be closed, I think this could be very valuable for users of the library.

@github-actions github-actions bot removed the stale label Jul 15, 2022
@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@yozachar
Copy link

image

I get missing stub files.

Related: #1065

Would adding a py.typed, to this repository resolve this?

@moi90
Copy link
Contributor

moi90 commented Sep 3, 2023

Would adding a py.typed, to this repository resolve this?

No, the problem is, that no type annotations are available at all. It would be tedious to add them manually and keep them up to date.

And, so far, is not easy to extract typing information from Cython code automatically.

Apart from stubgen (which is only able to inspect the compiled result, not the Cython code itself) https://github.com/Vizonex/CyStub could be another solution, but it does not work with Cython 3.

@WyattBlue WyattBlue removed the stale label Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants