Skip to content

AttributeError: __qualname__ on Python 3.9 with typing.Sequence, Collection, Iterable or other generic aliases #493

@palotasb

Description

@palotasb

To reproduce:

$ python3.9 -c 'from pydantic_settings import BaseSettings, CliSettingsSource
from typing import *
class S(BaseSettings):
    s: Sequence[int]  # or Collection, Iterable, etc.
CliSettingsSource(S)'
Traceback (most recent call last):
  File "<string>", line 5, in <module>
  File "/.../.venv/lib/python3.9/site-packages/pydantic_settings/sources.py", line 1165, in __init__
    self._connect_root_parser(
  File "/.../.venv/lib/python3.9/site-packages/pydantic_settings/sources.py", line 1574, in _connect_root_parser
    self._add_parser_args(
  File "/.../.venv/lib/python3.9/site-packages/pydantic_settings/sources.py", line 1656, in _add_parser_args
    kwargs['metavar'] = self._metavar_format(field_info.annotation)
  File "/.../.venv/lib/python3.9/site-packages/pydantic_settings/sources.py", line 1886, in _metavar_format
    return self._metavar_format_recurse(obj).replace(', ', ',')
  File "/.../.venv/lib/python3.9/site-packages/pydantic_settings/sources.py", line 1874, in _metavar_format_recurse
    list(map(self._metavar_format_recurse, self._get_modified_args(obj))), obj_qualname=obj.__qualname__
  File "/opt/homebrew/Cellar/[email protected]/3.9.20/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 711, in __getattr__
    raise AttributeError(attr)
AttributeError: __qualname__

The error also happens when using types based on typing._BaseGenericAlias, like typing.Collection or typing.Iterable

The call ultimately fails on this line:
https://github.com/python/cpython/blob/3.9/Lib/typing.py#L711

The support for .__qualname__ for generic aliases was added only in Python 3.10:
https://github.com/python/cpython/blob/3.10/Lib/typing.py#L977

Hence this call on Python 3.9 is wrong:
https://github.com/pydantic/pydantic-settings/blob/v2.6.1/pydantic_settings/sources.py#L1874

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions