8000 PEP 484 Type Annotations (feature request) · Issue #12345 · tensorflow/tensorflow · GitHub
[go: up one dir, main page]

Skip to content
PEP 484 Type Annotations (feature request) #12345
@ed-alertedh

Description

@ed-alertedh

System information

N/A

Describe the problem

Background

PEP 484 [1] added support for type hints in Python. These are purely annotations and are not enforced by the interpreter, however there are tools such as mypy [2] which can be run to check for consistency in the annotations. The typeshed initiative [3] has started to build external collections of type annotations for commonly used libraries.

When adding type annotations to a codebase, it is best if you can achieve near 100% coverage, otherwise uncertainty propagates out from everywhere the "untyped" code is called. A codebase using TF would likely struggle to gain much benefit from type-checking in any of the core code built on top of TF.

Benefits of Adding Type Annotations

  • The expected inputs and outputs of functions become much clearer
  • Code completion is able to provide more useful suggestions, boosting productivity by reducing amount of time spent referring to docs
  • Static analysis can uncover latent bugs (case study here[5])

Difficulties/Drawbacks

  • People may be encouraged to overly constrain types, removing some of the flexibility of a dynamic language. But given that Google's Python style-guide discourages "Power Features" [4] I would argue that striving towards code that is explicit is a similar philosophy
  • The protobuf compiler would need to be augmented to generate type annotations.
  • The Tensorflow Python codebase is huge, so at this point adding the annotations would be a huge undertaking.
  • Tensorflow still supports python 2.7, 3.3 and 3.4 which do not have the type annotation syntax. So if this were implemented it would probably have to be in external *.pyi files, which is harder to maintain compared to inline type annotations in the source code.

Final thoughts

I realise that this would be a major undertaking and wouldn't be likely to ship any time soon, but I'm curious to gauge Google's thoughts on this new feature in Python. I'm about to start building a new codebase from scratch and was keen to use it as a chance to try out type annotations. I probably still will give it a shot, but I suspect that unless most of the common data science libs out there adopt this standard then its usefulness will be quite limited.

[1] https://www.python.org/dev/peps/pep-0484/
[2] http://mypy-lang.org/
[3] https://github.com/python/typeshed
[4] https://google.github.io/styleguide/pyguide.html#Power_Features
[5] http://blog.zulip.org/2016/10/13/static-types-in-python-oh-mypy/

Metadata

Metadata

Assignees

Labels

staleThis label marks the issue/pr stale - to be closed automatically if no activitystat:awaiting responseStatus - Awaiting response from authortype:featureFeature requests

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0