Skip to content

Type hints in mmif.Mmif #351

@marcverhagen

Description

@marcverhagen

In mmif.Mmif there is a method with the following signature

def get_documents_by_type(self, doc_type: Union[str, DocType]) -> List[Document]:

This always passed pytype tests fine until I used the above method in the summarizer, at which time I got the following error:

[13:49:55] (346-summarizer)> pytype mmif/utils/summarizer/
Computing dependencies
Analyzing 6 sources with 16 local dependencies
ninja: Entering directory `.pytype'
[5/6] check mmif.utils.summarizer.summary
FAILED: [code=1] /Users/marc/Desktop/projects/clams/code/clamsproject/mmif-python/.pytype/pyi/mmif/utils/summarizer/summary.pyi
/usr/local/opt/python@3.11/bin/python3.11 -m pytype.main --imports_info /Users/marc/Desktop/projects/clams/code/clamsproject/mmif-python/.pytype/imports/mmif.utils.summarizer.summary.imports --module-name mmif.utils.summarizer.summary --platform darwin -V 3.11 -o /Users/marc/Desktop/projects/clams/code/clamsproject/mmif-python/.pytype/pyi/mmif/utils/summarizer/summary.pyi --analyze-annotated --nofail --quick /Users/marc/Desktop/projects/clams/code/clamsproject/mmif-python/mmif/utils/summarizer/summary.py
/Users/marc/Desktop/projects/clams/code/clamsproject/mmif-python/mmif/utils/summarizer/summary.py:151:16: error: in video_documents: Function Mmif.get_documents_by_type was called with the wrong arguments [wrong-arg-types]
         Expected: (self, doc_type: Union[mmif.vocabulary.document_types.DocumentTypes, str])
  Actually passed: (self, doc_type: mmif.vocabulary.base_types.DocumentTypesBase)

        return self.mmif.get_documents_by_type(DocumentTypes.VideoDocument)
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It makes sense to me that using DocumentTypes is frowned upon by pytype and replacing the signature with the following got rid of the error:

def get_documents_by_type(self, doc_type: Union[str, DocumentTypes.Document]) -> List[Document]:

But then the CLI script crashes on some typing issue:

12:06:59] (346-summarizer)> mmif -h
Traceback (most recent call last):
  File "/usr/local/bin/mmif", line 5, in <module>
    from mmif.__init__ import cli
  File "/Users/marc/Desktop/projects/clams/code/clamsproject/mmif-python/mmif/__init__.py", line 12, in <module>
    from mmif.serialize import *
  File "/Users/marc/Desktop/projects/clams/code/clamsproject/mmif-python/mmif/serialize/__init__.py", line 3, in <module>
    from .mmif import *
  File "/Users/marc/Desktop/projects/clams/code/clamsproject/mmif-python/mmif/serialize/mmif.py", line 146, in <module>
    class Mmif(MmifObject):
  File "/Users/marc/Desktop/projects/clams/code/clamsproject/mmif-python/mmif/serialize/mmif.py", line 494, in Mmif
    def get_documents_by_type(self, doc_type: Union[str, DocumentTypes.Document]) -> List[Document]:
                                              ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 355, in inner
    return cached(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 481, in __getitem__
    return self._getitem(self, parameters)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 694, in Union
    parameters = tuple(_type_check(p, msg) for p in parameters)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 694, in <genexpr>
    parameters = tuple(_type_check(p, msg) for p in parameters)
                       ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 190, in _type_check
    if arg in (Any, LiteralString, NoReturn, Never, Self, TypeAlias):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/marc/Desktop/projects/clams/code/clamsproject/mmif-python/mmif/vocabulary/base_types.py", line 193, in __eq__
    return self._eq_internal(other, self.fuzzy_eq and other.fuzzy_eq)
                                                      ^^^^^^^^^^^^^^
AttributeError: type object 'Any' has no attribute 'fuzzy_eq'

The only thing I could come up with to make all this go away is to make the type hint useless.

def get_documents_by_type(self, doc_type: Any) -> List[Document]:

For now that is what I am doing in the summarizer branch, but it does not feel right. Note that this may also become an issue with the get_documents_locations and get_document_locations methods.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions