openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

Create more Python type hints from type comments

Open cclauss opened this issue 2 years ago • 1 comments

I am always frustrated when I see Python function comments that contain type information that cannot be used by mypy.

Describe the problem that you'd like solved

Use the information in the type comments to create Python type hints that mypy can use to find errors in our code. https://github.com/internetarchive/openlibrary/blob/39962157c12465d050e43458444aba9a59661c8d/openlibrary/catalog/merge/merge_marc.py#L17-L24 could be converted to

def build_titles(title: str) -> dict:
    """
    Uses a full title to create normalized and short title versions.
    :param str title: Full title of an edition
    :return: An expanded dict of title variations
    """

Instances of these PyCharm-style comments can be found in 56 files in our codebase by running: git grep -E ':param|:rtype' Does PyCharm have any builtin capabilities to do these conversions? *https://www.jetbrains.com/help/pycharm/using-docstrings-to-specify-types.html

Please do not modify all 56 files in a single pull request.

Pull requests that modify 5 to 10 files are much easier for project maintainers to review. Lessons learned in early pull requests can improve the conversion process and accelerate subsequent conversions and reviews.

We have already used @ilevkivskyi's wonderful com2ann tool to convert some type comments into Python type hints but that tool does not currently convert :param type name: and :rtype: type comments.

Proposal & Constraints

Either manually or with some code, extract the type information from these function comments to create Python type hints. While doing so and in code review, read the function body to ensure that the data types match the comments.

Additional context

Stakeholders

cclauss avatar Jul 17 '22 07:07 cclauss

I'd love to help with this. I will start from the bottom of the list created by git grep -E ':param|:rtype' and work up, creating PRs for each batch of five files.

scottbarnes avatar Jul 17 '22 16:07 scottbarnes