Skip to content

Method .get_fields_types() of annotated model returns AttributeError on Python 3.9 #43

Description

@agjharms

The following code block from ./docs/Usage_example_HealthRI_core.ipynb returns an AttributeError when run on Python 3.9:

from sempyro.hri_dcat.hri_dataset import HRIDataset
from sempyro.foaf.agent import Agent
from pprint import pprint

core_fields = HRIDataset.annotate_model()
types = core_fields.get_fields_types()

This is due to the fact that this method iterates over the properties in the model and uses the __name__attribute from the type hinting objects to determine their types. Based on this issue, python/cpython#88690, this attribute was not present for all types, therefore resulting in an AttributeError. On Python 3.10 the .get_fields_types() method does work.

The aforementioned AttributeError:

AttributeError                            Traceback (most recent call last)
Cell In[1], line 6
      3 from pprint import pprint
      5 core_fields = HRIDataset.annotate_model()
----> 6 types = core_fields.get_fields_types()
      7 mandatory_types = {k: types[k] for k in core_fields.mandatory_fields()}
      8 pprint(mandatory_types)

File ~/Documents/Projects/SeMPyRO/venv/lib/python3.9/site-packages/sempyro/rdf_model.py:67, in ModelAnnotationUtil.get_fields_types(self)
     66 def get_fields_types(self):
---> 67     return {key: {"datatype": self._get_list_types(datatype=value.annotation),
     68                   "RDF type": value.json_schema_extra.get("rdf_type", "No RDF type specified for the field")} for
     69             key, value in self.fields.items()}

File ~/Documents/Projects/SeMPyRO/venv/lib/python3.9/site-packages/sempyro/rdf_model.py:67, in <dictcomp>(.0)
     66 def get_fields_types(self):
---> 67     return {key: {"datatype": self._get_list_types(datatype=value.annotation),
     68                   "RDF type": value.json_schema_extra.get("rdf_type", "No RDF type specified for the field")} for
     69             key, value in self.fields.items()}

File ~/Documents/Projects/SeMPyRO/venv/lib/python3.9/site-packages/sempyro/rdf_model.py:77, in ModelAnnotationUtil._get_list_types(self, datatype)
     76 def _get_list_types(self, datatype):
---> 77     dt_name = datatype.__name__
     78     if dt_name in ["List", "Union"]:
     79         types = []

File /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/typing.py:701, in _BaseGenericAlias.__getattr__(self, attr)
    699 if '__origin__' in self.__dict__ and not _is_dunder(attr):
    700     return getattr(self.__origin__, attr)
--> 701 raise AttributeError(attr)

AttributeError: __name__

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions