Using `Optional[str]` instead of just `str` will let the editor help you detect errors where you could be assuming that a value is always a `str`, when it could actually be `None` too.
Using `Optional[str]` instead of just `str` will let the editor help you detect errors where you could be assuming that a value is always a `str`, when it could actually be `None` too.
@ -326,18 +266,18 @@ This also means that in Python 3.10, you can use `Something | None`:
The parameter `name` is defined as `Optional[str]`, but it is **not optional**, you cannot call the function without the parameter:
The parameter `name` is defined as `Optional[str]`, but it is **not optional**, you cannot call the function without the parameter:
@ -390,10 +330,10 @@ You can use the same builtin types as generics (with square brackets and types i
* `set`
* `set`
* `dict`
* `dict`
And the same as with Python 3.8, from the `typing` module:
And the same as with previous Python versions, from the `typing` module:
* `Union`
* `Union`
* `Optional` (the same as with Python 3.8)
* `Optional`
* ...and others.
* ...and others.
In Python 3.10, as an alternative to using the generics `Union` and `Optional`, you can use the <abbrtitle='also called "bitwise or operator", but that meaning is not relevant here'>vertical bar (`|`)</abbr> to declare unions of types, that's a lot better and simpler.
In Python 3.10, as an alternative to using the generics `Union` and `Optional`, you can use the <abbrtitle='also called "bitwise or operator", but that meaning is not relevant here'>vertical bar (`|`)</abbr> to declare unions of types, that's a lot better and simpler.
@ -409,7 +349,7 @@ You can use the same builtin types as generics (with square brackets and types i
* `set`
* `set`
* `dict`
* `dict`
And the same as with Python 3.8, from the `typing` module:
And generics from the `typing` module:
* `Union`
* `Union`
* `Optional`
* `Optional`
@ -417,29 +357,17 @@ And the same as with Python 3.8, from the `typing` module:
////
////
//// tab | Python 3.8+
* `List`
* `Tuple`
* `Set`
* `Dict`
* `Union`
* `Optional`
* ...and others.
////
### Classes as types { #classes-as-types }
### Classes as types { #classes-as-types }
You can also declare a class as the type of a variable.
You can also declare a class as the type of a variable.
@ -507,27 +413,9 @@ Pydantic has a special behavior when you use `Optional` or `Union[Something, Non
Python also has a feature that allows putting **additional <abbr title="Data about the data, in this case, information about the type, e.g. a description.">metadata</abbr>** in these type hints using `Annotated`.
Python also has a feature that allows putting **additional <abbr title="Data about the data, in this case, information about the type, e.g. a description.">metadata</abbr>** in these type hints using `Annotated`.
//// tab | Python 3.9+
Since Python 3.9, `Annotated` is a part of the standard library, so you can import it from `typing`.
In Python 3.9, `Annotated` is part of the standard library, so you can import it from `typing`.