Deltares / GEOLib-Plus

GEOLib+ is built on top of GEOLib and provides CPT (both GEF and XML) intepretation, among other useful tooling.
https://deltares.github.io/GEOLib-Plus
8 stars 10 forks source link

AttributeError: __args__ in the "typing" module when using Python >= 3.9 due to type hinting #3

Closed martijnkriebel closed 2 years ago

martijnkriebel commented 2 years ago

Importing geolib-plus with Python version >=3.9 causes an AttributeError in the "typing" module. My guess is that this is because of the changes in type hinting that were introduced in version 3.9 by PEP 585.

So, the most up to date version of Python that can be used with geolib-plus is 3.8.13. I think it would be good to at least mention this in the README and/or enforce this Python version in requirements.txt and pyproject.toml. An update of geolib-plus with support for Python >= 3.9 would of course be the best solution.

Log:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [2], in <cell line: 1>()
----> 1 import geolib_plus

File c:\ProgramData\Anaconda3\envs\test6\lib\site-packages\geolib_plus\__init__.py:1, in <module>
----> 1 from .cpt_base_model import *
      3 __version__ = "0.1.1"

File c:\ProgramData\Anaconda3\envs\test6\lib\site-packages\geolib_plus\cpt_base_model.py:26, in <module>
     19     @abstractmethod
     20     def read_file(self, filepath: Path) -> dict:
     21         raise NotImplementedError(
     22             "The method should be implemented in concrete classes."
     23         )
---> 26 class AbstractCPT(BaseModel):
     27     """Base CPT class, should define abstract."""
     29     #  variables

File c:\ProgramData\Anaconda3\envs\test6\lib\site-packages\pydantic\main.py:252, in ModelMetaclass.__new__(mcs, name, bases, namespace, **kwargs)
    246 if (
    247     isinstance(value, untouched_types)
    248     and ann_type != PyObject
    249     and not lenient_issubclass(getattr(ann_type, '__origin__', None), Type)
    250 ):
    251     continue
--> 252 fields[ann_name] = inferred = ModelField.infer(
    253     name=ann_name,
    254     value=value,
    255     annotation=ann_type,
    256     class_validators=vg.get_validators(ann_name),
    257     config=config,
    258 )
    259 if not inferred.required:
    260     fields_defaults[ann_name] = inferred.default

File c:\ProgramData\Anaconda3\envs\test6\lib\site-packages\pydantic\fields.py:309, in ModelField.infer(cls, name, value, annotation, class_validators, config)
    307 field_info.alias = field_info.alias or field_info_from_config.get('alias')
    308 annotation = get_annotation_from_field_info(annotation, field_info, name)
--> 309 return cls(
    310     name=name,
    311     type_=annotation,
    312     alias=field_info.alias,
    313     class_validators=class_validators,
    314     default=value,
    315     default_factory=field_info.default_factory,
    316     required=required,
    317     model_config=config,
    318     field_info=field_info,
    319 )

File c:\ProgramData\Anaconda3\envs\test6\lib\site-packages\pydantic\fields.py:271, in ModelField.__init__(self, name, type_, class_validators, model_config, default, default_factory, required, alias, field_info)
    269 self.shape: int = SHAPE_SINGLETON
    270 self.model_config.prepare_field(self)
--> 271 self.prepare()

File c:\ProgramData\Anaconda3\envs\test6\lib\site-packages\pydantic\fields.py:345, in ModelField.prepare(self)
    337 """
    338 Prepare the field but inspecting self.default, self.type_ etc.
    339 
    340 Note: this method is **not** idempotent (because _type_analysis is not idempotent),
    341 e.g. calling it it multiple times may modify the field and configure it incorrectly.
    342 """
    344 self._set_default_and_type()
--> 345 self._type_analysis()
    346 if self.required is Undefined:
    347     self.required = True

File c:\ProgramData\Anaconda3\envs\test6\lib\site-packages\pydantic\fields.py:439, in ModelField._type_analysis(self)
    437     self.outer_type_ = self.type_
    438     # re-run to correctly interpret the new self.type_
--> 439     self._type_analysis()
    440 else:
    441     self.sub_fields = [self._create_sub_type(t, f'{self.name}_{display_as_type(t)}') for t in types_]

File c:\ProgramData\Anaconda3\envs\test6\lib\site-packages\pydantic\fields.py:488, in ModelField._type_analysis(self)
    485 # Equality check as almost everything inherits form Iterable, including str
    486 # check for Iterable and CollectionsIterable, as it could receive one even when declared with the other
    487 elif origin in {Iterable, CollectionsIterable}:
--> 488     self.type_ = self.type_.__args__[0]
    489     self.shape = SHAPE_ITERABLE
    490     self.sub_fields = [self._create_sub_type(self.type_, f'{self.name}_type')]

File c:\ProgramData\Anaconda3\envs\test6\lib\typing.py:711, in _BaseGenericAlias.__getattr__(self, attr)
    709 if '__origin__' in self.__dict__ and not _is_dunder(attr):
    710     return getattr(self.__origin__, attr)
--> 711 raise AttributeError(attr)

AttributeError: __args__
evetion commented 2 years ago

I've made releases of both geolib and geolib-plus that should fully support 3.9. 3.10 isn't supported yet.