Skip to content

Commit

Permalink
add docs about avoiding type warnings for pydantic models which are v…
Browse files Browse the repository at this point in the history
…ariables (#902)
  • Loading branch information
dantownsend committed Nov 7, 2023
1 parent bfb6acd commit e4946d8
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/src/piccolo/serialization/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,40 @@ add additional fields, and to override existing fields.
>>> CustomBandModel(name="Pythonistas", genre="Rock")
Or even simpler still:

.. code-block:: python
class BandModel(create_pydantic_model(Band)):
genre: str
Avoiding type warnings
~~~~~~~~~~~~~~~~~~~~~~

Some linters will complain if you use variables in type annotations:

.. code-block:: python
BandModel = create_pydantic_model(Band)
def my_function(band: BandModel): # Variable not allowed in type expression!
...
The fix is really simple:

.. code-block:: python
# We now have a class instead of a variable:
class BandModel(create_pydantic_model(Band)):
...
def my_function(band: BandModel):
...
Source
~~~~~~

Expand Down

0 comments on commit e4946d8

Please sign in to comment.