Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pydantic and Pydantic Settings don't seem to provide library stubs for MyPy #9380

Closed
1 task done
bmitc opened this issue May 3, 2024 · 3 comments
Closed
1 task done
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@bmitc
Copy link

bmitc commented May 3, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

In the Pydantic documentation for MyPy, it is stated that:

Pydantic works well with mypy right out of the box.

However, Pydantic also ships with a mypy plugin that adds a number of important pydantic-specific features to mypy that improve its ability to type-check your code.

However, MyPy generates errors for a simple module,

error: Cannot find implementation or library stub for module named "pydantic"  [import-not-found]
note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
error: Cannot find implementation or library stub for module named "pydantic_settings"  [import-not-found]
error: Class cannot subclass "BaseModel" (has type "Any")  [misc]
error: Class cannot subclass "BaseModel" (has type "Any")  [misc]
error: Class cannot subclass "BaseSettings" (has type "Any")  [misc]

I've removed the line numbers because they don't match up anyway to the code example below since I removed documentation lines, but they don't matter anyway since MyPy doesn't typecheck any Pydantic piece.

As a note, I am using Poetry. Here is my pyproject.toml configuration for MyPy:

[tool.mypy]
plugins = ["pydantic.mypy"]
strict = true
follow_imports = "silent"

Whether plugins is present or not makes no difference to the errors above.

Example Code

from typing import Tuple, Type, Set
from pydantic import BaseModel
from pydantic_settings import (
    BaseSettings,
    PydanticBaseSettingsSource,
    SettingsConfigDict,
    YamlConfigSettingsSource,
)

class _DeviceConfig(BaseModel):
    bench: int
    side: Side

class _TCPDeviceConfig(BaseModel):
    ip_address: str
    port: int
    is_enabled: bool

class EmbeddedDeviceConfig(_TCPDeviceConfig, _DeviceConfig): pass

class FrontendDeviceConfig(_TCPDeviceConfig): pass

class TelemetryInfluxDBConfig(_TCPDeviceConfig): pass

class TelemetryGrafanaDBConfig(_TCPDeviceConfig): pass

class Settings(BaseSettings):
    telemetry_influxdb: TelemetryInfluxDBConfig
    telemetry_grafana: TelemetryGrafanaDBConfig
    embedded_devices: Set[EmbeddedDeviceConfig]
    frontend_device: FrontendDeviceConfig

    model_config = SettingsConfigDict(yaml_file="configuration.yaml")

    # pylint: disable=too-many-arguments
    @classmethod
    def settings_customise_sources(
        cls,
        settings_cls: Type[BaseSettings],
        init_settings: PydanticBaseSettingsSource,
        env_settings: PydanticBaseSettingsSource,
        dotenv_settings: PydanticBaseSettingsSource,
        file_secret_settings: PydanticBaseSettingsSource,
    ) -> Tuple[PydanticBaseSettingsSource, ...]:
        return (YamlConfigSettingsSource(settings_cls),)

Python, Pydantic & OS Version

             pydantic version: 2.7.1
        pydantic-core version: 2.18.2
          pydantic-core build: profile=release pgo=true
                 install path: <poetry-project-root>\.venv\Lib\site-packages\pydantic
               python version: 3.12.1 (tags/v3.12.1:2305ca5, Dec  7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)]
                     platform: Windows-11-10.0.22631-SP0
             related packages: mypy-1.9.0 pydantic-settings-2.2.1 typing_extensions-4.11.0
                       commit: unknown
@bmitc bmitc added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels May 3, 2024
@Viicos
Copy link
Contributor

Viicos commented May 3, 2024

There's probably an issue with your venv management: are you sure you are running mypy in the same env you have pydantic and pydantic-settings installed?

@bmitc
Copy link
Author

bmitc commented May 3, 2024

I don't install anything with pip directly and always add dependencies via poetry add <package>. So for this case, I would have done poetry add pydantic-settings. Also, I force Poetry to write the .venv to the local project directory so that it's always using the local .venv and not some .venv somewhere else on the computer. Lastly, I always run MyPy via Poetry, so it's something like poetry run mypy .... I have several dependencies installed, but Pydantic seems to be the only one with this issue.

Is there a listing of files that one would expect in the .venv for Pydantic such that MyPy can work with it?

@sydney-runkle
Copy link
Member

Hmm, I'm going to move this to a discussion. I think @Viicos is right, this probably relates to venv management.

@pydantic pydantic locked and limited conversation to collaborators May 16, 2024
@sydney-runkle sydney-runkle converted this issue into discussion #9446 May 16, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation
Projects
None yet
Development

No branches or pull requests

3 participants