Skip to content
Snippets Groups Projects

Type annotations in metadata mappings

Closed Lucifer Morningstar requested to merge generated-differential-D7342-source into master
1 unresolved thread

Add type annotations to metadata mappings (#2259)

This patch adds type annotations to metadata mappings so that the mypy type checker can detect bugs in code. While working on the path, I also encountered these issues:

  1. mypy raises error on detecting for implicit None returns, so I had to add return None at many places.

  2. At a few places, @property s' are accessed using cls. mypy rightfully raises an error, so I had to add ignore to silence it for now. It appears Python 3.9+ supports class level properties so the ignores could be replaced by the proper handling when the minimum python version is raised.

Closes #2259.


Migrated from D7342 (view on Phabricator)

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Build has FAILED

    Patch application report for D7342 (id=26539)

    Rebasing onto 9e89b83b...

    Current branch diff-target is up to date.
    Changes applied before test
    commit f60c67aa77c039db8f5399c7a79577b1dd658809
    Author: Kartik Ohri <kartikohri13@gmail.com>
    Date:   Sun Mar 13 14:09:23 2022 +0530
    
        Type annotations in metadata mappings
        
        Summary: Add type annotations to files in swh/indexer/metadata_dictionary/ (#2259)
        
        This patch adds type annotations to metadata mappings so that the mypy
        type checker can detect bugs in code. While working on the path, I also
        encountered these issues:
        
        1) TypedDict does not support using variables in defining or assigning
        keys. At a lot of places, the code uses SCHEMA_URI to construct the
        keys. mypy raises errors at these places. There is a tradeoff between
        replacing SCHEMA_URI with its literal value everywhere and using
        type: ignore hints.
        
        2) mypy raises error on detecting for implicit None returns, so I had
        to add return None at many places.
        
        3) At a few places, @property s' are accessed using `cls`. mypy
        rightfully raises an error, so I had to add ignore to silence it for
        now. It appears Python 3.9+ supports class level properties so the
        ignores could be replaced by the proper handling when the minimum
        python version is raised.
        
        Closes #2259.
        
        Reviewers: vlorentz

    Link to build: https://jenkins.softwareheritage.org/job/DCIDX/job/tests-on-diff/197/ See console output for more information: https://jenkins.softwareheritage.org/job/DCIDX/job/tests-on-diff/197/console

    • Fix mypy issues
  • Build has FAILED

    Patch application report for D7342 (id=26540)

    Rebasing onto 9e89b83b...

    Current branch diff-target is up to date.
    Changes applied before test
    commit 518ac10085e4109572a9108726286c6a18441b69
    Author: Kartik Ohri <kartikohri13@gmail.com>
    Date:   Sun Mar 13 15:51:53 2022 +0530
    
        Fix mypy issues
    
    commit f60c67aa77c039db8f5399c7a79577b1dd658809
    Author: Kartik Ohri <kartikohri13@gmail.com>
    Date:   Sun Mar 13 14:09:23 2022 +0530
    
        Type annotations in metadata mappings
        
        Summary: Add type annotations to files in swh/indexer/metadata_dictionary/ (#2259)
        
        This patch adds type annotations to metadata mappings so that the mypy
        type checker can detect bugs in code. While working on the path, I also
        encountered these issues:
        
        1) TypedDict does not support using variables in defining or assigning
        keys. At a lot of places, the code uses SCHEMA_URI to construct the
        keys. mypy raises errors at these places. There is a tradeoff between
        replacing SCHEMA_URI with its literal value everywhere and using
        type: ignore hints.
        
        2) mypy raises error on detecting for implicit None returns, so I had
        to add return None at many places.
        
        3) At a few places, @property s' are accessed using `cls`. mypy
        rightfully raises an error, so I had to add ignore to silence it for
        now. It appears Python 3.9+ supports class level properties so the
        ignores could be replaced by the proper handling when the minimum
        python version is raised.
        
        Closes #2259.
        
        Reviewers: vlorentz

    Link to build: https://jenkins.softwareheritage.org/job/DCIDX/job/tests-on-diff/198/ See console output for more information: https://jenkins.softwareheritage.org/job/DCIDX/job/tests-on-diff/198/console

    • Remove TypedDict's as per chat
  • Build is green

    Patch application report for D7342 (id=26543)

    Rebasing onto 9e89b83b...

    Current branch diff-target is up to date.
    Changes applied before test
    commit 601b6a0def92f7324cdb2ca12a20ba4f1504b62f
    Author: Kartik Ohri <kartikohri13@gmail.com>
    Date:   Mon Mar 14 15:29:48 2022 +0530
    
        Remove TypedDict's as per chat
    
    commit 518ac10085e4109572a9108726286c6a18441b69
    Author: Kartik Ohri <kartikohri13@gmail.com>
    Date:   Sun Mar 13 15:51:53 2022 +0530
    
        Fix mypy issues
    
    commit f60c67aa77c039db8f5399c7a79577b1dd658809
    Author: Kartik Ohri <kartikohri13@gmail.com>
    Date:   Sun Mar 13 14:09:23 2022 +0530
    
        Type annotations in metadata mappings
        
        Summary: Add type annotations to files in swh/indexer/metadata_dictionary/ (#2259)
        
        This patch adds type annotations to metadata mappings so that the mypy
        type checker can detect bugs in code. While working on the path, I also
        encountered these issues:
        
        1) TypedDict does not support using variables in defining or assigning
        keys. At a lot of places, the code uses SCHEMA_URI to construct the
        keys. mypy raises errors at these places. There is a tradeoff between
        replacing SCHEMA_URI with its literal value everywhere and using
        type: ignore hints.
        
        2) mypy raises error on detecting for implicit None returns, so I had
        to add return None at many places.
        
        3) At a few places, @property s' are accessed using `cls`. mypy
        rightfully raises an error, so I had to add ignore to silence it for
        now. It appears Python 3.9+ supports class level properties so the
        ignores could be replaced by the proper handling when the minimum
        python version is raised.
        
        Closes #2259.
        
        Reviewers: vlorentz

    See https://jenkins.softwareheritage.org/job/DCIDX/job/tests-on-diff/199/ for more details.

98 def mapping(self) -> Dict[str, str]:
80 99 """A translation dict to map dict keys into a canonical name."""
81 100 raise NotImplementedError(f"{self.__class__.__name__}.mapping")
82 101
83 102 @staticmethod
84 def _normalize_method_name(name):
103 def _normalize_method_name(name: str) -> str:
85 104 return name.replace("-", "_")
86 105
87 106 @classmethod
88 def supported_terms(cls):
107 def supported_terms(cls) -> Iterable[str]:
89 108 return {
90 109 term
91 for (key, term) in cls.mapping.items()
110 for (key, term) in cls.mapping.items() # type: ignore
  • Hi,

    Thanks for this, it looks good.

    One small change, though: instead of repeating Dict[str, Any] (and Dict[str, str] and others) everywhere, you could create a specific type alias for dict that represent JSON-LD documents, eg. JsonldDict = Dict[str, Any], and use it in signatures

  • Merge request was returned for changes

  • Phabricator Migration user restored source branch generated-differential-D7342-source

    restored source branch generated-differential-D7342-source

  • Closing this MR, as it was inactive for a while and accumulated conflicts

  • closed

  • Please register or sign in to reply
    Loading