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 Python type hints #359

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

add Python type hints #359

wants to merge 1 commit into from

Conversation

egpbos
Copy link
Member

@egpbos egpbos commented Oct 21, 2024

Added a subsection on type hints, following the discussion in #310. Also added a few cool libraries, e.g. FastAPI that was mentioned by Bouwe in #54 and Typer from the same author.

Fixes #310.

Checklist

SIGNIFICANT changes / additions, e.g. new chapters

  • I checked whether the contribution fits in The Turing Way before considering contributing to this Guide.
  • I discussed my contribution in an issue and took into account feedback.

ALL contributions

  • I previewed my changes locally using e.g. python3 -m http.server 4000 and confirmed they work correctly.
  • I checked for broken links, e.g. using the link checker GitHub Action workflow, or locally by using docker run --init -it -v `pwd`:/docs lycheeverse/lychee /docs --config=docs/lychee.toml, at least for the files I changed.
  • My name was added to the CITATION.cff file.

Added a subsection on type hints, following the discussion in #310. Also added a few cool libraries, e.g. FastAPI that was mentioned by Bouwe in #54 and Typer from the same author.

Fixes #310.
Copy link
Member

@bouweandela bouweandela left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @egpbos, very nice addition!

@@ -33,6 +33,29 @@ Building and/or using Python 2 is probably discouraged even more than, say, usin
* A popular way to learn Python is by doing it the hard way at http://learnpythonthehardway.org/
* Using [`pylint`](https://www.pylint.org) and [`yapf`](https://github.com/google/yapf) while learning Python is an easy way to get familiar with best practices and commonly used coding styles

### Type hints
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section would fit better between "code style" and "testing". In the code style chapter various tools for static code analysis are presented, and type hints are typically also used for static code analysis.

Comment on lines +55 to +56
Other important libraries like Matplotlib have very complex functions that take in many possible types of arguments, leading to overly complex variable types.
Such huge types clutter your code tremendously, so they are not typically encouraged.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Other important libraries like Matplotlib have very complex functions that take in many possible types of arguments, leading to overly complex variable types.
Such huge types clutter your code tremendously, so they are not typically encouraged.

I would leave this out. There is some discussion possible about the design choices of matplotlib and that is not the aim of our guide. How about saying that type hints are not practical if e.g. the researchers you are developing the code with are not comfortable with using them because they are too technical?

@@ -338,3 +362,4 @@ We recommend `flask`.
* For run-time configuration via command-line options, the built-in [`argparse`](https://docs.python.org/library/argparse.html) module usually suffices.
* A more complete solution is [`ConfigArgParse`](https://github.com/bw2/ConfigArgParse). This (almost) drop-in replacement for `argparse` allows you to not only specify configuration options via command-line options, but also via (ini or yaml) configuration files and via environment variables.
* Other popular libraries are [`click`](https://click.palletsprojects.com) and [`fire`](https://google.github.io/python-fire/).
* [Typer](https://typer.tiangolo.com): make a CLI app by using type hints with runtime effects. Very low on boilerplate for simple cases, but also allows for more complex cases. Uses `click` internally.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* [Typer](https://typer.tiangolo.com): make a CLI app by using type hints with runtime effects. Very low on boilerplate for simple cases, but also allows for more complex cases. Uses `click` internally.
* [Typer](https://typer.tiangolo.com): make a command-line application by using type hints with runtime effects. Very low on boilerplate for simple cases, but also allows for more complex cases. Uses `click` internally.

To keep the text easy to understand for less technical people


We recommend using type hints, where possible and _practical_.
Type hints are still being actively developed; not everything one would like to be able to express in a compact way can yet be achieved.
This is why, for instance, NumPy arrays and ML library "tensor" types still (in 2024) have awkward type hinting.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you be more specific about what "ML library" you mean? And also write out the abbreviation, please

See [this guide](https://realpython.com/python-type-checking/) to learn more about type hints.

Some tools to know about that make use of type hints:
- [Mypy](https://mypy.readthedocs.io/en/stable/index.html): tool that checks whether your code uses types correctly.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a bit broader by mentioning (or linking to a page with) alternatives? e.g. https://www.infoworld.com/article/2260170/4-python-type-checkers-to-keep-your-code-clean.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add information on type hints to Python chapter
2 participants