pydantic / pydantic-settings

Settings management using pydantic
https://docs.pydantic.dev/latest/usage/pydantic_settings/
MIT License
584 stars 60 forks source link

Mypy plugin reports false positives and not existing lines #403

Open febus982 opened 2 weeks ago

febus982 commented 2 weeks ago

Bug Report

mypy identifies false positive X | Y errors on not existing lines if pydantic.mypy plugin is enabled.

Note that I cannot find any instance of X | Y in the whole repository.

To Reproduce

This is the file: https://github.com/febus982/bootstrap-python-fastapi/blob/main/src/bootstrap/config.py

from pathlib import Path
from typing import Dict, Literal, Optional

from pydantic import BaseModel
from pydantic_settings import BaseSettings, SettingsConfigDict
from sqlalchemy_bind_manager import SQLAlchemyConfig

TYPE_ENVIRONMENT = Literal["local", "test", "staging", "production"]

class CeleryConfig(BaseModel):
    # https://docs.celeryq.dev/en/stable/userguide/configuration.html#configuration

    timezone: str = "UTC"

    # Broker config
    broker_url: Optional[str] = None
    broker_connection_retry_on_startup: bool = True

    # Results backend config
    result_backend: Optional[str] = None
    redis_socket_keepalive: bool = True

    # Enable to ignore the results by default and not produce tombstones
    task_ignore_result: bool = False

    # We want to use the default python logger configured using structlog
    worker_hijack_root_logger: bool = False

    # Events enabled for monitoring
    worker_send_task_events: bool = True
    task_send_sent_event: bool = True

    # Recurring tasks triggered directly by Celery
    beat_schedule: dict = {}
    # beat_schedule: dict = {
    #     "recurrent_example": {
    #         "task": "domains.books._tasks.book_cpu_intensive_task",
    #         "schedule": 5.0,
    #         "args": ("a-random-book-id",),
    #     },
    # }

class AppConfig(BaseSettings):
    model_config = SettingsConfigDict(env_nested_delimiter="__")

    APP_NAME: str = "bootstrap"
    CELERY: CeleryConfig = CeleryConfig()
    DEBUG: bool = False
    ENVIRONMENT: TYPE_ENVIRONMENT = "local"
    SQLALCHEMY_CONFIG: Dict[str, SQLAlchemyConfig] = dict(
        default=SQLAlchemyConfig(
            engine_url=f"sqlite+aiosqlite:///{Path(__file__).parent.parent.joinpath('sqlite.db')}",
            engine_options=dict(
                connect_args={
                    "check_same_thread": False,
                },
                echo=False,
                future=True,
            ),
            async_engine=True,
        ),
    )

Command used: mypy --python-version 3.9 --no-incremental from the root repository directory.

Expected Behavior

No errors reported

Actual Behavior

Note that the src/bootstrap/config.py file is only 64 lines long

src/bootstrap/config.py:128: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:129: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:130: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:131: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:132: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:133: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:134: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:135: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:136: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:137: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:138: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:139: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:140: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:141: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:142: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:143: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:144: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:145: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:146: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:147: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/bootstrap/config.py:148: error: X | Y syntax for unions requires Python 3.10  [syntax]
Found 21 errors in 1 file (checked 63 source files)

Your Environment

We can remove celery by installing celery-types but needs

at least python 3.10. For now we maintain the override.

[[tool.mypy.overrides]] module = [ "celery.*" ] ignore_missing_imports = true


- Python version used: Python 3.11.9 (but experienced also directly on 3.9 and 3.12)
- Pydantic and Pydantic-settings versions used: `pydantic==2.9.1` and `pydantic-settings==2.5.3` -  the issue didn't occur on `pydantic==2.8.2` and `pydantic-settings==2.3.4`
hramezani commented 1 week ago

Thanks @febus982 for reporting this issue. we will try to fix the problem.

user1584 commented 11 hours ago

I get the same 'error: X | Y syntax for unions requires Python 3.10 [syntax]' for the following file:

import pydantic_settings

class Temp(pydantic_settings.BaseSettings):
    pass

I'm pretty sure there's no '|' in there. 😃 Some versions: mypy=1.11.1 pydantic=2.8.2 pydantic-core=2.20.1 pydantic-settings=2.5.2

What's really strange is that I do not get the error in the CI-pipeline, which uses the exact same versions. I deleted the .mypy_cache folder multiple times but the error persists.