Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v0.10.3 (2025-01-19)

- Don't fail but warn, when we suspect that un unquoted string might actually be an object in config files.
- Improved typing checks for drafts: we forbid string type hints, as we cannot validate against them

## v0.10.2 (2025-12-11)

Expand Down
22 changes: 19 additions & 3 deletions confit/draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
R = TypeVar("R", covariant=True)


def _display_type_name(obj: Any) -> str:
return (
getattr(obj, "__qualname__", None)
or getattr(obj, "__name__", None)
or getattr(obj, "_name", None)
or str(obj)
)


# Note ! R (Return) and P (Parameters) are reversed compared to
# the usual convention of Protocols, because Jedi can't handle it otherwise
class Draftable(Protocol[R, P]):
Expand Down Expand Up @@ -126,11 +135,11 @@ def __get_pydantic_core_schema__(cls, source, handler):
return core_schema.no_info_plain_validator_function(cls.validate)

def __repr__(self):
return f"Draft[{self.type_.__qualname__}]"
return f"Draft[{_display_type_name(self.type_)}]"

def __instancecheck__(cls, obj):
if isinstance(type(obj), MetaDraft):
return obj.type_ == cls.type_ or cls.type_ is Any
return cls.type_ is Any or issubclass(obj.type_, cls.type_)
return False


Expand All @@ -147,6 +156,13 @@ def __init__(
):
self._func = func
self._kwargs = kwargs
return_type = inspect.signature(func).return_annotation
if isinstance(return_type, str):
raise RuntimeError(
f"{func} has been annotated as a string return type, "
f"and therefore cannot be used to validate the output : please "
"type hint it using non string type hints."
)

def instantiate(self, **kwargs) -> R:
"""
Expand Down Expand Up @@ -179,4 +195,4 @@ def __getattr__(self, name):
self._raise_draft_error()

def __repr__(self):
return f"Draft[{self._func.__qualname__}]"
return f"Draft[{_display_type_name(self._func)}]"
2 changes: 1 addition & 1 deletion confit/utils/xjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def loads(s: str):
except Exception:
if "\n" in s or "\r" in s:
return s
# Fail if we suspect that it is a malformed object
# Warn if we suspect that it is a malformed object
# (e.g. has ', ", {, }, [, ] in it)
if set(s) & set(",'\"{}[]$"):
warnings.warn(
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ docs = {requires-python = ">=3.9"}
[tool.setuptools.dynamic]
version = { attr = "confit.__version__" }

[tool.setuptools.packages]
find = {where = ["."], include = ["confit"]}

[tool.interrogate]
ignore-init-method = false
ignore-init-module = false
Expand Down
Loading