It's not possible to use Pydantic BaseSettings with a package called with Typer CLI
See original GitHub issueCopied original issue from: https://github.com/tiangolo/typer/issues/91
Describe the bug
I am using typer as a package where I get the following error
.
.
File "/root/typer_app/main.py", line 9, in publish
class Settings(BaseSettings):
File "pydantic/main.py", line 184, in pydantic.main.ModelMetaclass.__new__
File "pydantic/typing.py", line 136, in pydantic.typing.resolve_annotations
if arg is None:
KeyError: 'typer_app.main'
To Reproduce
I am using typer as a package
/root
| typer_app
| __init__.py
| main.py
where
main.py
import typer
from pydantic import BaseSettings
app = typer.Typer()
@app.command()
def publish():
class Settings(BaseSettings):
HTTP_ENDPOINT: str
settings = Settings()
typer.echo(settings.HTTP_ENDPOINT)
- Call it with:
PYTHONPATH=/root typer typer_app.main run
- It outputs:
.
.
File "/root/typer_app/main.py", line 9, in publish
class Settings(BaseSettings):
File "pydantic/main.py", line 184, in pydantic.main.ModelMetaclass.__new__
File "pydantic/typing.py", line 136, in pydantic.typing.resolve_annotations
if arg is None:
KeyError: 'typer_app.main'
- But I expected it to output a validation error since HTTP_ENDPOINT is not set.
Environment
Linux Mint - Python 3.8.2
typer==0.1.1
typer-cli==0.0.8
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Settings management - pydantic
If you create a model that inherits from BaseSettings , the model initialiser will attempt to determine the values of any fields not...
Read more >pydantic/Lobby - Gitter
I need to manipulate fields after object creation. For example a model has a field called "sequences" (represented by another model), where I...
Read more >pydantic-cli - PyPI
Pydantic Commandline Tool Interface. Turn Pydantic defined Data Models into CLI Tools and enable loading values from JSON files. Requires Pydantic >=1.5.1 ....
Read more >Pedantic Configuration Management with Pydantic
I wanted to take a declarative approach while designing a config management pipleline that will be modular, scalable and easy to maintain. To ......
Read more >Settings and Environment Variables - FastAPI
If you already know what "environment variables" are and how to use them, ... Import BaseSettings from Pydantic and create a sub-class, very...
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 Free
Top 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

Can I look into this?
@tiangolo I wasn’t able to get around to looking at the issue this week! If anyone else is interested in working on it, please be my guest 😃