Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add PEP 484 -- Type Hints #368

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

Open
pmhahn opened this issue Jul 1, 2020 · 3 comments
Open

Add PEP 484 -- Type Hints #368

pmhahn opened this issue Jul 1, 2020 · 3 comments

Comments

@pmhahn
Copy link
Contributor

pmhahn commented Jul 1, 2020

Python supports type hints to improve static analysis:
https://www.python.org/dev/peps/pep-0484/
This helps to identify cases, were wrong types are passed into functions et al by using type checkers likehttp://mypy-lang.org/ or https://github.com/google/pytype/.

python-ldap does not yet seem to contain those annotations nor am I aware of any project doing that.
Those annotation can either we added directly in the library like

def func(arg: str, val: int = 0) -> Tuple[int, str]

since Python 3.5 or in a Python-2 backward compatible way as

def func(arg, val=0): # type: (str, int) -> Tuple[int, str]

As an alternative the annotations can be put into so-called "stub files" names "*.pyi", which are collected by https://github.com/python/typeshed, but inline annotations are preferred as they are easier to keep in sync.

I started working on that, but other projects have preempted that
effort. My current work is at
univention/typeshed@8fc5f79,
but it not much more then generating the stubs using stubgen.
If someone wants to join that effort that is much appreciated.

This was proposed to the Mailing List.
@tiran proposed to start the work only after the Python 2 legacy code is removed:

we are planning to drop support for Python 3.5 and earlier in the next
version of python-ldap. This would make it simpler to add signatures for
type hints directly to the code base. Patches are welcome, but please
wait until we have finalized Python 2 cleanup in master.

@encukou
Copy link
Member

encukou commented Jul 1, 2020

I would prefer not using .pyi. Inline type hints are much more useful, especially for documentation.
Unfortunately, stubgen doesn't seem too successful: neither annotating things with Any, not specifying None return values, nor determinig that constants are str or int, is very useful.

IMO, type hints are useful for documentation purposes.
I would love to use Sphinx's autofunction to generate function signatures in the reference docs; maybe that can be done along with the type annotations?

@pmhahn
Copy link
Contributor Author

pmhahn commented Jul 2, 2020

I would prefer not using .pyi. Inline type hints are much more useful, especially for documentation.

See my quote from @tiran - type hints will be added directly into the Python code .py files after the old Python 2 code has been purged.
For now I'm using the .pyi files as my project is still Python 2.

Unfortunately, stubgen doesn't seem too successful: neither annotating things with Any, not specifying None return values, nor determinig that constants are str or int, is very useful.

Yes, it's a lot of work as stubgen only provides a very basic template. There are other tools which can generate better stubs by using runtime information - it's on my todo list to try them out.

IMO, type hints are useful for documentation purposes.
I would love to use Sphinx's autofunction to generate function signatures in the reference docs; maybe that can be done along with the type annotations?

Yes, I'm already using sphinx-autodoc-typehints, which re-uses the Python type annotation for Sphinx documentation, too.

@Alphix
Copy link
Contributor

Alphix commented May 16, 2023

@pmhahn see PR #522 for a first attempt at adding type hints

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

3 participants