Skip to content

Commit 7eb9281

Browse files
committed
re-add but deprecate the newtype documentation
1 parent 113c478 commit 7eb9281

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class Sample:
242242

243243
See [marshmallow's documentation about extending `Schema`](https://marshmallow.readthedocs.io/en/stable/extending.html).
244244

245-
### Custom type declarations
245+
### Custom type aliases
246246

247247
This library allows you to specify [customized marshmallow fields](https://marshmallow.readthedocs.io/en/stable/custom_fields.html#creating-a-field-class) using python's Annoted type [PEP-593](https://peps.python.org/pep-0593/).
248248

@@ -269,6 +269,32 @@ For convenience, some custom types are provided:
269269
from marshmallow_dataclass.typing import Email, Url
270270
```
271271

272+
### Custom NewType declarations [__deprecated__]
273+
274+
> NewType is deprecated in favor or type aliases using Annotated, as described above.
275+
276+
This library exports a `NewType` function to create types that generate [customized marshmallow fields](https://marshmallow.readthedocs.io/en/stable/custom_fields.html#creating-a-field-class).
277+
278+
Keyword arguments to `NewType` are passed to the marshmallow field constructor.
279+
280+
```python
281+
import marshmallow.validate
282+
from marshmallow_dataclass import NewType
283+
284+
IPv4 = NewType(
285+
"IPv4", str, validate=marshmallow.validate.Regexp(r"^([0-9]{1,3}\\.){3}[0-9]{1,3}$")
286+
)
287+
```
288+
289+
You can also pass a marshmallow field to `NewType`.
290+
291+
```python
292+
import marshmallow
293+
from marshmallow_dataclass import NewType
294+
295+
Email = NewType("Email", str, field=marshmallow.fields.Email)
296+
```
297+
272298
Note: if you are using `mypy`, you will notice that `mypy` throws an error if a variable defined with
273299
`NewType` is used in a type annotation. To resolve this, add the `marshmallow_dataclass.mypy` plugin
274300
to your `mypy` configuration, e.g.:

0 commit comments

Comments
 (0)