Skip to content

How to keep mypy happy using pydantic.DirectoryPath with a default value #2098

@michaeloliverx

Description

@michaeloliverx

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and couldn't find an answer
  • After submitting this, I commit to one of:
    • Look through open issues and helped at least one other person
    • Hit the "watch" button on this repo to receive notifications and I commit to help at least 2 people that ask questions in the future
    • Implement a Pull Request for a confirmed bug

Question

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.7.2
            pydantic compiled: True
                 install path: /Users/michaeloliver/echo/app/.venv/lib/python3.9/site-packages/pydantic
               python version: 3.9.0 (default, Oct 27 2020, 14:15:17)  [Clang 12.0.0 (clang-1200.0.32.21)]
                     platform: macOS-10.16-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']

I would like to use DirectoryPath with a default value if no environment override exists (using BaseSettings)

from pathlib import Path

from pydantic import BaseSettings, DirectoryPath

BASE_DIR = Path(__file__).parent


class Config(BaseSettings):
    CONTENT_DIR: DirectoryPath = BASE_DIR / "content"

mypy complains:

src/hublib/config.py:8: error: Incompatible types in assignment (expression has type "Path", variable has type "DirectoryPath")

What would be the best approach ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions