Download Python/val/3.13/dataclass-transform/modern_03.py from SwashBuckler001/idiombench: direct link, hf CLI and curl.
- Browser
- Download file 1.71 kB
-
https://huggingface.co/datasets/SwashBuckler001/idiombench/resolve/main/Python/val/3.13/dataclass-transform/modern_03.py
- Command line
-
hf download hf://datasets/SwashBuckler001/idiombench/Python/val/3.13/dataclass-transform/modern_03.py
-
curl -L -o modern_03.py https://huggingface.co/datasets/SwashBuckler001/idiombench/resolve/main/Python/val/3.13/dataclass-transform/modern_03.py
1.71 kB
| """ | |
| Tests the dataclass_transform mechanism honors implicit default values | |
| in field parameters. | |
| """ | |
| # Specification: https://typing.readthedocs.io/en/latest/spec/dataclasses.html#field-specifier-parameters | |
| from typing import Any, Callable, Literal, TypeVar, dataclass_transform, overload | |
| T = TypeVar("T") | |
| # > Field specifier functions can use overloads that implicitly specify the | |
| # > value of init using a literal bool value type (Literal[False] or Literal[True]). | |
| def field1( | |
| *, | |
| default: str | None = None, | |
| resolver: Callable[[], Any], | |
| init: Literal[False] = False, | |
| ) -> Any: | |
| ... | |
| def field1( | |
| *, | |
| default: str | None = None, | |
| resolver: None = None, | |
| init: Literal[True] = True, | |
| ) -> Any: | |
| ... | |
| def field1( | |
| *, | |
| default: str | None = None, | |
| resolver: Callable[[], Any] | None = None, | |
| init: bool = True, | |
| ) -> Any: | |
| ... | |
| def field2(*, init: bool = False, kw_only: bool = True) -> Any: | |
| raise NotImplementedError | |
| def create_model(*, init: bool = True) -> Callable[[type[T]], type[T]]: | |
| raise NotImplementedError | |
| class CustomerModel1: | |
| id: int = field1(resolver=lambda: 0) | |
| name: str = field1(default="Voldemort") | |
| CustomerModel1() | |
| CustomerModel1(name="hi") | |
| # This should generate an error because "id" is not | |
| # supposed to be part of the init function. | |
| CustomerModel1(id=1, name="hi") # E | |
| class CustomerModel2: | |
| id: int = field2() | |
| name: str = field2(init=True) | |
| # This should generate an error because kw_only is True | |
| # by default for field2. | |
| CustomerModel2(1) # E | |
| CustomerModel2(name="Fred") | |