Skip to content

Test case failures in Python 3.9 #1832

@tirkarthi

Description

@tirkarthi

Bug

__args__ was removed in https://bugs.python.org/issue40397 and https://bugs.python.org/issue40398 . It seems a lot of places use it that causes test failures in Python 3.9. A quick grep of args shows many places.

rg __args__ 
pydantic/fields.py
425:            for type_ in self.type_.__args__:
447:            for i, t in enumerate(self.type_.__args__):
449:                    self.type_ = self.type_.__args__[0]
463:            self.type_ = self.type_.__args__[0]
473:            self.type_ = self.type_.__args__[0]
476:            self.type_ = self.type_.__args__[0]
479:            self.type_ = self.type_.__args__[0]
482:            self.key_field = self._create_sub_type(self.type_.__args__[0], 'key_' + self.name, for_keys=True)
483:            self.type_ = self.type_.__args__[1]
488:            self.type_ = self.type_.__args__[0]
497:            self.sub_fields = [self._create_sub_type(t, f'{self.name}_{i}') for i, t in enumerate(self.type_.__args__)]

pydantic/types.py
135:    __args__: List[Type[T]]  # type: ignore
167:    # __args__ is needed to conform to typing generics api
168:    namespace = {'min_items': min_items, 'max_items': max_items, 'item_type': item_type, '__args__': [item_type]}
177:    __args__: Set[Type[T]]  # type: ignore
206:    # __args__ is needed to conform to typing generics api
207:    namespace = {'min_items': min_items, 'max_items': max_items, 'item_type': item_type, '__args__': [item_type]}

pydantic/schema.py
808:            args: Tuple[Any, ...] = type_.__args__

pydantic/typing.py
179:        return type_.__args__
250:            if type_.__args__ is None or not isinstance(type_.__args__[0], type):
252:            return type_.__args__[0]

Sample test failure :

pytest -k test_list_bare  
=============================== test session starts ===============================
platform linux -- Python 3.9.0b5, pytest-5.3.1, py-1.5.3, pluggy-0.12.0
rootdir: /root/checked_repos/pydantic, inifile: setup.cfg, testpaths: tests
plugins: hypothesis-4.43.1, cov-2.8.1
collected 1754 items / 1753 deselected / 1 selected                               

tests/test_edge_cases.py F                                                  [100%]

==================================== FAILURES =====================================
_________________________________ test_list_bare __________________________________

    def test_list_bare():
>       class MyModel(BaseModel):

tests/test_edge_cases.py:1356: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
pydantic/main.py:262: in __new__
    fields[ann_name] = inferred = ModelField.infer(
pydantic/fields.py:309: in infer
    return cls(
pydantic/fields.py:271: in __init__
    self.prepare()
pydantic/fields.py:345: in prepare
    self._type_analysis()
pydantic/fields.py:463: in _type_analysis
    self.type_ = self.type_.__args__[0]
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = typing.List, attr = '__args__'

    def __getattr__(self, attr):
        # We are careful for copy and pickle.
        # Also for simplicity we just don't relay all dunder names
        if '__origin__' in self.__dict__ and not _is_dunder(attr):
            return getattr(self.__origin__, attr)
>       raise AttributeError(attr)
E       AttributeError: __args__

/usr/lib/python3.9/typing.py:640: AttributeError
======================= 1 failed, 1753 deselected in 1.37s ========================

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions