Pytorch-lightning: Add typing to the codebase

Created on 18 Feb 2020  路  11Comments  路  Source: PyTorchLightning/pytorch-lightning

Python typing is a good way to rapidly check for coding issue while developing and it also allows to generate documentation with types automatically.

It's a good first issue for someone who wants to contribute to PL.

documentation enhancement good first issue let's do it!

All 11 comments

I like this and I'm happy to help out here. My suggestion is to do it gradually and split into multiple PRs, e.g., starting with Trainer and LightningModule, then Loggers and so on.

@awaelchli thx and happy to see more your PRs soon :]

awesome! let's keep this going as we keep making fixes

It is not done yet lol

@hadim I am looking at adding more typing to the rest of the code base and I am facing this issue again with failing imports (e.g. in tests). I think it is related to forward referencing / cyclic references.
For example, in the callbacks I need to import the Trainer and Lightning classes to type the callback signatures, which somehow fails at runtime.
Have you faced such issues before?

No, I haven't but an easy fix is to use types as string. Instead of:

class Node:
    def next() -> Node:
        pass

Try:

class Node:
    def next() -> 'Node':
        pass

This StackOverflow response adds more context to @hadim's response:

In short, that's an upcoming feature of Python 4.0. The most backward compatible workaround is the solution suggested by @hadim above.

Thanks for your suggestions. With just the string method there are no errors, but my IDE (pycharm) is complaining and suggesting that I still need to import Trainer (when I declare trainer: 'Trainer'). However, when I add this import and run tests, it fails to import.
The stackoverflow post doesn't say if the import is needed or not. Probably it does, otherwise how would the type system know which class it is?

Wow, is there road map for Python 4.0? What will be the last 3.x series? @luiscape

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I guess we can close it as we are continuously adding with every new PR...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mmsamiei picture mmsamiei  路  3Comments

iakremnev picture iakremnev  路  3Comments

edenlightning picture edenlightning  路  3Comments

baeseongsu picture baeseongsu  路  3Comments

versatran01 picture versatran01  路  3Comments