question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consider type hints for 3.x.y release?

See original GitHub issue

Situation

Python 3.5 introduced the typing module. As soon as we are sunsetting the 2.9.x release of semver, it raises the question if we should use type hints for our 3.x.y release of semver.

Currently, we don’t use it for our functions and methods.

To avoid any misinterpretations, the typing module contains this note:

The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc.

To start discussion and to get a better picture where we stand on this topic, I’ve opened this issue. 😉

Example

This is a short example of a type hint for the parse function:

def parse(version: str) -> dict:
    """
    Parse version to major, minor, patch, pre-release, build parts.

    :param version: version string
    :return: dictionary with the keys 'build', 'major', 'minor', 'patch',
     and 'prerelease'. The prerelease or build keys can be None
     if not provided

Bernát Gábor recommends that “type hints should be used whenever unit tests are worth writing.”

Pros and Cons

I’ve collected some pros and cons, probably there are more that I haven’t listed here. This list is by no means exhaustive. Feel free to amend it.

Pros

  • Helps to catch type errors when testing with mypy.
  • Could support IDEs to provide a better popup help.
  • Improves documentation when using the sphinx-autodoc-typehints plugin.
  • Help you as a developer to write better code.

Cons

  • A slight impact of performance.
  • Needs another dependency when checking for type hints (mypy).
  • Needs a bit more time to add it.
  • Works best with modern Python (version >= 3.5).

Scope

If we want this, it will only be implemented in the 3.x.y release of semver. Older releases (2.9.x) are not covered by this issue and won’t be modified.

Furthermore, we need to create a semver.pyi file with the command stubgen (integrated in mypy).

See also

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (13 by maintainers)

github_iconTop GitHub Comments

3reactions
ppktcommented, Dec 10, 2019

I’m a huge fan of typing in Python. I wasn’t aware about impact on performance but I think every library should use them. As far as I know, Python 3.4 is no longer maintained so we can drop support for it. I don’t use mypy - I only rely on support in IDE but results are really great, especially in bigger projects.

I think it’s a must-have feature for semver 3.0.0 😃

1reaction
tomschrcommented, Oct 26, 2020

As Thomas Laferriere already added type hints into #290, I think we can consider this as done now. Thanks Thomas for all your work! Much appreciated! 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

typing — Support for type hints — Python 3.11.1 documentation
The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers,...
Read more >
python - How to specify multiple return types using type-hints
seems like a XY problem. I'd ask why a function would return a list or a bool in the first place.
Read more >
Type Annotations in Python 3.8 - Medium
The typing module adds support for type hints. It contains some of the types you will use most often: List, Dict, and Tuple....
Read more >
A deep dive on Python type hints | Vicki Boykis
Since the release of Python's type hints in 2014, ... 20-30% of Python 3 codebases are using hints (also sometimes called annotations).
Read more >
Type hints cheat sheet - mypy 0.991 documentation
Type hints cheat sheet#. This document is a quick cheat sheet showing how to use type annotations for various common types in Python....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found