question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It's not possible to use Pydantic BaseSettings with a package called with Typer CLI

See original GitHub issue

Copied 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:closed
  • Created 3 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
kbanccommented, Jul 30, 2020

Can I look into this?

1reaction
kbanccommented, Aug 21, 2020

@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 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found