Skip to content

Commit e3603d1

Browse files
committed
Minor improvements
1 parent 703d66d commit e3603d1

2 files changed

Lines changed: 28 additions & 22 deletions

File tree

DOCUMENTATION.rst

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -505,11 +505,11 @@ Some notes about this support are:
505505
are not required to be ``runtime_checkable``. But the accepted classes must
506506
match exactly the signature of the protocol's public methods.
507507

508-
- ``dataclasses`` are supported even when nested. Final classes, attrs'
509-
``define`` decorator, and pydantic's ``dataclass`` decorator and ``BaseModel``
510-
classes are supported and behave like standard dataclasses. For more details
511-
see :ref:`subclasses-disabled`. If a dataclass is mixed inheriting from a normal
512-
class, it is considered a subclass type instead of a dataclass.
508+
- ``dataclasses`` are supported even when nested and by default don't accept
509+
subclasses. Final classes, attrs' ``define``, pydantic's ``dataclass`` and
510+
pydantic's ``BaseModel`` classes are supported and behave like standard
511+
dataclasses. For more details see :ref:`subclasses-disabled`. If a dataclass
512+
is mixed inheriting from a normal class, by default it will accept subclasses.
513513

514514
- User-defined ``Generic`` types are supported. For more details see
515515
:ref:`generic-types`.
@@ -2374,6 +2374,13 @@ specific dataclass, the following can be done:
23742374
subclasses_disabled=[DataClassBaseType],
23752375
)
23762376

2377+
.. note::
2378+
2379+
Enabling subclass support for types is currently experimental. While the
2380+
interface and behavior is expected to be stable, fundamental issues may
2381+
arise that require changes to the design, which could result in breaking
2382+
changes in future releases.
2383+
23772384

23782385
.. _argument-linking:
23792386

jsonargparse/_common.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,16 @@ def set_parsing_settings(
140140
with ``omegaconf+`` parser mode, absolute interpolation paths are
141141
converted to relative. This is only intended for backward
142142
compatibility with ``omegaconf`` parser mode.
143-
subclasses_disabled: List of types or functions, to configure that
144-
when parsing only the exact type hints (not their subclasses) are
145-
accepted. Descendants of the configured types are also disabled.
146-
Functions should return True for types to disable.
147-
subclasses_enabled: List of types or disable function names, to
148-
configure that subclasses are accepted. Types given here have
149-
precedence over those in ``subclasses_disabled``. Giving a function
150-
name removes the corresponding function from
151-
``subclasses_disabled``. By default, the following disable functions
152-
are registered: ``is_pure_dataclass``, ``is_pydantic_model``,
153-
``is_attrs_class`` and ``is_final_class``.
143+
subclasses_disabled: List of types or functions, so that when parsing
144+
only the exact type hints (not their subclasses) are accepted.
145+
Descendants of the configured types are also disabled. Functions
146+
should return ``True`` for types to disable.
147+
subclasses_enabled: List of types or disable function names, so that
148+
subclasses are accepted. Types given here have precedence over those
149+
in ``subclasses_disabled``. Giving a function name removes the
150+
corresponding function from ``subclasses_disabled``. By default, the
151+
following disable functions are registered: ``is_pure_dataclass``,
152+
``is_pydantic_model``, ``is_attrs_class`` and ``is_final_class``.
154153
"""
155154
# validate_defaults
156155
if isinstance(validate_defaults, bool):
@@ -187,8 +186,8 @@ def set_parsing_settings(
187186
# subclass behavior
188187
if subclasses_disabled or subclasses_enabled:
189188
subclass_type_behavior(
190-
subclasses_disabled=subclasses_disabled or [],
191-
subclasses_enabled=subclasses_enabled or [],
189+
subclasses_disabled=subclasses_disabled,
190+
subclasses_enabled=subclasses_enabled,
192191
)
193192

194193

@@ -326,11 +325,11 @@ def is_subclasses_disabled(cls) -> bool:
326325

327326

328327
def subclass_type_behavior(
329-
subclasses_disabled: list[Union[type, Callable[[type], bool]]] = [],
330-
subclasses_enabled: list[Union[type, str]] = [],
328+
subclasses_disabled: Optional[list[Union[type, Callable[[type], bool]]]] = None,
329+
subclasses_enabled: Optional[list[Union[type, str]]] = None,
331330
) -> None:
332331
"""Configures whether class types accept or not subclasses."""
333-
for enable_item in subclasses_enabled:
332+
for enable_item in subclasses_enabled or []:
334333
if isinstance(enable_item, str):
335334
if enable_item not in subclasses_disabled_selectors:
336335
raise ValueError(f"There is no function '{enable_item}' registered in subclasses_disabled")
@@ -342,7 +341,7 @@ def subclass_type_behavior(
342341
f"Expected 'subclasses_enabled' list items to be types or strings, but got {enable_item!r}"
343342
)
344343

345-
for disable_item in subclasses_disabled:
344+
for disable_item in subclasses_disabled or []:
346345
if inspect.isclass(disable_item):
347346
subclasses_disabled_types.add(disable_item)
348347
elif inspect.isfunction(disable_item):

0 commit comments

Comments
 (0)