Pydantic model objects evaluate to None when used in group_data files
See original GitHub issueDescribe the bug When using an object that inherits from pydantic’s BaseModel as host data, it evaluates to None.
To Reproduce
# group_data/server.py
from pydantic import BaseModel
class DhcpData(BaseModel):
shared_network_name: str
dhcp = DhcpData(shared_network_name='pxe.lan')
# inventories/pxe.py
server = ['kvm-1']
# templates/configure.j2
{{host.data.dhcp.shared_network_name}}
# operations.py
from pyinfra import host
from pyinfra.operations import files
assert host.data.dhcp is not None
assert host.data.dhcp.shared_network_name == 'pxe.lan'
files.template(
name="Test",
src='templates/configure.j2',
dest='configure.sh',
mode='700',
)
$ pyinfra inventories/pxe.py operations.py
Expected behavior
host.data.dhcp
would be available for use in operations.py
or in any jinja template in the project directory.
Meta
- Include output of
pyinfra --support
.
System: Darwin
Platform: Darwin-19.6.0-x86_64-i386-64bit
Release: 19.6.0
Machine: x86_64
pyinfra: v1.2.1
Executable: /Users/mark/.pyenv/versions/aircraft/bin/pyinfra
Python: 3.7.7 (CPython, Clang 11.0.0 (clang-1100.0.33.17))
- How was pyinfra installed (source/pip)?
pip
- debug.log
File "/Users/mark/.pyenv/versions/3.7.7/envs/aircraft/lib/python3.7/site-packages/pyinfra_cli/main.py", line 216, in cli
_main(*args, **kwargs)
File "/Users/mark/.pyenv/versions/3.7.7/envs/aircraft/lib/python3.7/site-packages/pyinfra_cli/main.py", line 543, in _main
load_deploy_file(state, filename)
File "/Users/mark/.pyenv/versions/3.7.7/envs/aircraft/lib/python3.7/site-packages/pyinfra_cli/util.py", line 168, in load_deploy_file
exec_file(filename)
File "/Users/mark/.pyenv/versions/3.7.7/envs/aircraft/lib/python3.7/site-packages/pyinfra_cli/util.py", line 52, in exec_file
exec(PYTHON_CODES[filename], data)
File "operations.py", line 4, in <module>
assert host.data.dhcp is not None
AssertionError
- Consider including output with
-vv
and--debug
.
[pyinfra_cli.main] Checking potential directory:
[pyinfra_cli.main] Setting directory to:
--> Loading config...
--> Loading inventory...
[pyinfra_cli.inventory] Creating fake inventory...
[pyinfra_cli.inventory] Looking for group data in: group_data/server.py
--> Connecting to hosts...
[pyinfra.api.connectors.ssh] Connecting to: kvm-1 ({'allow_agent': True, 'look_for_keys': True, 'hostname': 'kvm-1', 'timeout': 10})
[kvm-1] Connected
[pyinfra.api.state] Activating host: kvm-1
--> Preparing operations...
Loading: operations.py
--> An unexpected exception occurred:
File "operations.py", line 4, in <module>
assert host.data.dhcp is not None
AssertionError
[pyinfra_cli.exceptions] File "/Users/mark/.pyenv/versions/3.7.7/envs/aircraft/lib/python3.7/site-packages/pyinfra_cli/main.py", line 216, in cli
_main(*args, **kwargs)
File "/Users/mark/.pyenv/versions/3.7.7/envs/aircraft/lib/python3.7/site-packages/pyinfra_cli/main.py", line 543, in _main
load_deploy_file(state, filename)
File "/Users/mark/.pyenv/versions/3.7.7/envs/aircraft/lib/python3.7/site-packages/pyinfra_cli/util.py", line 168, in load_deploy_file
exec_file(filename)
File "/Users/mark/.pyenv/versions/3.7.7/envs/aircraft/lib/python3.7/site-packages/pyinfra_cli/util.py", line 52, in exec_file
exec(PYTHON_CODES[filename], data)
File "operations.py", line 4, in <module>
assert host.data.dhcp is not None
[pyinfra_cli.exceptions] AssertionError
--> The full traceback has been written to pyinfra-debug.log
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Return Pydantic model as None if all fields are None
Now, I would like the pydantic object to be None if none of the field members were set. Example: data_a = { 'field_1':...
Read more >Postponed annotations - pydantic
Self-referencing fields will be automatically resolved after model creation. Within the model, you can refer to the not-yet-constructed model using a string:.
Read more >8 Reasons to Start Using Pydantic to Improve Data Parsing ...
1 — A simple syntax to define your data models. You can define your data inside a class that inherits from the BaseModel...
Read more >pydantic-factories - PyPI
Mock data generation for pydantic based models. ... It can also be used with other libraries that use pydantic as a ... Generating...
Read more >Validate JSON Documents in Python using Pydantic
Developers can specify the schema by defining a model. It has a rich set of features to do a variety of validations. We...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Now released in
1.3.1
!(will release this in v1.3.1 today/this week).