Open febus982 opened 2 months ago
Thanks @febus982 for reporting this issue. we will try to fix the problem.
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.
The error is related Python < 3.10, because the signature of BaseSettings.__init__
looks like this:
def __init__(
__pydantic_self__,
_case_sensitive: bool | None = None,
_nested_model_default_partial_update: bool | None = None,
_env_prefix: str | None = None,
_env_file: DotenvType | None = ENV_FILE_SENTINEL,
_env_file_encoding: str | None = None,
_env_ignore_empty: bool | None = None,
_env_nested_delimiter: str | None = None,
_env_parse_none_str: str | None = None,
_env_parse_enums: bool | None = None,
_cli_prog_name: str | None = None,
_cli_parse_args: bool | list[str] | tuple[str, ...] | None = None,
_cli_settings_source: CliSettingsSource[Any] | None = None,
_cli_parse_none_str: str | None = None,
_cli_hide_none_type: bool | None = None,
_cli_avoid_json: bool | None = None,
_cli_enforce_required: bool | None = None,
_cli_use_class_docs_for_groups: bool | None = None,
_cli_exit_on_error: bool | None = None,
_cli_prefix: str | None = None,
_cli_implicit_flags: bool | None = None,
_secrets_dir: PathType | None = None,
**values: Any,
) -> None:
You need to maintain backward compatibility or make separate releases for each Python version
I temporarily patched by specifying:
[tool.mypy]
plugins = "pydantic.mypy"
# Suppress errors since pydantic_settings uses X | Y typing
python_version = "3.10"
In the configuration.
Bug Report
mypy identifies false positive
X | Y
errors on not existing lines ifpydantic.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
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 longYour Environment
mypy 1.11.2 (compiled: yes)
--python-version 3.9
(also--no-incremental
to show line numbers but the error persists without)mypy.ini
(and other config files): from pyproject.tomlWe can remove celery by installing
celery-types
but needsat least python 3.10. For now we maintain the override.
[[tool.mypy.overrides]] module = [ "celery.*" ] ignore_missing_imports = true