Skip to content

Commit 0f8e54e

Browse files
committed
add fix
1 parent e37034c commit 0f8e54e

File tree

2 files changed

+32
-39
lines changed

2 files changed

+32
-39
lines changed

packages/settings-library/src/settings_library/base.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from functools import cached_property
3-
from typing import Any, Final, get_origin
3+
from typing import Any, Final
44

55
from common_library.pydantic_fields_extension import get_type, is_literal, is_nullable
66
from pydantic import ValidationInfo, field_validator
@@ -15,9 +15,9 @@
1515

1616
_logger = logging.getLogger(__name__)
1717

18-
_AUTO_DEFAULT_FACTORY_RESOLVES_TO_NONE_FSTRING: Final[
19-
str
20-
] = "{field_name} auto_default_from_env unresolved, defaulting to None"
18+
_AUTO_DEFAULT_FACTORY_RESOLVES_TO_NONE_FSTRING: Final[str] = (
19+
"{field_name} auto_default_from_env unresolved, defaulting to None"
20+
)
2121

2222

2323
class DefaultFromEnvFactoryError(ValueError):
@@ -133,28 +133,15 @@ def __pydantic_init_subclass__(cls, **kwargs: Any):
133133
for name, field in cls.model_fields.items():
134134
auto_default_from_env = _is_auto_default_from_env_enabled(field)
135135
field_type = get_type(field)
136-
137-
# Avoids issubclass raising TypeError. SEE test_issubclass_type_error_with_pydantic_models
138-
is_not_composed = (
139-
get_origin(field_type) is None
140-
) # is not composed as dict[str, Any] or Generic[Base]
141136
is_not_literal = not is_literal(field)
142137

143-
if (
144-
is_not_literal
145-
and is_not_composed
146-
and issubclass(field_type, BaseCustomSettings)
147-
):
138+
if is_not_literal and issubclass(field_type, BaseCustomSettings):
148139
if auto_default_from_env:
149140
# Builds a default factory `Field(default_factory=create_settings_from_env(field))`
150141
field.default_factory = _create_settings_from_env(name, field)
151142
field.default = None
152143

153-
elif (
154-
is_not_literal
155-
and is_not_composed
156-
and issubclass(field_type, BaseSettings)
157-
):
144+
elif is_not_literal and issubclass(field_type, BaseSettings):
158145
msg = f"{cls}.{name} of type {field_type} must inherit from BaseCustomSettings"
159146
raise ValueError(msg)
160147

packages/settings-library/tests/test_base.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -335,28 +335,34 @@ class SettingsClassExt(SettingsClass):
335335
}
336336

337337

338-
def test_issubclass_type_error_with_pydantic_models():
339-
# There is a problem
340-
#
341-
# TypeError: issubclass() arg 1 must be a class
342-
#
343-
# SEE https://github.com/pydantic/pydantic/issues/545
344-
#
345-
# >> issubclass(dict, BaseSettings)
346-
# False
347-
# >> issubclass(dict[str, str], BaseSettings)
348-
# Traceback (most recent call last):
349-
# File "<string>", line 1, in <module>
350-
# File "/home/crespo/.pyenv/versions/3.10.13/lib/python3.10/abc.py", line 123, in __subclasscheck__
351-
# return _abc_subclasscheck(cls, subclass)
352-
# TypeError: issubclass() arg 1 must be a class
353-
#
338+
def test_fixed_issubclass_type_error_with_pydantic_models():
354339

355340
assert not issubclass(dict, BaseSettings)
356-
357-
# NOTE: this should be fixed by pydantic at some point. When this happens, this test will fail
358-
with pytest.raises(TypeError):
359-
issubclass(dict[str, str], BaseSettings)
341+
assert not issubclass(
342+
# FIXED with
343+
#
344+
# pydantic 2.11.7
345+
# pydantic_core 2.33.2
346+
# pydantic-extra-types 2.10.5
347+
# pydantic-settings 2.7.0
348+
#
349+
#
350+
# TypeError: issubclass() arg 1 must be a class
351+
#
352+
# SEE https://github.com/pydantic/pydantic/issues/545
353+
#
354+
# >> issubclass(dict, BaseSettings)
355+
# False
356+
# >> issubclass(dict[str, str], BaseSettings)
357+
# Traceback (most recent call last):
358+
# File "<string>", line 1, in <module>
359+
# File "/home/crespo/.pyenv/versions/3.10.13/lib/python3.10/abc.py", line 123, in __subclasscheck__
360+
# return _abc_subclasscheck(cls, subclass)
361+
# TypeError: issubclass() arg 1 must be a class
362+
#
363+
dict[str, str],
364+
BaseSettings,
365+
)
360366

361367
# here reproduces the problem with our settings that ANE and PC had
362368
class SettingsClassThatFailed(BaseCustomSettings):

0 commit comments

Comments
 (0)