pyro-ppl / pyro

Deep universal probabilistic programming with Python and PyTorch
http://pyro.ai
Apache License 2.0
8.57k stars 986 forks source link

Using Python type hints #2550

Open jpchen opened 4 years ago

jpchen commented 4 years ago

Since Pyro supports exclusively Python 3, we should look into adding type hints. This has a few advantages:

e.g.

from typing import List, Optional

def foo(bar: List, baz: Optional[List] = None) -> List:
    return bar

Add type hints to modules

Additional functionality for type hints

fritzo commented 4 years ago

I guess we should add a mypy test stage

fritzo commented 3 years ago

This would also allow us to remove sphinx type annotations, if we were to use https://pypi.org/project/sphinx-autodoc-typehints/

kamathhrishi commented 3 years ago

Can I work on this issue? Adding type hints to the existing codebase? Maybe not the entire codebase in a PR, but at least some sections of it. Then subsequent PR's could cover the codebase.

fritzo commented 3 years ago

Hi @kamathhrishi, sure, we'd appreciate any help! I especially like the idea of a bunch of small easy-to-review PRs that add type hints to one module at a time.

kamathhrishi commented 3 years ago

Thank you @fritzo. I have made a PR for optim module. Review it and let me know. I am new to the codebase and would like to take this as a starting step.

fritzo commented 2 years ago

It looks like we should also add py.types stub files to various directories to avoid errors like

infer.py:4: error: Skipping analyzing "pyro": module is installed, but missing library stubs or py.typed marker
infer.py:4: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
infer.py:5: error: Skipping analyzing "pyro.distributions": module is installed, but missing library stubs or py.typed marker
infer.py:8: error: Skipping analyzing "pyro.infer": module is installed, but missing library stubs or py.typed marker
infer.py:9: error: Skipping analyzing "pyro.infer.autoguide": module is installed, but missing library stubs or py.typed marker
infer.py:10: error: Skipping analyzing "pyro.infer.reparam": module is installed, but missing library stubs or py.typed marker
infer.py:11: error: Skipping analyzing "pyro.optim": module is installed, but missing library stubs or py.typed marker
willtai commented 2 years ago

Can I take a look at parts of this? I can open a PR for params